tiffgeotiff

Changing TIFF image compression type while preserving all tags


Is there a command line tool that will change the type of compression used in a TIFF image without discarding unknown tags? Imagemagick 7.1.1-36 discards GeoTIFF tags when I do the following, despite this post implying otherwise.

% magick USGS_13_n35w119_20240207.tif -compress Zip USGS_13_n35w119_20240207zip.tif

Here’s a sample image. I’m trying to do this on macOS.


Solution

  • TL,DR

    Compress with ImageMagick and copy forward tags from original file with exiftool:

    # Compress with ImageMagick
    magick INPUT.TIF -compress LZW result.tif
    # Copy forward tags from original
    exiftool -TagsFromFile INPUT.TIF result.tif
    

    Or, you can make a one-liner:

    magick INPUT.TIF -compress lzw TIFF:- | exiftool -TagsFromFile INPUT.TIF - > result.tif
    

    Original Answer

    I downloaded a file with GeoTIFF tags from here. I then LZW compressed it with ImageMagick:

    magick SP27GTIF.TIF -compress LZW new.tif
    
    magick: Unknown field with tag 33550 (0x830e) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    magick: Unknown field with tag 33922 (0x8482) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    magick: Unknown field with tag 34735 (0x87af) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    magick: Unknown field with tag 34737 (0x87b1) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    

    Then I checked the file new.tif and the tags were missing. I put the tags back by copying forward from the original file with with exiftoool:

    exiftool -TagsFromFile SP27GTIF.TIF new.tif
    

    And, on checking again, the original GeoTIFF tags (from SP27GTIF.TIF) were present in the LZW-compressed image new.tif .

    identify -verbose new.tif | grep -Ei "tag|LZW"
    
      Format: TIFF (Tagged Image File Format)
    identify: Unknown field with tag 33550 (0x830e) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    identify: Unknown field with tag 33922 (0x8482) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    identify: Unknown field with tag 34735 (0x87af) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
    identify: Unknown field with tag 34737 (0x87b1) encountered. `TIFFReadDirectory' @ warning/tiff.c/TIFFWarnings/945.
      Compression: LZW
    

    Checking with tiffinfo gives, among other things:

      Tag 33550: 32.800000,32.800000,0.000000
      Tag 33922: 0.000000,0.000000,0.000000,681480.000000,1913050.000000,0.000000
      Tag 34735: 1,0,2,5,1024,0,1,1,1025,0,1,1,1026,34737,24,0,2049,34737,23,24,3072,0,1,26771
      Tag 34737: State Plane NAD27, Feet|NAD27 in Feet default"|
      Predictor: horizontal differencing 2 (0x2)