![](/img/trans.png)
[英]How to use PIL to resize and apply rotation EXIF information to the file?
[英]Trouble accessing exif information with PIL.Image._getexif()
我試圖從“.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' object has no attribute '_getexif'”的唯一谷歌結果是舊的/404'd 沒有幫助,讓我相信這不是一個常見的問題。
看來PIL不是我要完成的工作的合適模塊。
通過使用PyExifTool從nef文件中提取EXIF信息,我能夠達到目的(根據EXIF信息對文件夾中的項目進行排序)。
如果您堅持使用 PIL,下面是一個工作片段,但似乎有更好的模塊可以產生更多結果。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.