AnsweredAssumed Answered

Using ExifRead on reapting filenames

Question asked by jpilbeam Champion on Jul 23, 2019
Latest reply on Jul 24, 2019 by jpilbeam


I have a lot of JPEGs in a Windows directory. In most cases these JPEGs do not have similar filenames. On the other hand, there are some repeats:


Using exifread, I'm appending the 'date taken' to the end of the seven digit number of the filenames. This script gets me close, but it's not exactly doing what I want. It skips the files with similar names (e.g. 7400010 (2), 7400010b). Also, after running the script I'm having trouble avoiding the '.' of the file extension. Ideally, I'd like every filename to have this structure: 7400010_2018-04-25. And as far as the files with similar names goes, how do I preserve the (2) and the b?

Loops through folder and adds the 'Date Taken' from JPG

import arcpy, sys
import exifread
from exifread import exif
import os
##import time
from datetime import datetime

im = r"\\path\to\pics2\CH74"

for root, dirnames, filenames in os.walk(im): #iterate directory
    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
                ds = time.strptime(dt, '%Y:%m:%d %H:%M:%S')
                nt = time.strftime("%Y-%m-%d",ds)
                newname = fname[0:8] + "_" + nt + ".jpg"

                os.rename(os.path.join(root,fname), os.path.join(root,newname))


Here's how the filenames look after running this script: