I'd like to edit the nodata values in several raster files, but big number of these file doesn't have any nodata value defined, there is nothing to edit. I'v tried the gdal_translate -a_nodata command but it'as not working for me. As I understand this command is only suitable for changing an already defined nodata value.

Any ideas?

  • 5
    -a_nodata assigns nodata even if it is not set earlier. If your rasters use some pixel value for nodata, like 0 0 0, but it is not marked as nodata into image metadata the -a_nodata should work. Please describe how it does not work for you.
    – user30184
    Apr 18, 2019 at 20:41
  • Virtual files stored on disk are kept in an XML format with the following elements. .... Any datasets copied/ translated from this will not have a nodata value. This is useful ... Apr 18, 2019 at 21:05
  • gdal.org/gdal_edit.html
    – user2856
    Apr 19, 2019 at 3:07

2 Answers 2


Python works pretty well for this:

from osgeo import gdal
from osgeo.gdalconst import GA_Update
filename = 'somefile.tif'
nodata = 0
# open the file for editing
ras = gdal.Open(filename, GA_Update)
# loop through the image bands
for i in range(1, ras.RasterCount + 1):
    # set the nodata value of the band
# unlink the file object and save the results
ras = None

if you have gdal in python (you can get the .whl file here if needed), you should be able to do the following:

import gdal
ds = gdal.Open(r"Path\To\Your\File.extension",1) # The 1 means that you are opening the file to edit it)
rb = ds.GetRasterBand(1) #assuming your raster has 1 band. 
rb.SetNoDataValue(YOUR VALUE HERE)
rb= None 
ds = None

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.