import rastereasy

Crop image based on pixel coordinates

Two options for applying crop

  • Do not crop the image but returns a cropped image (function crop with default option )

  • Crop the image directly (function crop with option inplace=True)

Two options for cropping

  • With respect to pixel coordinates

  • With respect to lat/lon coordinates

  • In each case, you must indicate coordinates (pixel or latitude/longitude) of the north-west (top left) and south-east (bottom right) corner

1) Read and plot image

name_im='./data/demo/sentinel.tif'
image=rastereasy.Geoimage(name_im,history=True)
image.colorcomp(['4','3','2'],extent="pixel")
../_images/168356abd2bac6f9acdec5573874879b995d6be4cebcf345369a96cb8da58a1f.png
help(image.crop)
Help on method crop in module rastereasy.rastereasy:

crop(*args, area=None, dest_name=None, pixel=True, inplace=False) method of rastereasy.rastereasy.Geoimage instance
    Crop the image to a specified extent.
    
    This method extracts a rectangular subset of the image, defined either by pixel
    coordinates or by geographic coordinates, and updates the current image to contain
    only the cropped region.
    
    Parameters
    ----------
    area : tuple
        Area to crop
            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  `area = ((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)
            `area = ((lon1,lon2),(lat1,lat2))`
    
    inplace : bool, default False
        If False, return a copy. Otherwise, do cropping in place and return None.
    
    pixel : bool, optional
        Coordinate system flag:
        - If True: Coordinates are interpreted as pixel indices (row, col)
        - If False: Coordinates are interpreted as geographic coordinates (lon, lat)
        Default is True.
    
    Returns
    -------
    Geoimage
        A copy of the cropped image or None if `inplace=True`
    
    Examples
    --------
    >>> # Crop using pixel coordinates
    >>> original_shape = image.shape
    >>> image_crop = image.crop(area=((100, 500), (200, 600)))
    >>> print(f"Original shape: {original_shape}, New shape: {image_crop.shape}")
    >>>
    >>> # Crop using geographic coordinates
    >>> image_crop = image.crop(area=((-122.5, -122.3), (37.8, 37.7)), pixel=False)
    >>> image.visu()
    >>>
    >>> # Crop and save the result
    >>> image_crop = image.crop(area=((100, 500), (200, 600)), dest_name='cropped_area.tif')
    >>>
    >>>
    >>> # Crop using pixel coordinates
    >>> original_shape = image.shape
    >>> image.crop(area=((100, 500), (200, 600)), inplace=True) # inplace = True : modify directly the image
    >>> print(f"Original shape: {original_shape}, New shape: {image.shape}")
    >>>
    >>> # Crop using geographic coordinates
    >>> image.crop(area=((-122.5, -122.3), (37.8, 37.7)), pixel=False, inplace=True)
    >>> image.visu()
    >>>
    >>> # Crop and save the result
    >>> image.crop(area=((100, 500), (200, 600)), dest_name='cropped_area.tif', inplace=True)
    
    Notes
    -----
    - For consistency with older versions, a use with 4 parameters (deb_row_lon, end_row_lon, deb_col_lat, end_col_lat)
      instead of the `area` tuple is possible
    deb_row_lon : int or float
        Starting position (north):
        - If pixel=True: Starting row (y) coordinate
        - If pixel=False: Starting longitude coordinate
    
    end_row_lon : int or float
        Ending position (south):
        - If pixel=True: Ending row (y) coordinate
        - If pixel=False: Ending longitude coordinate
    
    deb_col_lat : int or float
        Starting position (west):
        - If pixel=True: Starting column (x) coordinate
        - If pixel=False: Starting latitude coordinate
    
    end_col_lat : int or float
        Ending position (east):
        - If pixel=True: Ending column (x) coordinate
        - If pixel=False: Ending latitude coordinate
    
    dest_name : str, optional
        Path to save the cropped image. If None, the image is not saved.
        Default is None.
    
    - The cropping operation changes the spatial extent of the image but preserves
    the resolution and projection.
    - When using pixel coordinates, the format is (row_start, row_end, col_start, col_end).
    - When using geographic coordinates, the format is (lon_start, lon_end, lat_start, lat_end).

2) Cropping by returning a new image

2) a) With respect to pixel coordinates

deb_row=50
deb_col=100
end_row = 200
end_col=300

print('Before crop')
image.info()
image.colorcomp([4,3,2],title='original image',extent="pixel")
image_crop = image.crop(area=((deb_row,end_row),(deb_col,end_col)))
print('After crop')
image_crop.info()
image_crop.colorcomp([4,3,2],title='cropped image',extent="pixel")
Before crop
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

--- History of modifications---
[2025-10-17 18:25:48] - Read image ./data/demo/sentinel.tif
../_images/5854df05fdce6708fc8818d4dfbef769f8b7851d7ae5078f9818f4a2b0849b55.png
After crop
- Size of the image:
   - Rows (height): 150
   - Cols (width): 200
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.07488251, 38.36338418)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

--- History of modifications---
[2025-10-17 18:25:49] - Created image from data array
[2025-10-17 18:25:49] - Created by cropping from pixel coordinates rows 50-200, cols 100-300
../_images/236d6a5b933421eea9a00cc7fd77fce3a5cc21c936232483ca3d1a0da4b664cf.png

2) b) With respect to lat/lon coordinates

Get information of lon / lat bos

print('latitude - longitude of top left corner', image.pixel2latlon(0,0))
print('latitude - longitude of bottom right corner', image.pixel2latlon(image.shape[0],image.shape[1]))
# Transform the coordinates pixels in lat,lon
deb_row=50
deb_col=100
end_row = 200
end_col=300
lat1,lon1=image.pixel2latlon(deb_row,deb_col)
lat2,lon2=image.pixel2latlon(end_row,end_col)
print('lat,lon corresponding to (%d,%d) : (%f,%f)'%(deb_row,deb_col,lat1,lon1))
print('lat,lon corresponding to (%d,%d) : (%f,%f)'%(end_row,end_col,lat2,lon2))
latitude - longitude of top left corner (7.086164175158828, 38.34525905140625)
latitude - longitude of bottom right corner (6.99582780410311, 38.4359177538106)
lat,lon corresponding to (50,100) : (7.081654,38.354320)
lat,lon corresponding to (200,300) : (7.068111,38.372448)
image.colorcomp([4,3,2],title='original image')
image_cropped=image.crop(area=((lon1,lon2),(lat1,lat2)),pixel=False)
print('After crop')
image_cropped.info()
image_cropped.colorcomp([4,3,2],title='cropped image')
../_images/5b3727e0c6be385cf01dca29f7ca1812b787d7e1c0397793f0302e865eb167df.png
After crop
- Size of the image:
   - Rows (height): 150
   - Cols (width): 200
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.07488251, 38.36338418)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

--- History of modifications---
[2025-10-17 18:25:49] - Created image from data array
[2025-10-17 18:25:49] - Created by cropping from geographic coordinates lon 38.35432021446677-38.37244815225719, lat 7.081654161856243-7.068110860101535
../_images/11dddc91adc56dddcbad19463ef6a215f0674373f63c79062fd54971c5676543.png

3) Cropping with inplace=True (modify the image directly)

help(image.crop)
Help on method crop in module rastereasy.rastereasy:

crop(*args, area=None, dest_name=None, pixel=True, inplace=False) method of rastereasy.rastereasy.Geoimage instance
    Crop the image to a specified extent.
    
    This method extracts a rectangular subset of the image, defined either by pixel
    coordinates or by geographic coordinates, and updates the current image to contain
    only the cropped region.
    
    Parameters
    ----------
    area : tuple
        Area to crop
            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  `area = ((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)
            `area = ((lon1,lon2),(lat1,lat2))`
    
    inplace : bool, default False
        If False, return a copy. Otherwise, do cropping in place and return None.
    
    pixel : bool, optional
        Coordinate system flag:
        - If True: Coordinates are interpreted as pixel indices (row, col)
        - If False: Coordinates are interpreted as geographic coordinates (lon, lat)
        Default is True.
    
    Returns
    -------
    Geoimage
        A copy of the cropped image or None if `inplace=True`
    
    Examples
    --------
    >>> # Crop using pixel coordinates
    >>> original_shape = image.shape
    >>> image_crop = image.crop(area=((100, 500), (200, 600)))
    >>> print(f"Original shape: {original_shape}, New shape: {image_crop.shape}")
    >>>
    >>> # Crop using geographic coordinates
    >>> image_crop = image.crop(area=((-122.5, -122.3), (37.8, 37.7)), pixel=False)
    >>> image.visu()
    >>>
    >>> # Crop and save the result
    >>> image_crop = image.crop(area=((100, 500), (200, 600)), dest_name='cropped_area.tif')
    >>>
    >>>
    >>> # Crop using pixel coordinates
    >>> original_shape = image.shape
    >>> image.crop(area=((100, 500), (200, 600)), inplace=True) # inplace = True : modify directly the image
    >>> print(f"Original shape: {original_shape}, New shape: {image.shape}")
    >>>
    >>> # Crop using geographic coordinates
    >>> image.crop(area=((-122.5, -122.3), (37.8, 37.7)), pixel=False, inplace=True)
    >>> image.visu()
    >>>
    >>> # Crop and save the result
    >>> image.crop(area=((100, 500), (200, 600)), dest_name='cropped_area.tif', inplace=True)
    
    Notes
    -----
    - For consistency with older versions, a use with 4 parameters (deb_row_lon, end_row_lon, deb_col_lat, end_col_lat)
      instead of the `area` tuple is possible
    deb_row_lon : int or float
        Starting position (north):
        - If pixel=True: Starting row (y) coordinate
        - If pixel=False: Starting longitude coordinate
    
    end_row_lon : int or float
        Ending position (south):
        - If pixel=True: Ending row (y) coordinate
        - If pixel=False: Ending longitude coordinate
    
    deb_col_lat : int or float
        Starting position (west):
        - If pixel=True: Starting column (x) coordinate
        - If pixel=False: Starting latitude coordinate
    
    end_col_lat : int or float
        Ending position (east):
        - If pixel=True: Ending column (x) coordinate
        - If pixel=False: Ending latitude coordinate
    
    dest_name : str, optional
        Path to save the cropped image. If None, the image is not saved.
        Default is None.
    
    - The cropping operation changes the spatial extent of the image but preserves
    the resolution and projection.
    - When using pixel coordinates, the format is (row_start, row_end, col_start, col_end).
    - When using geographic coordinates, the format is (lon_start, lon_end, lat_start, lat_end).

3) a) With respect to pixel coordinates

