【问题标题】:Trouble accessing exif information with PIL.Image._getexif()无法使用 PIL.Image._getexif() 访问 exif 信息
【发布时间】:2022-03-16 20:27:56
【问题描述】:

我正在尝试从“.nef”文件中提取 exif 信息,以便根据检索到的数据将文件自动分类到文件夹中。

根据我的阅读,PIL 似乎是将信息导入 Python 的不错选择。

我已经安装了 PIL,它可以正确导入,PIL.Image 模块也是如此。

当我尝试调用 'PIL.Image._getexif()' 时出现问题

from PIL import Image
from PIL.ExifTags import TAGS

firstfile = 'link to file'
exif = Image._getexif(firstfile)

得到这个错误:

AttributeError: 'module' object has no attribute '_getexif'

较长版本的代码也会出错:

def get_exif(fn):
    ret = {}
    i = Image.open(fn)
    info = i._getexif()
    for tag, value in info.items():
        decoded = TAGS.get(tag, tag)
        ret[decoded] = value
    Image.close(fn)
    return ret

exifinfo = get_exif(firstfile)

这失败了:

AttributeError: _getexif

也许我的 PIL 安装错了?为什么 '_getexif()' 不能被调用?

注意事项: 直接搜索“AttributeError:'module'对象没有属性'_getexif'”的唯一谷歌结果是旧的/404'd没有帮助,让我相信这不是一个常见的问题。

【问题讨论】:

    标签: python image python-imaging-library exif


    【解决方案1】:

    PIL 似乎不是我想要完成的合适的模块。

    我能够通过使用PyExifTool 从 nef 文件中提取 EXIF 信息来达到我的目的(根据 EXIF 信息对文件夹中的项目进行排序)。

    【讨论】:

      【解决方案2】:

      如果您坚持使用 PIL,下面是一个有效的 sn-p,但似乎有更好的模块可以产生更多的结果。

      https://pypi.org/project/exif/

      from PIL import Image
      from PIL.ExifTags import TAGS
      
      ret = {}
      with Image.open(img_path) as file:
          info = file.getexif()
          for tag, value in info.items():
              decoded = TAGS.get(tag, tag)
              ret[decoded] = value
          return ret
      

      【讨论】:

        猜你喜欢
        • 2020-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多