如何为tiff fi添加附加标签

2024-09-30 05:32:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图读取并保存一个带有一些附加标记的tiff文件,当我创建一个新的图像,但当我打开一个图像,然后试图写回一些元标记时,它是不起作用的(图像可以被写入,但它将保持原标记不变,没有任何变化)。在

我附上了我的测试代码,感谢任何帮助!在

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

Tags: 文件to标记test图像imageinfotrue
2条回答

对于感兴趣的人:

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

那将完全解决问题

以下内容适用于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。在

我的理解是,只有在不使用libtiff编写文件时,这才有效。使用libtiff时,将忽略自定义标记。在

相关问题 更多 >

    热门问题