如何使用PyExifTool返回EXIF标签 [英] How Return EXIF tags using PyExifTool
问题描述
我无法弄清楚如何使用 PyExifTool 从原始照片中提取EXIF标签.我要做的就是提取照片的创建日期,并使用该日期重命名项目文件夹.我已经尝试了多种方法,但不断出现一系列以"ValueError(无法解码JSON对象")结尾的错误.
I am having trouble figuring out how to extract an EXIF tag from a raw photo using PyExifTool. All I want to do is extract the date the photo was created and rename the project folder with that date. I've tried a variety of things I keep getting a series of errors ending with "ValueError("No JSON object could be decoded").
赞:
import exiftool
files = "CRW_1368.CRW"
with exiftool.ExifTool() as et:
metadata = et.get_tag(DateTimeOriginal, files)
print execute_json(metadata)
我不清楚如何正确设置它.我对脚本还比较陌生.
It's not clear to me how to properly set it up. I am relatively new to scripting.
感谢您的帮助!
推荐答案
首先,您必须确保该标签存在.在我的测试中,没有 DateTimeOriginal
标记,因此我必须选择文件中实际存在的标记.我选择了"DateCreated".我收到未定义 execute_json
的错误.当我将其更改为 et.execute_json
时,我能够得到相同的错误.由于您只拉一个标签,因此应该可以打印它.通过快速更改您的打印对帐单,我得到了预期的日期/时间.
First, you have to make sure that the tag exists. In my test, there was no DateTimeOriginal
tag, so I had to select a tag that was actually in my file. I chose 'DateCreated'. I got the error that execute_json
wasn't defined. When I changed it to et.execute_json
, I was able to get your same error. Since you're only pulling one tag, you should be able to just print it. By a quick change of your print statement, I got the expected date/time.
import exiftool
import os, errno
files = file.jpg
with exiftool.ExifTool() as et:
metadata = et.get_tag('DateCreated', files)
print(metadata)
# or, (skipping those two lines) as per your actual question
new_folder = et.get_tag('DateCreated', files)
try:
os.makedirs(new_folder)
except OSError as e:
if e.errno != errno.EEXIST:
raise
在我的情况下,使用print语句,获得了预期的结果我文件中的'2017:04:25 17:40:42'
And in my case, with the print statement, got back the expected '2017:04:25 17:40:42' from my file.
有关目录检查/创建的更新替代方法,请参见:如何创建目录(如果没有)存在吗?
For updated alternatives to directory checking/creating, see: How can I create a directory if it does not exist?
这篇关于如何使用PyExifTool返回EXIF标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!