Resize image using another image

With the gdalwarp command it is possible to resize an image. Combining with python, to get the size of an existing image, it is possible to create a script to resize and reproject one image to another image.

1. Import GDAL


import os

import osgeo.gdal as gdal

2. Get bounding box and projection from input image

# Get information from image
dataset = gdal.Open(inBaseFile, gdal.GA_ReadOnly )
projection = dataset.GetProjection()
geotransform = dataset.GetGeoTransform()
xSize = dataset.RasterXSize
ySize = dataset.RasterYSize

# Get bounding box
minX = geotransform[0]
maxY = geotransform[3]
pixSizeX = geotransform[1]
pixSizeY = geotransform[5]
maxX = minX + (xSize * pixSizeX)
minY = maxY + (ySize * pixSizeY)

3. Create gdalwarp command from information.

bbStr = ' -te %s %s %s %s '%(minX, minY, maxX, maxY)
resStr = ' -tr %s %s '%(pixSizeX, pixSizeY)
projectionStr = ''' -t_srs '%s' ''' %(projection)
warpOptions = ' -of KEA ' + bbStr + resStr + projectionStr
warpCMD = 'gdalwarp ' + warpOptions + inLargeImage + ' ' + \
    outSubsetImage
os.system(warpCMD)

A full script is available in the RSGIS_Scripts repository, and can be run using:

python subsetImage2Image.py baseImage inImage outImage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s