Skip to content

Instantly share code, notes, and snippets.

@terrettaz
Created February 12, 2011 10:27
Show Gist options
  • Select an option

  • Save terrettaz/823678 to your computer and use it in GitHub Desktop.

Select an option

Save terrettaz/823678 to your computer and use it in GitHub Desktop.
This script orders your jpeg files by adding a prefix number to each file based on the EXIF creation date. It can be run recursively. It ask a confirmation before making any changes.
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
__version__ = "$Revision: 0.1 $"
__author__ = "Pierrick Terrettaz"
__date__ = "2007-08-15"
import sys, os
import popen2
import pprint
def usage():
print "%s [option] dir [dir1 [dir n]]" % os.path.basename(sys.argv[0])
print "Options"
print " -h print this help"
print " -r rename file recursively"
print "\nThis script orders your jpeg files by adding "
print "a prefix number to each file based on the EXIF "
print "creation date. It can be run recursively. "
print "It ask a confirmation before making any changes."
def formatNumber(num, count):
s = ""
if count < 10:
return s + "%s" % num
else:
numLen = len("%s" % num)
countLen = len("%s" % count)
if numLen >= countLen:
s += formatNumber(num, count/10)
else:
s += "0" + formatNumber(num, count/10)
return s
def check_tools():
valid_os = ['posix']
try:
valid_os.index(os.name)
except:
print "supported os: ",
pprint.pprint(valid_os)
sys.exit(1)
commands = ['exiftool', 'awk', 'xargs']
missing = []
for c in commands:
r, w = popen2.popen2('which %s' % c)
ret = r.readline()[:-1]
r.close()
w.close()
if ret == '':
missing.append(c)
if len(missing) > 0:
print "missing programs: ",
pprint.pprint(missing)
sys.exit(1)
def search_files(directory, paths, recursive):
if os.path.exists(directory) and os.path.isdir(directory):
for f in os.listdir(directory):
path = os.path.sep.join((directory, f))
if os.path.isfile(path) and f.lower().endswith('.jpg'):
try:
paths.index(path)
except:
paths.append(path)
elif recursive and os.path.isdir(path):
search_files(path, paths, recursive)
if __name__ == '__main__':
if len(sys.argv) == 1 or sys.argv[1] == '-h':
usage()
sys.exit(1)
check_tools()
paths = []
recursive = sys.argv[1] == '-r'
if recursive:
args = sys.argv[2:]
else:
args = sys.argv[1:]
for i in args:
search_files(i, paths, recursive)
r, w = popen2.popen2(
'xargs -I{} exiftool -f -DateTimeOriginal {} \
| grep Original | awk {\'print $4 " " $5\'}')
for p in paths:
w.write(p+'\n')
w.close()
tmp = []
i = 0
for l in r.readlines():
date_exif = l[:-1]
if date_exif != ': -':
tmp.append((date_exif, paths[i]))
i += 1
r.close()
paths = tmp
paths.sort()
i = 1
size = len(paths)
print "%d files will be renamed, continue ? [y/n] " % size,
choice = sys.stdin.readline()[:-1]
if choice.lower() == 'y':
for date, p in paths:
f = os.path.basename(p)
d = os.path.dirname(p)
old_file = os.path.sep.join((d, f))
try:
idx = f.index('_')
int(f[:idx]) # check if the predfix is an number
f = f[idx+1:]
except: pass
new_file = "%s%s%s_%s" % (d, os.path.sep, formatNumber(i, size), f)
os.rename(old_file, new_file)
i += 1
print "%d files renamed" % (i-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment