Re: Change the Nodata value in a file.

Sean Gillies


I'm sorry this isn't more obviously documented. You can change the nodata value of an existing dataset by opening it in r+ mode and setting the attribute.

with"example.tif", "r+") as dataset:
    dataset.nodata = 255

Same goes for the crs and transform attributes. Hope this helps!

On Wed, Dec 19, 2018 at 6:58 AM <christina.ratcliff@...> wrote:
Hi Everyone,

Using RasterIO, what is the correct way to change (or assign if it isn't already set) the no data value for a single or multi band TIF file?

For example using the test rgb.byte.tif file, how do I change the nodata value from 0 to 255?  So far I have tried:
with'RGB.byte.tif') as src:  
bands =
bands.fill_value = 255
# or
# bands.set_fill_value(255)

kwargs = src.meta.copy()
kwargs.update({'nodata': 255})

with'copy.RGB.byte.tif', 'w', **kwargs) as dst:
Currently this only changes tif header/metadata information and not the actual data.

Options I've seen to accomplish this include
# 1. masked arrays
bands =, 255)

# 2. loop through bands and change value
for i in range(1, src.count + 1):
band =
band = np.where(band==0,255,band)

# 3. numpy indexing
bands =
bands[bands==0] = 255
Or should I be using rasterIO's read_mask(), write_mask() or dataset_mask()?

What is the preferred method? I haven't been able to find this in any of the user documentation.


Sean Gillies

Join to automatically receive all group messages.