Skip to content

Save custom TIFF tags in LZW-compressed TIFFs #660

Closed

Description

@jondo

If I understand correctly, the recent changes (#369) have not made it possible to write custom TIFF tags. I would like to do the following:

info = TiffImagePlugin.ImageFileDirectory()
CustomTagId = 37000
info[CustomTagId] = 6
info.tagtype[CustomTagId] = 3 # 'short' according to TiffTags.TYPES

img.save(pathname, tiffinfo = info)

This corresponds to "Defining Application Tags" in http://www.remotesensing.org/libtiff/addingtags.html.

Activity

  1. wiredfool commented on May 24, 2014

    @wiredfool
    Member

    Sorry I missed this earlier.

    I think this works. The following code writes your custom tag, and will read it back in with the correct value.

    def test_custom_metadata():
        img = lena()
        f = tempfile('temp.tiff')
    
        info = TiffImagePlugin.ImageFileDirectory()
        CustomTagId = 37000
    
        info[CustomTagId] = 6
        info.tagtype[CustomTagId] = 3 # 'short' according to TiffTags.TYPES
    
        Image.DEBUG=True
        img.save(f, tiffinfo = info)
    
    
        reloaded = Image.open(f)
    
        assert_equal(6, reloaded.tag.getscalar(CustomTagId))
    Tiffinfo Keys: <bound method ImageFileDirectory.keys of <PIL.TiffImagePlugin.ImageFileDirectory object at 0x7fd62ffb98d0>>
    Tag 256, Type: None, Value: (128,)
    save: ImageWidth (256) - type: short (3) - value: (128,)
    Tag 257, Type: None, Value: (128,)
    save: ImageLength (257) - type: short (3) - value: (128,)
    Tag 258, Type: None, Value: (8, 8, 8)
    save: BitsPerSample (258) - type: short (3) - value: (8, 8, 8)
    Tag 259, Type: None, Value: (1,)
    save: Compression (259) - type: short (3) - value: (1,)
    Tag 262, Type: None, Value: (2,)
    save: PhotometricInterpretation (262) - type: short (3) - value: (2,)
    Tag 273, Type: None, Value: (0,)
    save: StripOffsets (273) - type: long (4) - value: (0,)
    Tag 277, Type: None, Value: (3,)
    save: SamplesPerPixel (277) - type: short (3) - value: (3,)
    Tag 278, Type: None, Value: (128,)
    save: RowsPerStrip (278) - type: short (3) - value: (128,)
    Tag 279, Type: None, Value: (49152,)
    save: StripByteCounts (279) - type: short (3) - value: (49152,)
    Tag 284, Type: None, Value: (1,)
    save: PlanarConfiguration (284) - type: short (3) - value: (1,)
    Tag 37000, Type: 3, Value: (6,)
    save: unknown (37000) - type: short (3) - value: (6,)
    *** TiffImageFile._open ***
    - __first: 8
    - ifh:  b'II*\x00\x08\x00\x00\x00'
    tag: ImageWidth (256) - type: short (3) - value: (128,)
    tag: ImageLength (257) - type: short (3) - value: (128,)
    tag: BitsPerSample (258) - type: short (3) - value: (8, 8, 8)
    tag: Compression (259) - type: short (3) - value: (1,)
    tag: PhotometricInterpretation (262) - type: short (3) - value: (2,)
    tag: StripOffsets (273) - type: long (4) - value: (152,)
    tag: SamplesPerPixel (277) - type: short (3) - value: (3,)
    tag: RowsPerStrip (278) - type: short (3) - value: (128,)
    tag: StripByteCounts (279) - type: short (3) - value: (49152,)
    tag: PlanarConfiguration (284) - type: short (3) - value: (1,)
    tag: unknown (37000) - type: short (3) - value: (6,)
    *** Summary ***
    - compression: raw
    - photometric_interpretation: 2
    - planar_configuration: 1
    - fill_order: 1
    - size: (128, 128)
    format key: (b'II', 2, 1, 1, (8, 8, 8), ())
    - raw mode: RGB
    - pil mode: RGB
    tiles:  [('raw', (0, 0, 128, 128), 152, ('RGB', 0, 1))]
    ok
    
  2. jondo commented on May 26, 2014

    @jondo
    Author

    Unfortunately, I observe that this only works with TiffImagePlugin.WRITE_LIBTIFF = False, so I cannot use it for LZW-compressed TIFFs, see #659.

    (I have tested the standalone version:

    # Tested with WinPython-32bit-3.3.5.0
    
    from PIL import Image, TiffImagePlugin
    from nose.tools import assert_equal
    
    def test_custom_metadata():
        img = Image.new('L', (300, 300))
        img.putpixel((150,150), 255)
    
        info = TiffImagePlugin.ImageFileDirectory()
        CustomTagId = 37000
    
        info[CustomTagId] = 6
        info.tagtype[CustomTagId] = 3 # 'short' according to TiffTags.TYPES
    
        Image.DEBUG=True
        TiffImagePlugin.WRITE_LIBTIFF = False # Set to True to see it break.
        img.save('temp.tiff', tiffinfo = info)
    
    
        reloaded = Image.open('temp.tiff')
    
        assert_equal(6, reloaded.tag.getscalar(CustomTagId))
    
    
    test_custom_metadata()

    )

  3. wiredfool commented on May 26, 2014

    @wiredfool
    Member

    I don't remember anything special for custom tags when I did the work on libtiff, so I'm pretty sure that they're not in there yet.

  4. added this to the Future milestone on Jun 7, 2014
  5. changed the title [-]Save custom TIFF tags[/-] [+]Save custom TIFF tags in LZW-compressed TIFFs[/+] on Feb 23, 2015
  6. radarhere commented on Aug 25, 2018

    @radarhere
    Member

    The custom tag is skipped here - https://github.com/python-pillow/Pillow/blob/f115546b56a54b6458f189/src/PIL/TiffImagePlugin.py#L1512

    So this looks like it is part of the 'UNDONE -- add code for the custom dictionary'

  7. radarhere commented on Oct 16, 2018

    @radarhere
    Member

    I've created PR #3350 for this

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugEnhancementTIFF

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions