如何为 tiff 文件添加附加标签 [英] How to add additional Tag for tiff file

查看:186
本文介绍了如何为 tiff 文件添加附加标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取并保存带有一些附加标签的 tiff 文件,当我制作一个运行良好的新图像但当我打开一个图像然后尝试写回一些元标签时它不起作用(图像可以是写,但它会保持原来的标签不变).

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()

推荐答案

以下适用于 Pillow 2.3 版:

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]

当在当前文件夹中使用 input.tiff 运行时,这会打印 my special tiff tag.

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

我的理解是,这仅在您不使用 libtiff 写入文件时才有效.使用 libtiff 时会忽略自定义标签.

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

这篇关于如何为 tiff 文件添加附加标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