import rastereasy

With rastereasy, you can plot spectral bands associated with selected pixels by a simple click. You can also extract in a list of numpy arrays the associated values.

Important for google colab

To use the interactive plotting features in Google Colab, a special two-step setup is required. Follow these steps in the exact order. Separating the commands into different cells and restarting the session is essential.

Step 1: Install Libraries

Run the following cell to install rastereasy and the necessary dependencies for interactive widgets.

!pip install rastereasy ipympl
from google.colab import output
output.enable_custom_widget_manager()

Step 2: Restart the Runtime

After the installation is complete, you must restart the runtime.

Go to the menu: Runtime > Restart runtime (or use the shortcut Ctrl+M).

Step 3: Run Your Code

After restarting, you can now enable the interactive mode and use the library in a new cell.

%matplotlib widget
import rastereasy

1) Open an image

name_im='./data/demo/sentinel.tif'
names = {"NIR":8,"G":3,"CO" : 1,"SWIR2":11,"B": 2,"R":4,"RE1":5,"RE2":6,"RE3":7,"WA":9,"SWIR1":10,"SWIR3":12}
image_names=rastereasy.open(name_im,names=names)

2) Plot spectra by clicking on pixels

help(image_names.plot_spectra)
Help on method plot_spectra in module rastereasy:

plot_spectra(
    bands=None,
    fig_size=(15, 5),
    percentile=2,
    title='',
    title_im='Original image (click outside to stop)',
    title_spectra='Spectra',
    xlabel='Bands',
    ylabel='Value',
    zoom=None,
    pixel=None
) method of rastereasy.Geoimage instance
    Interactive tool to explore and plot spectral values from user-selected pixels.

    This method displays the image and allows the user to click on pixels to see
    their spectral values across all bands plotted as a line graph. Multiple pixels
    can be selected to compare different spectral signatures.

    Parameters
    ----------
    bands : list of str, optional
        List of three band identifiers to use for the background image display.
        If None, uses the first three bands in the image.
        Default is None.

    fig_size : tuple, optional
        Size of the figure in inches as (width, height).
        Default is (15, 5).

    percentile : int, optional
        Percentile value for contrast stretching of the background image.
        Default is 2.

    title : str, optional
        Main title for the figure.
        Default is ''.

    title_im : str, optional
        Title for the image panel.
        Default is "Original image (click outside to stop)".

    title_spectra : str, optional
        Title for the spectral plot panel.
        Default is "Spectra".

    xlabel : str, optional
        X-axis label for the spectral plot.
        Default is "Bands".

    ylabel : str, optional
        Y-axis label for the spectral plot.
        Default is "Value".

    zoom : tuple, optional
        To visualize  only a window of the image
            If based on pixel coordinates, you must indicate
            - the row/col coordinades of
                    the north-west corner (deb_row,deb_col)
            - the row/col coordinades of
                    the south-east corner (end_row,end_col)
            in a tuple  `zoom = ((deb_row,end_row),(deb_col,end_col))`

            If based on latitude/longitude coordinates, you must indicate
            - the lat/lon coordinades of the north-west corner (lat1,lon1)
            - the lat/lon coordinades of the south-east corner (lat2,lon2)
            `zoom = ((lon1,lon2),(lat1,lat2))`
        If not provide, visualize the entire image

    pixel : bool, optional
        Coordinate system flag, if zoom is given:
        - If True: Coordinates are interpreted as pixel indices
        - If False: Coordinates are interpreted as geographic coordinates
        Default is True.



    Returns
    -------
    tuple
        A tuple containing:
        - series : list of lists - Spectral values for each selected pixel
        - pixel_i : list of int - Row coordinates of selected pixels
        - pixel_j : list of int - Column coordinates of selected pixels

    Examples
    --------
    >>> # Explore spectral signatures in the image
    >>> spectra, rows, cols = image.plot_spectra()
    >>> print(f"Selected {len(spectra)} pixels")
    >>>
    >>> # Customize the display
    >>> spectra, rows, cols = image.plot_spectra(
    >>>     bands=["NIR", "R", "G"],
    >>>     title_im="Click on different vegetation types",
    >>>     title_spectra="Vegetation Spectral Signatures")
    >>>
    >>> # Zoom of a part of the image
    >>> spectra, rows, cols = image.plot_spectra(
    >>>     bands=["NIR", "R", "G"],
    >>>     zoom=((100,200),(100,400)),
    >>>     title_im="Click on different vegetation types",
    >>>     title_spectra="Vegetation Spectral Signatures")

    Notes
    -----
    To end pixel selection, click outside the image area or on the "Finish" button.
    This tool is particularly useful for:
    - Exploring spectral differences between land cover types
    - Identifying spectral anomalies
    - Training classification algorithms
    - Building spectral libraries

Plot spectra with the three first bands in color composition

You can click on the Finish button or outside the image to stop the visualization.

# If google colab, do not forget to put `%matplotlib widget` before importing rastereasy
image_names.plot_spectra()
# -> This will plot an image where you can click to visualize spectre
([], [], [])

Plot spectra with selected bands for color composition

