Reputation: 21
I'm working with camera kit that produces images with date-times saved as their filenames (e.g. "2019-10-24-10-06-56.jpg"), rather than within their metadata. I'm trying to write a Python script that enables me to extract date-times from their filenames, and add to the date-time metadata properties of each file. This is so I can upload to e.g. Google Photos for correct chronological indexing.
From what I can gather, Google Photos uses the 'Created' date-time property, so I believe this is what I need to change for each JPG file. What's the best way of doing this? I've tried a few things including changing EXIF data (successfully, using piexif) but this doesn't seem to change the image properties as shown in Windows, and it still does not correctly index in Google Photos.
Keen to do this in Python if possible (I'm using Python 2.7) - already written the code to extract date-time from filename.
The following code appears to successfully change the EXIF data:
from datetime import datetime
import piexif
import os
jpgFolder = r"C:\Users\srgan\Desktop\Naturewatch Camera Photos\NaturewatchCameraPhotos_24Oct19"
## count number of photos found
listOfFiles = os.listdir(jpgFolder)
fileCount = len(listOfFiles)
## create datetimeString from JPG filename
for jpg in os.listdir(jpgFolder):
filepath = jpgFolder + "\\" + jpg
year = jpg[0:4]
month = jpg[5:7]
day = jpg[8:10]
hour = jpg[11:13]
mins = jpg[14:16]
datetimeStringNew = day + "/" + month + "/" + year + " " + hour + ":" + mins
## change exif datetimestamp for "Date Taken"
exif_dict = piexif.load(filepath)
exif_dict['Exif'] = { piexif.ExifIFD.DateTimeOriginal: datetime(2019, 10, 25, 1, 1).strftime("%d:%m:%Y %H:%M") }
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filepath)
correctly returning the following:
{'Exif': {36867: '25:10:2019 01:01'}, '0th': {34665: 26}, 'Interop': {}, '1st': {}, 'thumbnail': None, 'GPS': {}}
but this is not reflected in the image properties in Windows (1)/image properties (2)/dated correctly when uploaded to Google Photos.
Thanks!
The Jolly Geographer (Not an experienced coder so apologies for any silly questions)
Upvotes: 2
Views: 2977
Reputation: 1649
Firstly thanks for posting - I had similar problems and your code gave me some good hints!
My issues occured after uploading, as there is no GMT/UTC tag in Exif, so for google photos, when there is no GPS data, it uploads the data with the assumption that the upload location matches the time zone (eg. GMT+11) that the photo was taken in. You can either VPN to that location, or change all the photos once uploaded, by selecting the recent uploads, and 'edit date and time' selecting the new location and re-adjusting the time relatively.
Secondly, I found that updating both the DateTimeOriginal, and the DateTimeDigitized seemed to help.
Thirdly, I think the date format for the Exif data is yyyy:mm:dd hh:mm:ss, so I've updated that below too.
Also, I used a datetime string parser to turn your existing filedate into a python date
Here's my take on your code, that might help you (and keep the rest of the Exif data too!)
from datetime import datetime
import piexif
import os
jpgFolder = r"C:\Users\srgan\Desktop\Naturewatch Camera Photos\NaturewatchCameraPhotos_24Oct19"
## count number of photos found
listOfFiles = os.listdir(jpgFolder)
fileCount = len(listOfFiles)
## create datetimeString from JPG filename
for jpg in os.listdir(jpgFolder):
filepath = jpgFolder + "\\" + jpg
parsedate=datetime.strptime(jpg[0:16], "%Y-%m-%d-%H-%M-%S")
## change exif datetimestamp for "Date Taken"
exif_dict = piexif.load(filepath)
newExifDate=parsedate.strftime("%Y:%m:%d %H:%M:%S")
exif_dict['Exif'][piexif.ExifIFD.DateTimeOriginal]=newExifDate
exif_dict['Exif'][piexif.ExifIFD.DateTimeDigitized]=newExifDate
exif_bytes = piexif.dump(exif_dict)
piexif.insert(exif_bytes, filepath)
Upvotes: 2