Return to Snippet

Revision: 1929
at November 24, 2006 16:21 by block


Initial Code
#Jason Palmer, 2006.
#http://www.jason-palmer.com/
#Use the software as you please, just please give me reference when doing so.

require 'rubygems'
require 'rmagick'

src_img = ARGV[0]
dst1_img = ARGV[1]
dst2_img = ARGV[2]
split_type = ARGV[3].downcase unless ARGV[3].nil?

if src_img.nil? || dst1_img.nil? || dst2_img.nil?
  puts "Usage: img_resize.rb src_img dst1_img dst2_img split_type=horizontal"
  exit(1)
end

if split_type.nil? || (split_type != 'horizontal' && split_type != 'vertical')
  split_type = 'horizontal'
end

begin
  src = Magick::Image::read(src_img).first
rescue Exception=>e
  puts "Error reading source image file"
  puts e.to_s()
  exit(1)
end

begin
  if split_type == 'horizontal'
    left_img = src.crop(0,0, (src.columns / 2), src.rows)
    right_img = src.crop((src.columns / 2), 0, src.columns, src.rows)
  elsif split_type == 'vertical'
    left_img = src.crop(0,0, (src.columns / 2), (src.rows / 2))
    right_img = src.crop((src.columns / 2), (src.rows / 2), src.columns, src.rows)
  end
rescue Exception=>e
  puts "Error splitting file #{split_type}ly"
  puts e.to_s()
  exit(1)
end

begin
  left_img.write(dst1_img)
  right_img.write(dst2_img)
rescue Exception=>e
  puts "Error writing files"
  puts "Make sure you have permission to write to the destination directory"
  puts e.to_s()
end

Initial URL
http://www.jason-palmer.com/2006/11/14/rmagick-image-slicing/

Initial Description


Initial Title
RMagick Image Slicing

Initial Tags


Initial Language
Ruby