Skip to content

Instantly share code, notes, and snippets.

@rail01
Last active October 3, 2024 13:51
Show Gist options
  • Save rail01/ec8cc010ddbedf4a8f03ec01cd4076e8 to your computer and use it in GitHub Desktop.
Save rail01/ec8cc010ddbedf4a8f03ec01cd4076e8 to your computer and use it in GitHub Desktop.
Mass-rename RAW files in a folder to give them ISO 8601-ish timestamp names for easier identification, management and preventing duplicates
#!/usr/bin/env python3
# RAW Rename
#
# Goes through a list of files inside a directory and finds RAW files not matching desired naming scheme
# Then uses `exiftool` to change their names based on photo capture timestamp from file metadata
#
# e.g. original filename "DSC_0001.NEF" will be changed to "IMG_1970-01-01T12-00-00.NEF"
# Script only works with RAW file formats supported by `exiftool`: https://exiftool.org/#supported
#
# Having `exiftool` installed is necessary
#
import argparse
import os
import re
parser = argparse.ArgumentParser()
parser.add_argument( '--dir', dest='dir', type=str, default=os.getcwd() )
args = parser.parse_args()
# Normalize the dir string
args.dir = args.dir.rstrip( '/' )
# Get a list of files from target directory
listFiles = os.listdir( args.dir )
# File extensions we want to work with: RAW formats supported by exiftool
allowedExt = ['cr2', 'cr3', 'nef', 'nrw', 'pef', 'dng', '3fr', 'arq', 'arw', 'cs1', 'dcr', 'dcp', 'erf', 'iiq', 'k25', 'kdc', 'mef', 'mrw', 'orf', 'ori', 'raf', 'raw', 'rw2', 'rwl', 'sr2', 'srf', 'srw', 'x3f']
# Change name of a file to ISO-ish timestamp from Exif data
def rawRename( targetFile ):
fileFullPath = args.dir + '/' + targetFile
# Yay `exiftool`
exifToolFix = "exiftool '-filename<DateTimeOriginal' -d 'IMG_%Y-%m-%dT%H-%M-%S%%lc.%%e' '" + fileFullPath +"' -v"
# Run the command
os.system( exifToolFix )
# Print start message
print( 'The script will start renaming files in:', args.dir + '\n' )
# Loop through the list
for file in listFiles:
# Check if extension matches
isAllowedExt = file.lower().endswith( tuple( allowedExt ) )
# Check if name isn't good already
isNameOkay = bool( re.match( r"^IMG_\d{4}-[01]\d-[0-3]\dT[0-2]\d-[0-5]\d-[0-5]\d$", file[:-4] ) )
# Get the files to work on
if isAllowedExt and isNameOkay != True:
rawRename( file )
# Print end message
print( '\nFinished renaming files in:', args.dir )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment