1

I have an image that I'm trying to resize to a specific set of bounds.

I'm trying to use gdal to do this, but my problem is that gdal just adds a border of padded pixels to the image rather than actually resizing it.

Here is the image before (top) and after (bottom) resizing showing the boundary that I'm resizing to in red. The black pixels are nodata values added by GDAL; technically the image has been resized to the right proportions, but GDAL hasn't resampled any pixel values which is what I'm looking for.

Before resizing

After resizing

These are the GDAL commands I've tried but neither output what I'm looking for:

gdalwarp -te 330585.0 8645385.0 604215.0 8918715.0 -tr 10 10 -overwrite "input.tif" "output.tif"

gdalwarp  -te 330585.0 8645385.0 604215.0 8918715.0 -ts 9111 9121 -overwrite "input.tif" "output.tif"

An alternative I have is to resize the image as an array using scikit-image like this:

from skimage.transform import resize

output = resize(input, (9111, 9121), preserve_range=True)

I'd rather avoid this solution because I'm trying to avoid reading the whole image into memory at once.

1
  • You can use gdal_translate gdal.org/programs/gdal_translate.html with -ullr option and give desired upper-left and lower-right coordinates. Another option is to add some ground control points. With those you can even stretch and rotate the image. In both cases you must know what you are doing.
    – user30184
    Jun 30 '21 at 18:37
1

I managed to fix this issue in two steps:

  1. Updating the transform of the image. Setting the origin of the transform at the corner of the bounds and calculating a new pixel width & height to make the image fill the full bounds extent.

  2. Running the following gdalwarp command to produce the desired extent and resolution:

gdalwarp -r cubic -overwrite -tr 10 10 -te 330585.0 8645385.0 604215.0 8918715.0  image.tif output.tif

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.