I'm attempting to extract the Date Taken from jpgs within a directory. And then I want to have the date printed as such: mo/day/year
Here's an example of a JPG's Properties menu:
This code runs with no error but the output isn't what I want.
import exifread
from exifread import exif
import os
import time
im = r"path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal']) #get Date Taken from JPG
date = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
print("Photo:{} Date Taken:{}".format(fname, date))
Photo:0710100.JPG Date Taken:time.struct_time(tm_year=2018, tm_mon=6, tm_mday=5, tm_hour=13, tm_min=49, tm_sec=40, tm_wday=4, tm_yday=166, tm_isdst=-1)
Ideally, I want the output to look as such:
Photo:0710100.JPG Date Taken:06/05/2018
For line 14, I can't find the right format. I've also tried the datetime module but couldn't get that to work right either.
Solved! Go to Solution.
You should delete line 9 in your last code sample, and then adjust the variable name in line 10. Try something like:
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal'])#get JPG 'Date taken'
ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
print(time.strftime("%m/%d/%Y",ds))
Also, I try to avoid using variable names like date and time as they sometimes can conflict with items in the include modules (instead I use dt for datetime and ds for datestring, for example).
Try:
st = '2018:06:05 13:49:40'
dt = time.strptime(st, '%Y:%m:%d %H:%M:%S')
print time.strftime("%m/%d/%Y",dt)
I was experimenting with some exif code in my answers in this thread which may be of interest.
Randy,
Thanks, that helped. As it stands, it only reads the first JPG, which has the date 04/27/2018. It isn't printing the dates of the numerous other files?
im = r"path\to\DOT_SignInventory"
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal'])#get JPG 'Date taken'
st = '2018:04:27 09:30:59'
date = time.strptime(st, '%Y:%m:%d %H:%M:%S')
print(time.strftime("%m/%d/%Y",date))
04/27/2018
04/27/2018
04/27/2018
04/27/2018
04/27/2018
04/27/2018
etc...
It seems the first part of the code involving exif (up to line 😎 works fine. If I put a print statement after line 8:
print(dt)
it gives me:
2018:04:27 09:30:59
2018:04:27 09:36:30
2018:04:27 09:40:00
2018:04:27 09:41:03
2018:04:27 09:42:03
2018:04:27 09:47:00
etc...
You should delete line 9 in your last code sample, and then adjust the variable name in line 10. Try something like:
for root, dirnames, filenames in os.walk(im):
for fname in filenames:
if fname.endswith('.JPG'):
with open(os.path.join(root, fname), 'rb') as image: #file path and name
exif = exifread.process_file(image)
dt = str(exif['EXIF DateTimeOriginal'])#get JPG 'Date taken'
ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
print(time.strftime("%m/%d/%Y",ds))
Also, I try to avoid using variable names like date and time as they sometimes can conflict with items in the include modules (instead I use dt for datetime and ds for datestring, for example).