Skip to content

Instantly share code, notes, and snippets.

@jdevera
Created November 30, 2010 22:09
Show Gist options
  • Save jdevera/722515 to your computer and use it in GitHub Desktop.
Save jdevera/722515 to your computer and use it in GitHub Desktop.
Rename image files depending on the camera that created them.
#!/usr/bin/env python
# Copyright (c) 2010 Jacobo de Vera
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
"""
Rename image files depending on the camera that created them.
Save the original filename in the image's metadata.
"""
import pyexiv2
import re
import os
import sys
import stat
# Easy exif names
Model = 'Exif.Image.Model'
DateTimeOriginal = 'Exif.Photo.DateTimeOriginal'
DocumentName = 'Exif.Image.DocumentName'
def get_new_file_name(filepath, camera_model, date_taken):
# Get all the parts of the name
path , filename = os.path.split(filepath)
filebase, extension = os.path.splitext(filename)
if camera_model == 'NIKON D5000':
if not filebase.startswith('DSC_'):
print "%s is already processed. Not modifying"%(filepath)
return None
prefix = 'd5k'
date_taken_str = date_taken.strftime('%Y%m%d')
seq_number = re.sub(r'DSC_([0-9]{4})', r'\1', filebase, re.I)
final_name = '%s_%s_%s%s'%(prefix, date_taken_str, seq_number, extension.lower())
elif camera_model == 'COOLPIX P2':
if not filebase.startswith('DSCN'):
print "%s is already processed. Not modifying"%(filepath)
return None
prefix = 'cp2'
seq_number = re.sub(r'DSCN([0-9]{4})', r'\1', filebase, re.I)
date_taken_str = date_taken.strftime('%Y%m%d')
final_name = '%s_%s_%s%s' % (prefix, date_taken_str, seq_number, extension.lower())
elif camera_model == 'Nexus One':
prefix = 'nex'
if filebase.startswith(prefix):
print "%s is already processed. Not modifying"%(filepath)
return None
date_taken_str = date_taken.strftime('%Y%m%d_%H%M%S')
final_name = "%s_%s%s"%(prefix, date_taken_str, extension.lower())
else:
print 'No rule for camera model "%s", skipping.'%camera_model
return None
final_path = os.path.join(path, final_name)
return final_path
def rename_file(filepath):
# Get the current metadata
im = pyexiv2.ImageMetadata(filepath)
im.read()
# Try to obtain the image's date and time
if DateTimeOriginal in im:
date_taken = im[DateTimeOriginal].value
else:
print "%s has no date info"%filepath
return
# Try to obtain the camera model
if Model in im:
camera_model = im[Model].value
else:
print "%s has no camera info"%filepath
return
# Apply the model based rules to obtain a new file name
final_path = get_new_file_name(filepath, camera_model, date_taken)
if filepath == None:
return
print "%s -> %s"%(filepath, final_path)
# Save the original file name in the image's Exif
path, filename = os.path.split(filepath)
im[DocumentName] = filename
im.write(True) # preserve timestamps
# Remove executable bit from images
os.chmod(filepath, stat.S_IWUSR | stat.S_IRUSR | stat.S_IRGRP)
# Change the file name.
os.rename(filepath, final_path)
for f in sys.argv[1:]:
rename_file(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment