Created
November 30, 2015 12:32
-
-
Save dzerrenner/389964cea0b7892c42bf to your computer and use it in GitHub Desktop.
Read EXIF data and change file creation time accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
import exifread | |
import pywintypes | |
import win32con | |
import win32file | |
import os | |
from pytz import timezone | |
LOCAL_TZ = timezone('Europe/Berlin') | |
def change_creation_time(fname, newtime): | |
wintime = pywintypes.Time(LOCAL_TZ.localize(newtime)) | |
winfile = win32file.CreateFile( | |
fname, win32con.GENERIC_WRITE, | |
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE | win32con.FILE_SHARE_DELETE, | |
None, win32con.OPEN_EXISTING, | |
win32con.FILE_ATTRIBUTE_NORMAL, None) | |
win32file.SetFileTime(winfile, wintime, None, None) | |
winfile.close() | |
if __name__ == "__main__": | |
path = r'E:\DCIM\100ANDRO' | |
for entry in os.listdir(path): | |
if entry.upper().endswith(".JPG"): | |
src = os.path.join(path, entry) | |
f = open(src, 'rb') | |
exif = exifread.process_file(f, stop_tag='EXIF DateTimeOriginal') | |
print(src) | |
if 'EXIF DateTimeOriginal' in exif: | |
t = exif['EXIF DateTimeOriginal'].values | |
ctime = datetime.strptime(t, '%Y:%m:%d %H:%M:%S') | |
change_creation_time(src, ctime) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment