Here's a quick script I put together to correct the cell size of a folder of GeoTiff rasters:
import os, sys
from osgeo import gdal
from osgeo import gdalconst
WorkingFolder = sys.argv[1] # first command line argument
# change to a hard path like r'c:\your\path' or 'c:\\your\\path'
# without the r if it suits your purposes..
for f in os.listdir(WorkingFolder): # loop through for each file in the folder
fName, fExt = os.path.splitext(f) # break up file name and extension
if fExt.upper() == '.TIF':
# this is a tif file, open it and get the existing geotransform
ds = gdal.OpenShared(os.path.join(WorkingFolder,f),gdalconst.GA_Update) # open the raster using FULL path
gt = ds.GetGeoTransform()
# gt is a tuple :
# adfGeoTransform[0] /* top left x */
# adfGeoTransform[1] /* w-e pixel resolution */
# adfGeoTransform[2] /* 0 */
# adfGeoTransform[3] /* top left y */
# adfGeoTransform[4] /* 0 */
# adfGeoTransform[5] /* n-s pixel resolution (negative value) */
# from http://www.gdal.org/gdal_tutorial.html
# you only want to change 1 and 5 but tuples are read-only so
# I convert to a list:
gtLst = list(gt)
# change the pixel size here
# 'y' pixel size is usually negative
gtLst[1] = 4.6
gtLst[5] = -4.6
# apply the changes
ds.SetGeoTransform(gtLst)
ds = None # close the dataset and commit the changes
It's mostly comments but shows the key points. The updated cell size is hard coded, perhaps it should be a variable, but will work in this case.
Be careful of GeoTIFF with world files - this will only update the TIFF and not the TFW file (unless the TIF is not a GeoTIFF) which will lead to contradictions in georeference, depending on what other packages do with this situation you could still end up with the old cell size. It's best to remove any TFW files that exist (move to another folder just in case the georeference isn't embedded).
gdal_translate
notgdal translate
. Also, as you may already be aware, this would involve a pixel interpolation, so you should explicitly choose one using the-r
flag and an appropriate option.