MMzztx
MMzztx

Reputation: 233

How to add additional Tag for tiff file

I am trying to read and save a tiff file with some additional Tags, when I make a new image that works well but when I open an image then trying to write some meta tags back it is not working (The image can be written but it will keep the original tag without any change).

I attached my testing code, I am appreciated for any help!

from PIL import Image, TiffImagePlugin
def test_custom_metadata():

    img = Image.open('myimage.tif')

    info = TiffImagePlugin.ImageFileDirectory()
    CustomTagId = 37000

    info[CustomTagId] = 6
    info.tagtype[CustomTagId] = 3 # 'short' TYPE

    Image.DEBUG=True
    TiffImagePlugin.WRITE_LIBTIFF = False # Set to True to see it break.
    img.save('./temp2.tiff', tiffinfo = info)

test_custom_metadata()

Upvotes: 2

Views: 6132

Answers (2)

MMzztx
MMzztx

Reputation: 233

For someone interested that topic:

C/C++ method to add custom tag for tiff file

That will perfectly solve the problem

Upvotes: -2

Bernhard
Bernhard

Reputation: 8821

The following works for me with Pillow version 2.3:

from PIL import Image

image_1 = Image.open('input.tiff')
image_1.tag[37000] = 'my special tiff tag'
image_1.save('output.tiff', tiffinfo=image_1.tag)

image_2 = Image.open('output.tiff')
print image_2.tag[37000]

This prints my special tiff tag when running with an input.tiff in the current folder.

My understanding is that this only works when you don't use libtiff for writing the file. When using libtiff custom tags are ignored.

Upvotes: 5

Related Questions