I'm using gdal lib for Python in a Jupyter Notebook environment.

I'd like to retrieve the resolution of a raster to use it in a gdal.warp call.

At the moment I do the following :

src = gdal.Open(mask)
ulx, xres, xskew, uly, yskew, yres  = src.GetGeoTransform()

but I get too much information. Is there a way to only retreive xres and yres?

  • 1
    because I really don't like to instantiate useless variables. I try to clarify and organize a working code ;-) Jul 17, 2020 at 9:35
  • 3
    Not adding as an answer as it's ugly, but you could use slice notation: xres, yres = src.GetGeoTransform()[1:6:4]
    – user2856
    Jul 17, 2020 at 10:06

3 Answers 3


If you only want to avoid the unused variables use this code:

import gdal
src = gdal.Open(mask)
_, xres, _, _, _, yres  = src.GetGeoTransform()
  • any drawbacks of that solution? possibilities that it won't work? this should be an answer. Apr 20, 2022 at 17:43
  • I does work using Python 3.8.10 and gdal 3.1.4.
    – Zoltan
    Apr 20, 2022 at 19:54

Google says no. But a "workaround" could be to use operator.itemgetter:

import gdal, operator
src = gdal.Open(mask)
xres, yres = operator.itemgetter(1,5)(src.GetGeoTransform())

This worked for me in gdal version 3.4.1:

src = gdal.Open(mask)
xres = src.RasterXSize
yres = src.RasterYSize

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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