【问题标题】:Saving Tiff with specific Tiff Tags using PIL使用 PIL 保存带有特定 Tiff 标签的 Tiff
【发布时间】:2020-12-15 15:14:02
【问题描述】:

我正在尝试使用带有自定义标签的 PIL 保存 TIFF 图像。

import numpy as np
import PIL
numrows=10
numcols=10
data = np.random.randint(0, 255, (numrows,numcols)).astype(np.uint8)

rawtiff=PIL.Image.fromarray(data)

custtifftags={'Photometric':1, 'Compression':1, 'BitsPerSample':32,\
              'SamplePerPixel':1, 'SampleFormat':3,'ImageLength':10,\
              'ImageWidth':10, 'PlanarConfiguration':1, 'ResolutionUnit':2}

rawtiff.save('test.tiff', tiffinfo=custtifftags)

我收到以下错误:

TypeError: '<' not supported between instances of 'str' and 'int'

是什么导致了这个错误?如何在设置我自己的标签时使用 PIL 保存图像?

【问题讨论】:

    标签: python python-3.x image python-imaging-library tiff


    【解决方案1】:

    TIFF 不按名称定义标签,仅按数值定义。 为了方便起见,字母名称。 在 TiffTags 中,您可以找到 PIL 中使用的字母值。

    作为解决方案,您可以尝试:

    from PIL.TiffImagePlugin import ImageFileDirectory_v2
    custtifftags = ImageFileDirectory_v2()
    custtifftags[101] = 'Test Data 101'
    

    【讨论】:

      【解决方案2】:

      我想通了。您需要按如下方式格式化 TIFF 标签:

      custtifftags={262:(1,), 259:(0,), 258:(32,),\
                    277:(1,), 339:(3,),257:(10,),\
                    256:(10,), 284:(1,), 296:(2,)}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-22
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        相关资源
        最近更新 更多