Skip to content

Instantly share code, notes, and snippets.

@NonlinearFruit
Last active December 26, 2016 14:50
Show Gist options
  • Save NonlinearFruit/4e0b11449cdfce3f3b5b465cb2e3a489 to your computer and use it in GitHub Desktop.
Save NonlinearFruit/4e0b11449cdfce3f3b5b465cb2e3a489 to your computer and use it in GitHub Desktop.
Sorts photos into a directory hierarchy according to their timestamp.
# The script is for taking a ton
# of photos and placing them into
# a structure like:
#
# - 2016
# - 01 January '16
# - 02 February '16
# - 03 March '16
# - ...
# - 2015
# - 01 January '15
# - 02 February '15
# - 03 March '15
# - ...
# - ...
#
# Note:
# - Images that fail will be printed to stdout (manually sort)
# - Doesn't handle videos
# - Sorted 10 gigs in 24 minutes
from PIL import Image
import os
import calendar
from shutil import copyfile
#//////////////
# SET ME PLEAZ
#//////////////
# Sorts all photos in there directories (and subdirs)
target_folders = [
"/media/nonfrt/FreeAgent Drive/2016-02 DCIM/Camera/",
"/media/nonfrt/FreeAgent Drive/Dads old pics/",
"/media/nonfrt/FreeAgent Drive/General/stuff/Pictures/",
"/media/nonfrt/FreeAgent Drive/Pictures and Videos/Pictures/Much a to do about nothing/",
"/media/nonfrt/FreeAgent Drive/Pictures and Videos/Pictures/Dads Pictures/",
"/media/nonfrt/FreeAgent Drive/Pictures and Videos/Pictures/Grandpas pics/",
"/media/nonfrt/FreeAgent Drive/Pictures and Videos/Pictures/needtobesorted/"
]
# Into here!
result_folder = "/home/nonfrt/Pictures/SortedPhotos/"
#//////////////////
# PLEAZ DON'T EDIT
#//////////////////
# From pycruft:
# http://stackoverflow.com/a/3207973/4769802
def getAllFilePaths(starDir):
f = []
for (dirpath, dirnames, filenames) in os.walk(starDir):
f.extend([[dirpath+x,dirpath+"/"+x][dirpath[-1]!="/"] for x in filenames])
return f
# From ashoalm:
# http://stackoverflow.com/a/23064792/4769802
IMAGE = None
def get_date_taken():
return IMAGE._getexif()[36867]
def get_folder(dateString):
pieces = dateString.split(":")
yr = pieces[0]
mn = pieces[1]
fldr = result_folder + yr + "/"
x = int(mn)
y = [str(x),"0"+str(x)][x<10]
y += " "+calendar.month_name[x]+" '"+yr[2:]
fldr += y + "/"
return fldr
# For each target dir
for target_folder in target_folders:
try:
# For each file in that dir
for photo in getAllFilePaths(target_folder):
IMAGE = None
try:
IMAGE = Image.open(photo)
filename = photo.split("/")[-1]
path = get_folder(get_date_taken())
IMAGE.save(path+filename)
except:
try:
os.makedirs(path)
IMAGE.save(path+filename)
except:
try:
IMAGE.save(target_folder+"junk/"+filename)
# print "Fail: "+photo
# print "\t[Photo in Junk]: "+target_folder+"junk/"
except Exception as e:
# try:
# copyfile(photo,target_folder+"junk/")
# print "Double Fail: "+photo
# print "\t[File in Junk]:"+path+filename
# except:
print "x3 Fail: "+photo
# print str(e)
print "\nDONE: "+target_folder+"!!\n"
except:
print "FAILED: "+target_folder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment