![]() |
|
Image sorting - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Python (https://sinister.li/Forum-Python) +--- Thread: Image sorting (/Thread-Image-sorting) |
Image sorting - Deque - 04-11-2014 I wrote a small python script to sort my images of my son into folders named month + year where the image was taken. Not more or less. Put into the directory where the images are and run. Code: #moves images of working directory into folders with their creation month + year
from shutil import move
from os import listdir, makedirs, getcwd
from os.path import isfile, join, exists, isdir
import exifread
import sys
months = {'01' : 'Januar', '02' : 'Februar', '03' : 'Maerz', '04' : 'April',
'05' : 'Mai', '06' : 'Juni', '07' : 'Juli', '08' : 'August', '09' : 'September',
'10' : 'Oktober', '11' : 'November', '12' : 'Dezember'}
path_name = getcwd()
files = [ f for f in listdir(path_name) if isfile(join(path_name,f))]
for image in files:
with open(image, "rb") as f:
tags = exifread.process_file(f, stop_tag='EXIF DateTimeOriginal')
datestr = "0"
if "EXIF DateTimeOriginal" in tags:
datestr = str(tags["EXIF DateTimeOriginal"])
elif "Image DateTime" in tags:
datestr = str(tags["Image DateTime"])
if not datestr == "0":
month = months[datestr.split(":")[1]]
year = datestr.split(":")[0]
folder = month + " " + year
if not exists(folder):
makedirs(folder)
if isdir(folder):
move(image, folder)
print "moved", image, "to", folder, "with date", datestr
else:
print >> sys.stderr, folder, "is no directory"
else:
print "no date found for", imageRE: Image sorting - Psycho_Coder - 04-11-2014 This is why I love programmers the most. Write little scripts and Get your work done simply,instead of doing it manually. I do a similar thing when organizing my files into specified directories. Thing become simple with code and Python. I love this language more than Java. Truly fabulous. |