deb_row=50
deb_col=100
end_row = 200
end_col=300
#### 2) a) With respect to pixel coordinates

print('Before crop')
image.info()
image.colorcomp([4,3,2],title='original image',extent="pixel")
# dest_name : to save the image
image.crop(area=((deb_row,end_row),(deb_col,end_col)),dest_name='./data/results/crop/crop_pixel1.tif', inplace=True)
print('After crop')
image.info()
image.colorcomp([4,3,2],title='cropped image',extent="pixel")
Before crop
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

--- History of modifications---
[2025-10-17 18:25:48] - Read image ./data/demo/sentinel.tif
../_images/5854df05fdce6708fc8818d4dfbef769f8b7851d7ae5078f9818f4a2b0849b55.png
After crop
- Size of the image:
   - Rows (height): 150
   - Cols (width): 200
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.07488251, 38.36338418)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

--- History of modifications---
[2025-10-17 18:25:48] - Read image ./data/demo/sentinel.tif
[2025-10-17 18:25:49] - Cropped from shape (1000, 1000) to (150, 200) using pixel coordinates rows 50:200, cols 100:300
	 Cropped image saved to: ./data/results/crop/crop_pixel1.tif
../_images/236d6a5b933421eea9a00cc7fd77fce3a5cc21c936232483ca3d1a0da4b664cf.png