image=rastereasy.open(name_im)
# If google colab, do not forget to put `%matplotlib widget` before importing rastereasy
image.plot_spectra(['5','1','3'],title_im='Sentinel-2 image',title_spectra='associated spectra',xlabel='given bands',ylabel='spectral values')

# -> This will plot an image where you can click to visualize spectre
([], [], [])

3) Get spectral values of some pixels selected by a simple click

To this end, you need to put in some variables the outputs

# If google colab, do not forget to put `%matplotlib widget` before importing rastereasy
series, pixels_i,pixels_j = image.plot_spectra(['8','9','2'],title_im='Sentinel-2 image',title_spectra='associated spectra',xlabel='given bands',ylabel='spectral values')
print('collect %d spectra'%len(series))
for i in range(len(series)):
    print('*[%.3d] pixel i = '%(i+1), pixels_i[i], 'pixel j = ', pixels_j[i], 'spectral values = ',series[i])
collect 19 spectra
*[001] pixel i =  674 pixel j =  305 spectral values =  [ 785 1006 1342 1714 2060 2310 2461 2640 2727 2879 3517 3212]
*[002] pixel i =  627 pixel j =  505 spectral values =  [1242 1434 1734 2126 2451 2575 2710 2670 2798 2786 3617 3425]
*[003] pixel i =  500 pixel j =  708 spectral values =  [251 254 485 338 361 243 259 229 245 226 173 137]
*[004] pixel i =  313 pixel j =  557 spectral values =  [ 889  958 1250 1490 1843 2195 2388 2602 2730 2703 3006 2647]
*[005] pixel i =  225 pixel j =  248 spectral values =  [ 673 1002 1392 1862 2108 2338 2443 2452 2723 2761 3382 3112]
*[006] pixel i =  178 pixel j =  316 spectral values =  [ 848  974 1230 1630 1968 2119 2289 2331 2454 2458 3349 3211]
*[007] pixel i =  167 pixel j =  586 spectral values =  [ 736 1200 1484 1798 1956 2205 2484 2464 2612 2504 3095 2886]
*[008] pixel i =  160 pixel j =  729 spectral values =  [1073 1254 1562 2092 2309 2408 2495 2524 2617 2666 3553 3486]
*[009] pixel i =  321 pixel j =  890 spectral values =  [240 257 471 301 335 233 221 203 206 187 161 127]
*[010] pixel i =  671 pixel j =  755 spectral values =  [253 263 485 326 350 243 255 226 225 202 160 139]
*[011] pixel i =  812 pixel j =  586 spectral values =  [ 584  594  821  885 1650 1997 2183 1449 2125 2925 1725 1408]
*[012] pixel i =  791 pixel j =  287 spectral values =  [ 689  861 1108 1456 1673 1831 1974 2054 2219 2306 3296 3154]
*[013] pixel i =  565 pixel j =  233 spectral values =  [ 943 1758 1928 1978 2269 2483 2591 2386 2767 2844 3686 3523]
*[014] pixel i =  321 pixel j =  233 spectral values =  [ 885 1106 1406 1844 2148 2316 2465 2480 2702 2630 3678 3529]
*[015] pixel i =  170 pixel j =  329 spectral values =  [ 720  947 1278 1688 1868 2132 2331 2196 2424 2392 3164 2986]
*[016] pixel i =  154 pixel j =  500 spectral values =  [ 627  778 1122 1566 1774 1908 2015 2054 2190 2221 3057 2903]
*[017] pixel i =  222 pixel j =  687 spectral values =  [ 610  718  946 1068 1631 2289 2563 2532 2742 2743 2873 2230]
*[018] pixel i =  435 pixel j =  867 spectral values =  [242 250 492 330 350 233 238 216 212 200 154 121]
*[019] pixel i =  180 pixel j =  285 spectral values =  [ 657  935 1236 1606 1964 2186 2311 2422 2576 2729 3383 3250]
# 4) Get spectral values of some pixels by zooming the image

series, pixels_i,pixels_j = image_names.plot_spectra(zoom=((100,200),(200,670)))
#series, pixels_i,pixels_j = image_names.plot_spectra()
print('collect %d spectra'%len(series))
for i in range(len(series)):
    print('*[%.3d] pixel i = '%(i+1), pixels_i[i], 'pixel j = ', pixels_j[i], 'spectral values = ',series[i])
collect 6 spectra
*[001] pixel i =  152 pixel j =  308 spectral values =  [ 756  926 1228 1666 1979 2073 2181 2208 2308 2312 3191 3068]
*[002] pixel i =  157 pixel j =  422 spectral values =  [ 768 1042 1376 1786 2010 2134 2206 2224 2342 2498 3224 3059]
*[003] pixel i =  131 pixel j =  518 spectral values =  [ 725  946 1294 1738 2000 2082 2169 2136 2254 2277 3108 3062]
*[004] pixel i =  118 pixel j =  474 spectral values =  [ 741  902 1254 1730 2071 2138 2206 2148 2312 2251 3055 2976]
*[005] pixel i =  125 pixel j =  307 spectral values =  [ 809 1021 1362 1818 2069 2193 2359 2382 2504 2491 3454 3323]
*[006] pixel i =  138 pixel j =  258 spectral values =  [ 768  939 1292 1710 2025 2084 2219 2216 2345 2411 3271 3190]