pythonnumpymatplotlibsyntax-errorsentinel

import image-plot the SentinelHub-py utils issue


I am trying to plot an image using SentinelHub-py. This is part of my the code:

from sentinelhub import SHConfig   

config = SHConfig()
config.sh_client_id = "76186bb6-a02e-4457-9a9d-126e4fffaed4"
config.sh_client_secret = "aTlX[s:39vzA8p}HA{k*Zp!fJNF~(c7e.u7r21V!"

config.save()

%reload_ext autoreload
%autoreload 2
%matplotlib inline

#Import them
import os
import datetime
import numpy as np
import matplotlib.pyplot as plt


from sentinelhub import SHConfig
from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, bbox_to_dimensions, DownloadRequest

from utils import plot_image

betsiboka_coords_wgs84 = [46.16, -16.15, 46.51, -15.58]

resolution = 60
betsiboka_bbox = BBox(bbox=betsiboka_coords_wgs84, crs=CRS.WGS84)
betsiboka_size = bbox_to_dimensions(betsiboka_bbox, resolution=resolution)

print(f'Image shape at {resolution} m resolution: {betsiboka_size} pixels')

"""
Utilities used by example notebooks
"""
import matplotlib.pyplot as plt
import numpy as np


def plot_image(image, factor=3.5/255, clip_range=(0,1)):
    """
    Utility function for plotting RGB images.
    """
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
    if clip_range is not None:
        ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
    else:
        ax.imshow(image * factor, **kwargs)
    ax.set_xticks([])
    ax.set_yticks([])

which give me the following error:

    ---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6632/1226496077.py in <module>
      9 from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, DataCollection, bbox_to_dimensions, DownloadRequest
     10 
---> 11 from utils import plot_image
     12 
     13 CLIENT_ID='76186bb6-a02e-4457-9a9d-126e4fffaed4'

ImportError: cannot import name 'plot_image' from 'utils' (c:\xxxxxxxxxxxx.py)

someone made a comment on this issue by saying:

"It seems that the utils package that you are calling is not the correct one. Try loading the utils.py from the examples folder, or that you can find [here][1] (i.e. copy the file to your working directory with your notebook)."
[1]: https://github.com/sentinel-hub/sentinelhub-py

I change my code to this:

    """
Utilities used by example notebooks
"""
import matplotlib.pyplot as plt
import numpy as np


def plot_image(image, factor=3.5/255, clip_range=(0,1)):
    """
    Utility function for plotting RGB images.
    """
    fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))
    if clip_range is not None:
        ax.imshow(np.clip(image * factor, *clip_range), **kwargs)
    else:
        ax.imshow(image * factor, **kwargs)
    ax.set_xticks([])
    ax.set_yticks([])

still didn't plot the image.

Please, any suggestions?


Solution

  • You have to navigate to your utils.py. In my computer is:

    (/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/utils).

    Later, you have to copy and paste the next script from:

    https://github.com/sentinel-hub/sentinelhub-py/blob/master/examples/utils.py

    Finally, you will be able to type in your python script: "from utils import plot_image"

    Best, Jose