Run it as: ruby photo_organizer.rb
Code:
#photo_organizer.rb
require 'fileutils'
def fileShortDate(fich)
dt = File.mtime(fich)
dt.strftime("%d-%m-%Y")
end
def isImagem(fich)
File.extname(fich).upcase == ".JPG" or File.extname(fich).upcase == ".PNG"
end
print "Creating dirs "
Dir.foreach(".") { |x| Dir.mkdir(fileShortDate(x)) and print(".") if (isImagem(x)) unless File.directory?(fileShortDate(x)) }
print "\nCopying pics "
Dir.foreach(".") { |x| FileUtils.mv(x, fileShortDate(x)+'/'+x) and print(".") if (isImagem(x)) }
2 comments:
It's very common for you to surprise me with these little scripts.
This time I couldn't resist and supply my own improvements. I've approach the problem by first look into the Exif properties of the picture and discover the day it was taken.
Run it as: python photo_organizer.py
Code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from os import listdir, access, mkdir, stat, F_OK
from time import localtime, strftime
from pyexiv2 import Image
from shutil import move
def isFileImage(filename):
return filename.upper().endswith(('.jpg', '.png'))
def fileDate(filename):
fileDate = None
try:
im = Image(filename)
im.readMetadata()
fileDate = im['Exif.Image.DateTime'].strftime('%d_%b_%Y')
except:
fileDate = strftime("%d_%b_%Y", localtime(stat(filename).st_mtime))
return fileDate
if __name__=="__main__":
for file in listdir('.'):
if isFileImage(file):
fd = fileDate(file)
if not access(fd, F_OK):
mkdir(fd)
move(file, fd)
aha, yep that should be even more reliable. It does mean 1 more dependency(for the exif stuff).
The modified time (mtime), that i use in this case, seems to be accurate.
This script is actually 3 years old or something, but i still keep using it!
I'll keep posting more little scripts! In ruby :)
Post a Comment