Saari Development

This blog is intended to be a log of my (Ali Rizvi's) professional ramblings as a software development engineer. I intend to add logs of my experience with different technologies, software, tech books/articles and related stuff from time to time. My intention is to have an archive for my personal use and public benefit.

Monday, June 25, 2007

Ruby: Rotating Image Files Code


require 'rmagick'
require 'exifr'
require 'pp'

ROTATE_CLOCKWISE = 90
ROTATE_ANTICLOCKWISE = -90

def rotate(file)
image_exif = EXIFR::JPEG.new(file).exif

puts "#{file} : #{image_exif[:orientation]}"
if (image_exif[:orientation] == EXIFR::TIFF::RightTopOrientation)
degrees = ROTATE_CLOCKWISE
elsif (image_exif[:orientation] == EXIFR::TIFF::LeftBottomOrientation)
degrees = ROTATE_ANTICLOCKWISE
else
puts "Not rotating #{file}"
return
end

image = (Magick::Image.read(file)).first
#image.rotate(degrees).write("#{file.split('.').first}_rotated.jpg")
image.rotate(degrees).write("#{file}")
end

dir = ARGV[0]

Dir.entries(dir).each do |file|
if (file =~ /jpg/i)
rotate(File.join(dir, file))
end
end

1 Comments:

Blogger Remco van 't Veer said...

Can be done a lot easier with EXIFR:

EXIFR::JPEG.new(file).exif.orientation.transform_rmagick(Magick::Image.read(file).first)

The transform_rmagick method does all the rotation for you.

9:49 AM  

Post a Comment

<< Home