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

6
  • If this is your homework, did your teacher give you any hint about what to try?
    – user30184
    May 27, 2017 at 10:39
  • No it is not 😊 I'm working on a project
    – geoinfo
    May 27, 2017 at 23:57
  • Write that as an answer and tell which tools can be used for such editing.
    – user30184
    May 29, 2017 at 12:22
  • I updated the question, it is an uncomplete answer, the image lost it's projection infos.
    – geoinfo
    May 29, 2017 at 12:27
  • @user30184 do you have any idea doing this using gdal
    – geoinfo
    May 29, 2017 at 12:47

3 Answers 3

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

6
  • The result was a black image, each pixel contains a value, I guess this is similar to changing symbology.
    – geoinfo
    May 29, 2017 at 14:45
  • Very hard to say without having a sample of your image and what you await when you wrote "without losing associated data values". Sometimes palette has only a few entries in the beginning of the scale, sometimes the whole 0-255 range is utilized.
    – user30184
    May 29, 2017 at 14:52
  • I updated the post
    – geoinfo
    May 29, 2017 at 14:59
  • I meant to put a test image for downloading so that others could play with similar data that you have.
    – user30184
    May 29, 2017 at 20:06
  • already done, did you check the update?
    – geoinfo
    May 31, 2017 at 11:16
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.

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.

2
  • 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, 2017 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, 2017 at 13:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.