Pillow无法提取所有exif信息

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

我正在尝试使用 Pillow 获取照片的 exif 数据,但看起来它没有返回所有应该可用的数据。使用简单的代码:

from PIL import Image
from PIL.ExifTags import TAGS

def get_exif():
  i = Image.open('IMG_0780.JPG')
  info = i.getexif()
  return {TAGS.get(tag): value for tag, value in info.items()}
print(get_exif())

会回来

{'ResolutionUnit': 2, 'ExifOffset': 192, 'Make': 'Apple', 'Model': 'iPhone SE', 'Software': '11.3', 'Orientation': 1, 'DateTime': '2018:04:29 20:32:21', 'YCbCrPositioning': 1, 'XResolution': 72.0, 'YResolution': 72.0}

但是当我用 gthumb 打开同一张图像时,它显示了更多内容:

就我而言,我对 OriginalDateTime 特别感兴趣,但是还有很多其他数据我无法通过 Pillow 获得

Pillow 9.3.0
Python 3.10.6 (main, Aug 10 2022, 11:40:04) [GCC 11.3.0]
python python-imaging-library exif
1个回答
0
投票

更多数据嵌套在称为 IFD 的部分下,其中之一是“Exif SubIFD”,由您在输出中看到的

ExifOffset
指向。要获取该项目下的项目:

from PIL import ExifTags
img_exif = image.getexif()
for ifd_key, ifd_value in img_exif.get_ifd(ExifTags.Base.ExifOffset).items():
    ifd_tag_name = ExifTags.TAGS.get(ifd_key, ifd_key)
    print(f" {ifd_tag_name}: {ifd_value}")

要循环所有 IFD 中的所有标签,您可以这样做:

from PIL import ExifTags
IFD_CODE_LOOKUP = {i.value: i.name for i in ExifTags.IFD}
for tag_code, value in img_exif.items():
    # if the tag is an IFD block, nest into it
    if tag_code in IFD_CODE_LOOKUP:
        ifd_tag_name = IFD_CODE_LOOKUP[tag_code]
        print(f"IFD '{ifd_tag_name}' (code {tag_code}):")
        ifd_data = img_exif.get_ifd(tag_code).items()
        for nested_key, nested_value in ifd_data:
            nested_tag_name = ExifTags.GPSTAGS.get(nested_key, None) or ExifTags.TAGS.get(nested_key, None) or nested_key
            print(f"  {nested_tag_name}: {nested_value}")
    else:
        # root-level tag
        print(f"{ExifTags.TAGS.get(tag_code)}: {value}")
© www.soinside.com 2019 - 2024. All rights reserved.