With PyQGIS
ras = QgsRasterLayer("raster.tif")
pixelSizeX= ras.rasterUnitsPerPixelX()
pixelSizeY = ras.rasterUnitsPerPixelY()
print pixelSizeX
2.11668210081
print pixelSizeY
2.11685012701
With GDAL
from osgeo import gdal
raster = gdal.Open('raster.tif')
gt =raster.GetGeoTransform()
print gt
(258012.37107330866, 2.11668210080698, 0.0, 163176.6385398821, 0.0, -2.1168501270110074)
pixelSizeX = gt[1]
pixelSizeY =-gt[5]
print pixelSizeX
2.11668210080698
print pixelSizeY
2.1168501270110074
With Rasterio
import rasterio
raster = rasterio.open('raster.tif')
gt = raster.affine
print gt
Affine(2.11668210080698, 0.0, 258012.37107330866,
0.0, -2.1168501270110074, 163176.6385398821)
pixelSizeX = gt[0]
pixelSizeY =-gt[4]
print pixelSizeX
2.11668210080698
print pixelSizeY
2.1168501270110074
Or (from sgillies)
pixelSizeX, pixelSizeY = raster.res
print pixelSizeX
2.11668210081
print pixelSizeY
2.11685012701
pixelSizeX,pixelSizeY?Because now I use some algorithms from GDAL and some from RASTERIO in the some function and my app is slowly I thing. – Chris Papas Jun 12 '17 at 17:25pxsz, pysz = raster.reswith Rasterio. – sgillies Jun 12 '17 at 20:06gdal.Open('raster.tif')(with 'O') – shahryar Mar 01 '18 at 00:32DatasetReader object has no attribute affine– user32882 Sep 27 '18 at 09:25gt = raster.affineshould begt = raster.transform– Aaron Feb 05 '19 at 05:03raster.affineInstead, we need to usegt = raster.transform– beahacker Aug 31 '20 at 15:52