Skip to content

Instantly share code, notes, and snippets.

@markandey
Created November 21, 2010 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markandey/708717 to your computer and use it in GitHub Desktop.
Save markandey/708717 to your computer and use it in GitHub Desktop.
python script which displays a command suggestion to move your images in directories based on their EXIF data (date)
#!/usr/bin/env python
from PIL import Image
from PIL.ExifTags import TAGS
import sys
import datetime
from sys import exit
def get_exif(fn):
ret = {}
i = Image.open(fn)
info = i._getexif()
for tag, value in info.items():
decoded = TAGS.get(tag, tag)
ret[decoded] = value
return ret
if len(sys.argv)<2:
print 'filename is missing';
print 'Usage:',sys.argv[0],'<filename>','<option>' ;
print 'd for date, m for month and y for year' ;
raise SystemExit;
filename = sys.argv[1]
option='d';
if len(sys.argv)<3:
option='d';
else:
option=sys.argv[2]
info=get_exif(filename);
try:
for attr in info:
if attr=='DateTimeOriginal':
d=datetime.datetime.strptime(info[attr], "%Y:%m:%d %H:%M:%S");
if option=='d':
print 'cmd',filename, d.strftime("%d:%m:%Y") ;
elif option=='m':
print 'cmd',filename, d.strftime("%m:%Y") ;
elif option=='m':
print 'cmd',filename, d.strftime("%Y") ;
else:
print 'wrong option!';
break;
except:
print 'error';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment