EDIT: PARTIAL SOLUTION
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
ORIGINAL QUESTION
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.