Using gdal_translate in the command line seems to do the trick, even though the Python binding doesn't work.
This worked to crop a GeoTiff removing 300 pixel padding from top and left and keeping the next 2000x2000 pixels.
gdal_translate -srcwin 300 300 2000 2000 input.tif output.tif
I've spent an embarrassingly long time trying to figure this out.
I have a series of satellite images in GEOTiff format. Each image has a 300 pixel buffer where it overlaps with the image next to it.
Goal: I am trying to crop off the 300 pixel buffer off of each image and then use as a raster with GDAL.
Constraints: 1) I need to keep all of the metadata and coordinate system information associated with the files 2) I want to do this entirely in Python (no command line)
What I've tried unsuccessfully:
1) using gdal.translate's srcWin
function:
raster_data = gdal.Open('image.tif')
x_size = raster_data.RasterXSize
y_size = raster_data.RasterYSize
raster_data_unpadded = gdal.Translate('temp.tif', raster_data,
srcWin = [padding,padding,
x_size - padding*2,
y_size-padding*2])
The problem: This produces a black image with no data
2) Cropping image using PIL
and then saving back as TIF
from PIL import Image
img = Image.open(image.tif)
x_size, y_size = img.size
box = (padding, padding, x_size-padding, y_size - padding)
img_unpadded = img.crop(box)
img_unpadded.save('unpadded_image.tif')
The problem: PIL fails to save the file. It just hangs. Trying to save as a ".tiff" file produces the error "encoder error -9"
3) Using Rasterio
with rasterio.open("image.tif") as src:
out_image, out_transform = mask(src, geoms, crop=True)
out_meta = src.meta.copy()
The problem: Rasterio doesn't seem to accept masks in Pixels format (e.g. 300 pixels). Only takes geometries, like a polygon, in the coordinate system of the file. I don't know how to translate between Pixels and coordinates.
Open the raster dataset that you wish to crop using xarray or rioxarray. Open your shapefile as a geopandas object. Crop the data using the . clip() function.
GDAL is a translator library for raster and vector geospatial data formats that is released under an MIT style Open Source License by the Open Source Geospatial Foundation.
The gdalwarp utility is an image mosaicing, reprojection and warping utility. The program can reproject to any supported projection, and can also apply GCPs stored with the image if the image is “raw” with control information.
The following should work:
import rasterio
from rasterio.windows import Window
with rasterio.open('input.tif') as src:
window = Window(padding, padding, src.width - 2 * padding, src.height - 2 * padding)
kwargs = src.meta.copy()
kwargs.update({
'height': window.height,
'width': window.width,
'transform': rasterio.windows.transform(window, src.transform)})
with rasterio.open('cropped.tif', 'w', **kwargs) as dst:
dst.write(src.read(window=window))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With