Saari Development

Ali Rizvi’s Technical Blog as a Professional Software Development Engineer

Archive for June 2007

Ruby: Rotating Image Files Description

without comments

I was looking for a tool that would do the same thing that flick does on upload, that is rotate the files appropriately based on information embedded in the image files.

Finally I decided to just do it!

Since all my photos are on my windows machine that was my platform of choice.

Thankfully RMagick package for windows comes with ImageMagick installer. The only thing tricky was to install the gem file from the locally downloaded gem file.

While searching for EXIF library for Ruby I found quite a few on RubyForge but finally exifr seemed most functional/friendly.

Entry below shows the code I ended up with.

Instructions for outputting the html for the code in gVim on windows:

* :colorscheme delek
* Syntax -> Convert to HTML
* remove html, head and body tags
* paste to blogspot

Written by imsaar

June 25, 2007 at 12:48 pm

Posted in ruby

Ruby: Rotating Image Files Code

with one comment

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

Written by imsaar

June 25, 2007 at 12:43 pm

Posted in code, ruby