This forum is in archive mode. You will not be able to post new content.

Author Topic: [Python] Image Sorting  (Read 981 times)

0 Members and 1 Guest are viewing this topic.

Offline Deque

  • P.I.N.N.
  • Global Moderator
  • Overlord
  • *
  • Posts: 1203
  • Cookies: 518
  • Programmer, Malware Analyst
    • View Profile
[Python] Image Sorting
« on: April 15, 2014, 10:10:58 AM »
I wrote a small python script to sort my images into folders named month + year where the image was taken. (I've got just too many from my son and didn't like to sort them by hand at some point). Not more or less. Put into the directory where the images are and run.

Code: (Python) [Select]
#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' : 'January', '02' : 'February', '03' : 'March', '04' : 'April',
'05' : 'May', '06' : 'June', '07' : 'July', '08' : 'August', '09' : 'September',
'10' : 'October', '11' : 'November', '12' : 'December'}

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", image
« Last Edit: April 15, 2014, 10:11:13 AM by Deque »

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.