如何在python中更改图像捕获日期?

时间:2015-10-09 06:48:29

标签: python image image-processing

由于相机日期设置错误,我有超过500张图像(png / jpg)拍摄日期错误(拍摄日期)。我将照片移动到手机和移动画廊,根据“拍摄日期”对照片进行排序。我希望所有照片都按顺序显示。

那么如何使用python脚本更改捕获日期(采取日期)?

4 个答案:

答案 0 :(得分:7)

不需要编写Python,您可以使用jhead在终端的一行中完成。例如,将所有EXIF时间向前调整1小时

jhead -ta+1:00 *.jpg

对您的文件进行 COPY 并在第一个文件中进行测试!

here下载。

答案 1 :(得分:1)

使用piexif库很容易做到这一点:

from datetime import datetime
import piexif

filename = 'image.jpg'
exif_dict = {'Exif': { piexif.ExifIFD.DateTimeOriginal: datetime(2018, 1, 1, 0, 0, 0).strftime("%Y:%m:%d %H:%M:%S") }}
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filename)

此脚本会将新日期2018:01:01 00:00:00插入Date Taken的{​​{1}} EXIF字段值中。

答案 2 :(得分:0)

我可能迟到了这个聚会,但是我写了一个python脚本,用于根据文件名格式(例如IMG-20160117-WA0001.jpg)批量更改whatsapp照片的拍摄时间字段。 同样,这不会覆盖现有属性。 https://github.com/dsouzawilbur/Scripts/blob/master/Change_Photo_Taken_Time.py

import os
import re
import piexif

def absoluteFilePaths(directory):
    for dirpath,_,filenames in os.walk(directory):
        for f in filenames:
            fullPath = os.path.abspath(os.path.join(dirpath, f))
            if re.match(r"^IMG-\d\d\d\d\d\d\d\d-WA\d\d\d\d.*", f) and not re.match(r"^IMG-\d\d\d\d\d\d\d\d-WA\d\d\d\d-ANIMATION.gif", f):
                print(f+" Matched")
                match = re.search("^IMG-(\d\d\d\d)(\d\d)(\d\d)-WA\d\d\d\d.*", f)
                year = match.group(1)
                month= match.group(2)
                day = match.group(3)
                exif_dict = piexif.load(fullPath)
                #Update DateTimeOriginal
                exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                #Update DateTimeDigitized               
                exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                #Update DateTime
                exif_dict['0th'][piexif.ImageIFD.DateTime] = datetime(int(year), int(month), int(day), 4, 0, 0).strftime("%Y:%m:%d %H:%M:%S")
                exif_bytes = piexif.dump(exif_dict)
                piexif.insert(exif_bytes, fullPath)
                print("############################")


absoluteFilePaths("__DIRECTORY_WITH_PHOTOS__")

答案 3 :(得分:0)

PNG不支持EXIF,因此我根据Wilbur Dsouzaanswer来修正创建/修改时间:

import datetime
import os
import re
import sys
import time

import piexif


def fix(directory):
    print(directory)
    for dirpath, _, filenames in os.walk(directory):
        for f in filenames:
            fullPath = os.path.abspath(os.path.join(dirpath, f))
            # Format: Screenshot_20170204-200801.png
            if re.match(r"^Screenshot_\d\d\d\d\d\d\d\d-\d\d\d\d\d\d.*", f):
                match = re.search("^Screenshot_(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d).*", f)
                year = int(match.group(1))
                month = int(match.group(2))
                day = int(match.group(3))
                hour = int(match.group(4))
                minute = int(match.group(5))
                second = int(match.group(6))

                date = datetime.datetime(year=year, month=month, day=day, hour=hour, minute=minute, second=second)
                modTime = time.mktime(date.timetuple())

                print(f, date)

                os.utime(fullPath, (modTime, modTime))


if __name__ == "__main__":
    fix(sys.argv[1])