首页 > 解决方案 > 向图像添加 exif 日期和时间

问题描述

我有几百张缺少正确createdate字段的照片。我需要这个,以便当我将它们上传到 Google 照片时,它们会被正确排序。

幸运的是,几乎所有的照片都有另一个正确的日期字段,或者datetimeoriginal或之类modifydate的。

我希望使用 pythonexif模块来扫描文件,找到最旧的日期字段并使用它来添加createdate字段。不幸的是,该模块似乎不喜欢在不存在的地方添加新的 exif 字段。它出错了:

RuntimeError: only can add GPS IFD to image, not exif

我还有哪些其他选项可以添加缺失的字段?最好使用 Python,但 Ruby 或 Go 也可以 :-)

标签: pythonexif

解决方案


你绝对应该看看ExifTool命令行工具。它非常强大,所以可能根本不写脚本。

即使很难批处理您的任务,您也可以使用subprocess模块 ( subprocess.call(["exitfool", "param1", "param2"])/ subprocess.check_output(["exitfool", "param1", "param2"])) 从 Python 调用 exiftool。

这是我尝试解决您的问题(命令行):

我们有一张照片test.HEIC。我们来看看它的日期相关的EXIF:

Olhas-MBP:Downloads olia$ exiftool test.HEIC  | grep Date

File Modification Date/Time     : 2020:07:19 21:41:19+03:00
File Access Date/Time           : 2020:07:19 21:41:19+03:00
File Inode Change Date/Time     : 2020:07:19 21:41:19+03:00
Modify Date                     : 2020:03:30 11:41:02
Date/Time Original              : 2020:03:30 11:41:02
Create Date                     : 2020:03:30 11:41:02
Profile Date Time               : 2017:07:07 13:22:32
Create Date                     : 2020:03:30 11:41:02.891+03:00
Date/Time Original              : 2020:03:30 11:41:02.891+03:00
Modify Date                     : 2020:03:30 11:41:02+03:00

所以,这张照片有Create DateModify Date。让我们将“创建日期”设置为现在:

exiftool -r -all= -createdate=now "-exif:createdate<createdate" -overwrite_original test.HEIC
    1 image files updated

现在,让我们看看更新的标签:

Olhas-MBP:Downloads olia$ exiftool test.HEIC | grep Date
File Modification Date/Time     : 2020:07:19 21:51:52+03:00
File Access Date/Time           : 2020:07:19 21:51:53+03:00
File Inode Change Date/Time     : 2020:07:19 21:51:52+03:00
Create Date                     : 2020:07:19 21:51:52+03:00
Profile Date Time               : 2017:07:07 13:22:32

现在,让我们将“创建日期”设置为“修改日期”(不确定我是否选择了正确的标签):

Olhas-MBP:Downloads olia$ exiftool -r -all= -createdate=now "-FileModifyDate<FileCreateDate" -overwrite_original test.HEIC
    1 image files updated

Olhas-MBP:Downloads olia$ exiftool test.HEIC | grep Date
File Modification Date/Time     : 2020:07:19 21:53:19+03:00
File Access Date/Time           : 2020:07:19 21:54:09+03:00
File Inode Change Date/Time     : 2020:07:19 21:54:08+03:00
Create Date                     : 2020:07:19 21:54:08+03:00
Profile Date Time               : 2017:07:07 13:22:32

Olhas-MBP:Downloads olia$ exiftool -r -all= -createdate=now "-FileModifyDate<CreateDate" -overwrite_original test.HEIC
    1 image files updated

Olhas-MBP:Downloads olia$ exiftool test.HEIC | grep Date
File Modification Date/Time     : 2020:07:19 21:54:08+03:00
File Access Date/Time           : 2020:07:19 21:54:51+03:00
File Inode Change Date/Time     : 2020:07:19 21:54:50+03:00
Create Date                     : 2020:07:19 21:54:50+03:00
Profile Date Time               : 2017:07:07 13:22:32

推荐阅读