How to add a nodata mask or alpha channel to a source image that is read-only or should be treated as such?

For example I have this satellite image that uses jpeg compression in a geotiff. Since the source already uses a lossy compression we don't want to run it through any process that will resample the valid pixels again, thus losing more information. We also don't want to pay the storage penalty of converting to another file using lossless compression because a) we're duplicating the source, and b) the lossless-file will be several times larger than the source.

Yellow is current nodata (actually 0,0,0). Black jagged perimeter is what we want to turn into nodata.

The utility is a very convenient method of removing this unwanted collar in one step but we could also use a polygon mask or some other method. Unfortunately all of the methods I'm aware of create redundant copies of the data and/or resample our value pixels which we want to leave unchanged.

1 Answer 1


Use nearblack as originally thought, but with mask instead of alpha channel, and then discard the interim file. It can consume a lot of intermediate diskspace, but is storage efficient at the end.

  1. Ensure the source image will remain golden by marking read-only
  2. Create a working copy de-collared image with a mask file
  3. Rename the mask file to match original
  4. Remove working copy

Windows command line syntax:

attrib +r in_image.tif
nearblack -o xxx.tif -setmask in_image.tif
move xxx.tif.msk in_image.tif.msk
del xxx.*

If the source image is external, a read-only network share for example, use a virtual raster ():

gdalbuildvrt -o my_image.vrt \\server\share\in_image.tif
nearblack -o xxx.tif -setmask my_image.vrt
move xxx.tif.msk my_image.vrt.msk
del xxx.*

ArcMap, ArcGIS Pro and QGIS will now automatically discard the unwanted pixels and leave the original unchanged without any extra effort on your part:

cleaned image screenshot

Here's a script to recursively create masks for all TIF images in a directory tree. It's for the first scenario, where you can write to the folder where the images are stored:


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.