I have over 500 images (png /jpg) having wrong captured date (Date taken) because of wrong camera date settings. I moved photos to mobile and mobile gallery sorts photos on the
PNG does not support EXIF so I made this to fix the creation/modification time instead, based on Wilbur Dsouza's answer:
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])