1

I have a set of GeoTIFF having photometric = palette, I need to convert it to a greyscale image without losing associated data values. I need to do using Python or ArcGIS.

Changing the color could be done by setting the tiff tag to 1 (Luminance mode) using Python module PIL (Pillow):

from PIL import Image
im = Image.open("test_in.tif")
im.tag[262] = 1
im.save("test_out.tif")

The problem is that the generated file does not have any spatial ref., what should I do ?

Sample : image sample

geoinfo
  • 133
  • 1
  • 8

3 Answers3

1

Here comes a workaround with GDAL.

Create a virtual raster file with gdalbuildvrt http://www.gdal.org/gdalbuildvrt.html

gdalbuildvrt palettetest.vrt palette.tif

Open the palettetest.vrt file with text editor

Change

<ColorInterp>Palette</ColorInterp>

into

<ColorInterp>Gray</ColorInterp>

and remove the whole ColorTable section

<ColorTable>
      <Entry c1="120" c2="120" c3="120" c4="255" />
      <Entry c1="72" c2="72" c3="72" c4="255" />
     ...
 </ColorTable>

Now you have a virtual grayscale image that you can use with GDAL and QGIS as is.

gdalinfo palettetest.vrt
Driver: VRT/Virtual Raster
Files: palettetest.vrt
       palette.tif
Size is 1728, 1977
Coordinate System is `'
...
Band 1 Block=128x128 Type=Byte, ColorInterp=Gray

You can materialize it with gdal_translate

gdal_translate -of GTiff palettetest.tif grayscale.tif

The result in QGIS with min-max stretch between pixel values 2 and 82.

enter image description here

user30184
  • 65,331
  • 4
  • 65
  • 118
1

You need to pass along the TAGs as "tiffinfo" when saving:

from PIL import Image
im = Image.open("test_in.tif")
im.tag[262] = 1
im.save("test_out.tif", tiffinfo=im.tag)

This way, you shouldn't loose any of the meta-data. Also, the updated EXIF-TAGs, in your case TAG "262", should appear in your output file.

If you want to compare input and output files as for the TIFF-Tags, just use the following command lines:

exiftool -D -G -a -u -U -f -s "test_in.tif"
exiftool -D -G -a -u -U -f -s "test_out.tif"

exiftool is a cross-platform command line tool. I'm using it on my Linux Lubuntu 18.04 OS. In my opinion, it displays the TIFF-Tags in the most complete way, since python libraries like

  • exifread
  • PIL
  • tifffile
  • skimage.external.tifffile

seem to be blind on the eye of GeoTIFF-Tags, as they only display EXIF-Tags.

Andreas L.
  • 191
  • 1
  • 8
0

You are losing geo information such as spatial reference because you are using a library that only supports TIFF, not GeoTIFF (there's a difference).

See https://gis.stackexchange.com/a/16840/29005 for more info.

Senshi
  • 1,767
  • 1
  • 9
  • 22
  • I understand, Do you have any idea to change the color of the GeoTIFFs using gdal, because i've already googled this but in vain. – geoinfo May 29 '17 at 12:45
  • Changing the tiff mode to luminance is not "changing color", it just tells image viewers to treat the tiff as a greyscale. Look into gdal's setMetaData/setMetaDataItem and getMetadata/getMetaDataItem functions for this kind of change. – Senshi May 29 '17 at 13:19