Pages

Resequencing Photos from more than one Camera

This is a python script which I have written to resequence photos taken from two cameras. The script makes use of the date and time of the photos to sequence the filenames.

import os, glob, time

fprefix = "Beijing"        # Name to prefix photos
folder  = "D:/Beijing2008" # Folder to look for photos
ext     = "jpg"            # Extension of photos
sno     = 1                # Serial number to start

print "Folder = ", folder
print '-' * 60
date_file_list = []
for file in glob.glob(folder + "/*." + ext):
  stats = os.stat(file)
  lastmod_date = time.localtime(stats[8])
  date_file_tuple = lastmod_date, file
  date_file_list.append(date_file_tuple)
  #print date_file_list  # test

date_file_list.sort()
#date_file_list.reverse()  # newest mod date now first

import string
print "%-40s %s" % ("filename:", "last modified:")
for file in date_file_list:
  folder, file_name = os.path.split(file[1])

  # Rename File
  ext = os.path.splitext(file_name)
  file_name = fprefix + ("%03d" % sno) + string.lower(ext[1])
  sno = sno + 1
  newfile = folder + "/" + file_name

  os.rename(file[1], newfile)

  # convert date tuple to MM/DD/YYYY HH:MM:SS format
  file_date = time.strftime("%m/%d/%y %H:%M:%S", file[0])
  print "%-40s %-20s %s" % (folder, file_name, file_date)