从.TIF图像元数据使用PIL TiffTags提取比例尺

问题描述 投票:0回答:2

我是新来的Python,而我希望能提取电子显微镜(TIF)图像尺度信息。

当我打开在记事本文件并滚动至底部,我看到一个标题为“[扫描]”,并且它“PixelWidth = 3.10059e-010”下面的项目。

我想读在Python此值,并把它作为一个校准因子的图像内测量的物理距离。

我发现使用PIL(https://stackoverflow.com/a/46910779/10244370)有前途的方法,但运行的推荐代码时遇到错误。

from PIL import Image
from PIL.TiffTags import TAGS

with Image.open(imagetoanalyze) as img:
    meta_dict = {TAGS[key] : img.tag[key] for key in img.tag.iterkeys()}

我预计,创建一个对象“meta_dict”包含像“PixelWidth”字符串和彩车像“3.10059e-010”。

相反,我看到:

Traceback (most recent call last):

  File "<ipython-input-62-4ea0187b2b49>", line 2, in <module>
    meta_dict = {TAGS[key] : img.tag[key] for key in img.tag.iterkeys()}

  File "<ipython-input-62-4ea0187b2b49>", line 2, in <dictcomp>
    meta_dict = {TAGS[key] : img.tag[key] for key in img.tag.iterkeys()}

KeyError: 34682

显然,我做错了什么。任何帮助将不胜感激。谢谢!

python metadata python-imaging-library tiff
2个回答
1
投票

它看起来像你的文件可能是一个FEI SEM TIFF,其中包含INI像TIFF标签34682元。

尝试使用tifffile

import tifffile
with tifffile.TiffFile('FEI_SEM.tif') as tif:
    print(tif.fei_metadata['Scan']['PixelWidth'])

0
投票

使用PIL,我认为这将是更清晰的使用for循环设置你的字典,然后打印所需的结果。

from PIL import Image
from PIL.TiffTags import TAGS


with Image.open(imagetoanalyze) as img:
    meta_dict = {}
    for key in img.tag:      # don't really need iterkeys in this context
        meta_dict[TAGS.get(key,'missing')] = img.tag[key]

# Now you can print your desired unit:

print meta_dict["PixelWidth"]

如果你只需要一个值,你也可以查找与此PixelWidth标签数量:

for k in img.tag:
     print k,TAGS.get(k,'missing')

然后只需打印img.tag[<thatnumber>]不填充字典。

© www.soinside.com 2019 - 2024. All rights reserved.