Advertisement
Guest User

Imagefolder.py

a guest
May 20th, 2011
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.82 KB | None | 0 0
  1. # imagefolder.py
  2. # Date: 03-03-2010
  3. # Author: Dominic Portain
  4. # Dependencies: Windows 5.0, Python 2.6, EXIF.py
  5. # Function: Moves all JPGs and TIFFs into subfolders of the format YYMMDD
  6.  
  7. import os
  8. import EXIF
  9. import time
  10. path=str(os.getcwd()) # current working directory
  11. dirList = os.listdir(path)
  12. for fname in dirList:
  13.     fnamesplit=fname.split(".") # split filename to get extensions
  14.     if len(fnamesplit)>1: # file actually has an extension?
  15.         if fnamesplit[-1] in ('jpg', 'JPG', 'jpeg', 'JPEG', 'JPE', 'TIF', 'TIFF', 'tif', 'tiff'):
  16.             JPGi = open ("\\".join((path, fname)), 'rb')
  17.             tags = EXIF.process_file(JPGi, stop_tag='Image DateTime')
  18.             JPGi.close()
  19.  
  20.             if 'Image DateTime' in tags.keys(): # sufficient Exif data?
  21.                 imagedatetime = str(tags['Image DateTime']).split(" ")
  22.                 imagedate = imagedatetime[0].split(":")
  23.                 ifolder = imagedate # format: YYYYMMDD
  24.                 ifolder[0]=imagedate[0][2:4] # format: YYMMDD
  25.                 ifolderstr="".join(ifolder)
  26.                 notice = ""
  27.             else: # using file properties: the earlier one of creation and modification date
  28.                 notice = ", using file properties (no EXIF)"
  29.                 (fnameMode,fnameIno,fnameDev,fnameNlink,fnameUID,fnameGID,fnameSize,fnameAcctime,fnameModtime,fnameCreattime) = os.stat(fname)
  30.                 ifolderstr = time.strftime('%y%m%d', time.localtime(min(fnameModtime,fnameCreattime)))
  31.  
  32.             if not os.path.exists("\\".join((path, ifolderstr))): # path already available?
  33.                 os.system(" ".join(("mkdir", ifolderstr)))
  34.                 print " ".join(("Created Folder:", ifolderstr))
  35.             moveerror = os.system("".join(("move ", "\"", fname, "\" ", ifolderstr))) # NTFS move is much faster than copy + delete
  36.             if moveerror is 0:
  37.                 print " ".join(("Moved", fname, "to", ifolderstr, notice))
  38.             else:
  39.                 print " ".join(("Error moving", fname, "to", ifolderstr))
  40.     # ignore everything else in this folder
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement