Saari Development

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

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
Advertisements

Written by imsaar

June 25, 2007 at 12:43 pm

Posted in code, ruby

One Response

Subscribe to comments with RSS.

  1. 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.

    Remco

    September 7, 2007 at 4:49 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: