Reclassifying raster using GDAL?

gdal_reclassify is an unofficial Python tool, based on Python GDAL bindings, able to reclassify according to several classes of values.

Requirements:

python
numpy
gdal binaries
python-gdal bindings

Example:

python gdal_reclassify.py source_dataset.tif destination_dataset.tif -c "<30, <50, <80, ==130, <210" -r "1, 2, 3, 4, 5" -d 0 -n true -p "COMPRESS=LZW"

gdal_calc can be used for a reclassification of many classes.

For example, you can change values below (and equal) 12 to 10, values of 20, 30, 40, 50 stays the same, and values between above 50 and 62 are changed to 60:

  python gdal_calc.py -A input.tif --outfile=output.file --calc="10*(A<=12)+20*(A==20)+30*(A==30)+40*(A==40)+50*(A==50)+60*((A>50)*(A<=62))" --NoDataValue=0

If you're working in a python script then use the .ReadAsArray method. You can then reclassify using numpy.

import numpy as np
sample = np.random.randint(low = 0, high = 9, size =(5,5))
print(sample)
sample[sample == 4] = 40
sample[sample <= 2] = -20
print(sample)