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.Geoimage(name_im,names=names)
2) Plot spectra by clicking on pixels
help(image_names.plot_spectra)
Help on method plot_spectra in module rastereasy.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.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()
#image_names_crop.colorcomp(extent='pixel')
# -> This will plot an image where you can click to visualize spectre
nootebook
([], [], [])
Plot spectra with selected bands for color composition
image=rastereasy.Geoimage(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
nootebook
([], [], [])
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')
nootebook
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 12 spectra
*[001] pixel i =  483 pixel j =  502 spectral values =  [ 282  385  700  583 1121 1787 1731 1807 1896 2054  996  627]
*[002] pixel i =  473 pixel j =  606 spectral values =  [236 283 482 342 357 216 232 221 220 197 206 170]
*[003] pixel i =  252 pixel j =  494 spectral values =  [ 821  804 1030 1438 2061 2304 2489 2344 2686 2690 3378 3015]
*[004] pixel i =  392 pixel j =  354 spectral values =  [ 923 1160 1500 2040 2204 2348 2492 2432 2655 2628 3319 3122]
*[005] pixel i =  564 pixel j =  417 spectral values =  [1109 1114 1378 1768 2259 2404 2536 2452 2709 2697 3487 3318]
*[006] pixel i =  551 pixel j =  684 spectral values =  [248 260 490 334 355 245 258 231 238 221 173 132]
*[007] pixel i =  442 pixel j =  718 spectral values =  [244 282 497 343 347 242 237 224 228 215 175 141]
*[008] pixel i =  366 pixel j =  746 spectral values =  [221 259 447 321 342 217 224 216 213 190 181 147]
*[009] pixel i =  211 pixel j =  489 spectral values =  [ 912 1128 1464 1946 2122 2310 2444 2508 2629 2638 3502 3382]
*[010] pixel i =  369 pixel j =  375 spectral values =  [ 956 1214 1538 2050 2357 2437 2534 2546 2707 2673 3574 3488]
*[011] pixel i =  577 pixel j =  531 spectral values =  [ 230  354  626  526  764  829  873 1037  914  592  488  368]
*[012] pixel i =  621 pixel j =  533 spectral values =  [ 789 1020 1354 1272 1839 3527 4040 4133 4102 3477 2376 1866]
# 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()
nootebook
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 7 spectra
*[001] pixel i =  157 pixel j =  310 spectral values =  [ 746  946 1262 1704 1914 2035 2154 2133 2281 2295 3125 3068]
*[002] pixel i =  150 pixel j =  367 spectral values =  [ 689  960 1288 1688 1933 2136 2296 2257 2520 2470 3227 3027]
*[003] pixel i =  154 pixel j =  415 spectral values =  [ 709  773 1084 1372 1825 2070 2252 2416 2514 2483 3315 3052]
*[004] pixel i =  154 pixel j =  455 spectral values =  [ 708  800 1094 1482 1918 2183 2359 2462 2559 2442 3137 2882]
*[005] pixel i =  133 pixel j =  496 spectral values =  [ 617  730 1028 1466 1752 1948 2124 2024 2292 2478 3082 2898]
*[006] pixel i =  152 pixel j =  534 spectral values =  [ 816 1152 1534 2014 2247 2384 2452 2454 2551 2474 3458 3406]
*[007] pixel i =  164 pixel j =  526 spectral values =  [ 859  855 1174 1588 1910 2196 2372 2396 2615 2661 3345 3149]