← All tutorials

Geospatial · Practical guide

Read & write GeoTIFF

Move between georeferenced raster files and NumPy arrays while keeping the spatial metadata intact.

PythonGDALNumPy
Start learning
What you’ll learn
  • Read raster values and spatial metadata.
  • Write multiband arrays back to a GeoTIFF.
  • Check array shape, coordinate system, and geotransform.
Before you begin Python basics
  • A Python environment with NumPy and GDAL.
  • A local GeoTIFF for the usage example.
  • Arrays use rows × columns × bands; a single band needs a band axis.
01

Read & write helpers

The reader returns the array, geotransform, projection, and raster dimensions. The writer expects a three-dimensional array and writes Float32 bands.

Array shape
Multiband reads are rearranged from bands × rows × columns to rows × columns × bands.
Spatial metadata
Pass the original geotransform and projection to the writer to retain the raster location.
02

Use the helpers

Save the helpers above as read_write_geotiff.py, place a raster in your data folder, and replace the input and output filenames.

geotiff_usage.pyPython
↓
from read_write_geotiff import read_tif, array_to_geotiff

array, transform, projection, rows, cols = read_tif("data/imagery.tif")
if array.ndim == 2:
    array = array[..., None]
array_to_geotiff(array, "data/imagery_copy.tif", transform, projection)
print(rows, cols, array.shape)
03

Check the output

After writing, inspect the output alongside the input. A matching array shape alone does not confirm that the coordinates were retained.

  • Confirm rows, columns, and band count.
  • Compare the projection and six geotransform values.
  • Check data type and NoData behavior for your analysis.
  • Reopen the output and compare raster values.

Keep exploring

GDAL raster API
Download all examples