Skip to content

Instantly share code, notes, and snippets.

@vitaliel
Created December 1, 2008 12:30
Show Gist options
  • Save vitaliel/30716 to your computer and use it in GitHub Desktop.
Save vitaliel/30716 to your computer and use it in GitHub Desktop.
# Author: Vitalie Lazu
# Date: Mon, 01 Dec 2008 14:28:28 +0200
# Rake tasks to help you with family photos:
# * Download photos from camera
# * Arrange photos in folders by date like digikam,
# * Rotate them according exif information
#
# Setup to use this tasks:
# apt-get install rubygems imagemagick gphoto2 rake
# gem install exifr
namespace :ph do
desc "Get photos from camera to current folder"
task :get do
sh "gphoto2 -R -P"
end
desc "Delete all photos from camera"
task :delete do
sh "gphoto2 -R -D"
end
desc "Rotate photos from exif info"
task :rotate => :init do
for_each_image do |fname, info|
p info.orientation
if info.orientation
x = FakeImg.new
info.orientation.transform_rmagick(x)
op = x.to_s
dest_file = "r-" << fname
if op.length > 0
mv fname, dest_file
cmd = "convert '#{dest_file}' #{op} '#{fname}'"
puts cmd
sh cmd
end
end
end
end
desc "Arrange photos in folders by date"
task :arrange => :init do
for_each_image do |fname, info|
dir = info.date_time.strftime("%Y-%m-%d")
op = info.orientation.transform_rmagick(FakeImg.new).to_s
print "%s %s %s\n" % [fname, dir, op]
dest_file = File.join(dir, fname)
mkdir_p dir
if op.length > 0
cmd = "convert '#{fname}' #{op} '#{dest_file}'"
puts cmd
sh cmd
else
if test ?f, dest_file
puts "File exits: #{dest_file}"
else
mv fname, dest_file
end
end
end
end
desc "Show photos info"
task :list => :init do
for_each_image do |fname, info|
print "%s %s %s\n" % [fname, info.date_time, info.orientation.transform_rmagick(FakeImg.new).to_s]
end
end
task :init do
require 'rubygems'
require 'exifr'
end
def for_each_image(&block)
for fname in Dir["*"]
next unless test(?f, fname) && fname =~ /\.jpe?g$/i
yield fname, EXIFR::JPEG.new(fname)
end
end
class FakeImg
def initialize
@cmd = []
end
def flip
@cmd << "-flip"
self
end
def flop
@cmd << "-flop"
self
end
def rotate(degrees)
@cmd << "-rotate #{degrees.to_i}" if degrees.to_i != 0
self
end
def to_s
@cmd * ' '
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment