python - 向图像添加 exif 日期和时间
问题描述
我有几百张缺少正确createdate
字段的照片。我需要这个,以便当我将它们上传到 Google 照片时,它们会被正确排序。
幸运的是,几乎所有的照片都有另一个正确的日期字段,或者datetimeoriginal
或之类modifydate
的。
我希望使用 pythonexif
模块来扫描文件,找到最旧的日期字段并使用它来添加createdate
字段。不幸的是,该模块似乎不喜欢在不存在的地方添加新的 exif 字段。它出错了:
RuntimeError: only can add GPS IFD to image, not exif
我还有哪些其他选项可以添加缺失的字段?最好使用 Python,但 Ruby 或 Go 也可以 :-)
解决方案
你绝对应该看看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 Date
和Modify 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
推荐阅读
- php - 在我的 laravel 项目中出现 yahoo 错误
- docker - 在 docker 容器中运行我的 selenium 客户端代码时出现“PKIX 路径构建失败”错误
- python - 在 Python Dash 中将下拉菜单与图形输出相结合
- javascript - JS在手机上对response.data抛出错误
- regex - 如何让所有单词以同一个字母开头和结尾?
- cmake - 使用 Cmake 将目标链接到静态库
- firebase - Firestore REST API | 如何将firestore rest api与邮递员一起使用
- ios - XCode 13 缺少预定义的文本样式
- ruby - 如何避免将 \n 写入文件
- java - 如何使用 Gson 序列化/反序列化枚举