The next thing I did was to replace my Perl script that renames pictures from our digital cameras with a Python script. It requires Exiftool to read the metadata from the photo file. It renames all the generic IMG_0001.jpg and DSC_0001.jpg file names with names based on the date they were created (061231-001.jpg, 061231-002.jpg, etc.). Script is below:
#
# renames original file names from camera files
# DSC.*.jpg or IMG.*.jpg to a name based on the date, ie 071231-001.jpg
#
import os
import re
#
# formats the extension to 3 characters based on the number of pictures taken that date
# ie 001, 010, 100, etc
#
def imageCount(value):
if value<10:
return '00'+str(value)
elif value<100:
return '0'+str(value)
else:
return str(value)
#
# set work directory
#
os.chdir('c:\\images')
#
# list all files
#
dirContents = os.listdir('.')
#
# set the image file name pattern
#
imagePattern = re.compile('^[img|dsc].*.jpg',re.IGNORECASE)
#
# create list of images based matching the above pattern
#
imageList = [image for image in dirContents if imagePattern.search(image)]
#
# iterate through the original names to rename the files
#
for oldName in imageList:
#
# shell call to get exif info creation date of image
#
exifOut = os.popen('c:\\bin\\exiftool -CreateDate '+ oldName + ' -d %y%m%d').readlines()
#
# above output comes back as list, so iterate through list
#
for exif in exifOut:
#
# get rid of trailing newline
#
exif = exif.replace('\n','')
#
# split to get just the creation date
#
exifDate = exif.split(' : ',2)
#
# now loop through and check to see if we have other images with the
# filename we want to rename to. assume we won't go over 100
# pictures from that day
#
for i in range(100):
#
# create new file name
#
newName = exifDate[1] + '-' + imageCount(i+1) + '.jpg'
#
# now test if the proposed filename exists since we don't want
# to rename over an existing file
#
fileStat = os.path.exists(newName)
#
# if it doesn't exist we can rename
#
if fileStat==False:
print 'renaming ' + oldName + ' to ' + newName
#
# rename and break out of loop
#
os.rename(oldName,newName)
break
No comments:
Post a Comment