RMagick Image Slicing


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #Jason Palmer, 2006.
  2. #http://www.jason-palmer.com/
  3. #Use the software as you please, just please give me reference when doing so.
  4.  
  5. require 'rubygems'
  6. require 'rmagick'
  7.  
  8. src_img = ARGV[0]
  9. dst1_img = ARGV[1]
  10. dst2_img = ARGV[2]
  11. split_type = ARGV[3].downcase unless ARGV[3].nil?
  12.  
  13. if src_img.nil? || dst1_img.nil? || dst2_img.nil?
  14. puts "Usage: img_resize.rb src_img dst1_img dst2_img split_type=horizontal"
  15. exit(1)
  16. end
  17.  
  18. if split_type.nil? || (split_type != 'horizontal' && split_type != 'vertical')
  19. split_type = 'horizontal'
  20. end
  21.  
  22. begin
  23. src = Magick::Image::read(src_img).first
  24. rescue Exception=>e
  25. puts "Error reading source image file"
  26. puts e.to_s()
  27. exit(1)
  28. end
  29.  
  30. begin
  31. if split_type == 'horizontal'
  32. left_img = src.crop(0,0, (src.columns / 2), src.rows)
  33. right_img = src.crop((src.columns / 2), 0, src.columns, src.rows)
  34. elsif split_type == 'vertical'
  35. left_img = src.crop(0,0, (src.columns / 2), (src.rows / 2))
  36. right_img = src.crop((src.columns / 2), (src.rows / 2), src.columns, src.rows)
  37. end
  38. rescue Exception=>e
  39. puts "Error splitting file #{split_type}ly"
  40. puts e.to_s()
  41. exit(1)
  42. end
  43.  
  44. begin
  45. left_img.write(dst1_img)
  46. right_img.write(dst2_img)
  47. rescue Exception=>e
  48. puts "Error writing files"
  49. puts "Make sure you have permission to write to the destination directory"
  50. puts e.to_s()
  51. end

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.