3) b) With respect to lat/lon coordinates

Note : we need to reload the image since we modified it before (with inplace=True)

image=rastereasy.Geoimage(name_im)
image.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}

Get information of lon / lat bos

print('latitude - longitude of top left corner', image.pixel2latlon(0,0))
print('latitude - longitude of bottom right corner', image.pixel2latlon(image.shape[0],image.shape[1]))
# Transform the coordinates pixels in lat,lon
deb_row=50
deb_col=100
end_row = 200
end_col=300
lat1,lon1=image.pixel2latlon(deb_row,deb_col)
lat2,lon2=image.pixel2latlon(end_row,end_col)
print('lat,lon corresponding to (%d,%d) : (%f,%f)'%(deb_row,deb_col,lat1,lon1))
print('lat,lon corresponding to (%d,%d) : (%f,%f)'%(end_row,end_col,lat2,lon2))
latitude - longitude of top left corner (7.086164175158828, 38.34525905140625)
latitude - longitude of bottom right corner (6.99582780410311, 38.4359177538106)
lat,lon corresponding to (50,100) : (7.081654,38.354320)
lat,lon corresponding to (200,300) : (7.068111,38.372448)
image.colorcomp([4,3,2],title='original image')
image.crop(area=((lon1,lon2),(lat1,lat2)),pixel=False, inplace=True)
print('After crop')
image.info()
image.colorcomp([4,3,2],title='cropped image')
../_images/5b3727e0c6be385cf01dca29f7ca1812b787d7e1c0397793f0302e865eb167df.png
After crop
- Size of the image:
   - Rows (height): 150
   - Cols (width): 200
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.07488251, 38.36338418)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '10': 10, '11': 11, '12': 12}
../_images/11dddc91adc56dddcbad19463ef6a215f0674373f63c79062fd54971c5676543.png