Skip to content

Instantly share code, notes, and snippets.

@openfirmware
Last active May 17, 2019 18:43
Show Gist options
  • Save openfirmware/81e334e85aa18ae3846ab7629c6f9e25 to your computer and use it in GitHub Desktop.
Save openfirmware/81e334e85aa18ae3846ab7629c6f9e25 to your computer and use it in GitHub Desktop.
How to add a mask to a GeoTIFF using Photoshop

HOW TO CREATE A TRANSPARENT MASK ON A GEOTIFF

This is useful for georeferenced images or GeoTIFF files that have black borders caused by re-projection or warping.

Georeference the image in QGIS, and export to a GeoTIFF (original.tif). Use Photoshop to open the GeoTIFF and select the area you want to mask; in the "Layers" palette switch to the "Channels" tab and select the Alpha channel (create one if it doesn't exist). Fill the masked area with "black" in the Alpha channel, as save as a new TIFF (export.tif). Don't bother with compression when exporting from Photoshop as we are about to convert the image again.

This TIFF won't have geodata, so we will use some command line tools from libgeotiff to fix that.

$ listgeo -no_norm "original.tif" > metadata.geo
$ geotifcp -g metadata.geo "export.tif" map.tif
$ gdal_translate map.tif map2.tif -b 1 -b 2 -b 3 -mask 4 --config GDAL_TIFF_INTERNAL_MASK YES

The map2.tif file should have the original geodata and a fourth color band for the alpha channel. That last GDAL command can be modified to add other GeoTIFF options such as compression.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment