Crop an Image using Ruby and Rmagick

#!/usr/local/bin/ruby -w
require ‘RMagick’
include Magick

puts “Please enter the path of the file for which you want to perform cropping(complete path with file extension) :: \n ”
path=gets #gets the path from the user

puts “The file path specified by you is as follows :: \n ”
puts path.inspect #prints the user given path on the scrreen

path = path.chomp #this command helps remove the newline in the end of path to make it executable for Image.read()
img = Magick::Image.read(path).first #reads the image specfied by the user path

thumb = img.scale(640,480) #scales image to 640×480

puts ” Please choose the option
1 = Crop from left
2 = Crop from right
3 = Crop from top
4 = Crop from bottom ”

choice = gets #gets the user choice of cropping
choice=choice.to_i #converts the choice to integer by to_i so that it can be used in following case statement

puts “please specify the percentage that has to be cropped ”
percentcrop = gets #gets the percentage that has to be cropped from left,right,top,bottom choosen by user
percentcrop = percentcrop.to_i #converts the user entered percentage to integer
percent = percentcrop/100.0 #divides it by 100 so that it can be used for specifing new width and height of the pic

width = 640 ; #width of the pic (in pixels)
height = 480; #height of the pic (pixels)

widthchanged = (width*percent) #new width (based on the percentage specified by the user)
#widthchanged =widthchanged.to_i

heightchanged = (height*percent) #new height (based on the percentage of cropping specified by the user)
#heightchanged = heightchanged.to_i

case choice
when 1
final=thumb.crop(NorthWestGravity,widthchanged,height).write”/home/anmol/blogimage.jpg” #writes the cropped image into a file
final.display #displays the final cropped image
when 2
final=thumb.crop(NorthEastGravity,widthchanged,height).write”/home/anmol/blogimage.jpg” #writes the cropped image into a file
final.display #displays the final cropped image
when 3
final=thumb.crop(NorthWestGravity,width,heightchanged).write”/home/anmol/blogimage.jpg” #writes the cropped image into a file
final.display #displays the final cropped image
when 4
final=thumb.crop(SouthEastGravity,width,heightchanged).write”/home/anmol/blogimage.jpg” #writes the cropped image into a file
final.display #displays the final cropped image
else
puts “No cropping is performed”
final=thumb.write”/home/anmol/blogimage.jpg” #writes the cropped image into a file
final.display #displays the final cropped image
end

This program is the refined version of the previous crop program that I wrote . This program is basically performing cropping on a file specified by the user from four sides left,right,top and bottom according to the percentage specified by the user .

Now I am working on incorporating this program into skid website as a crop module using Ruby on Rails(RoR) . So below are some pictures which show how user can use this program to crop a desired picture.

the starting image

the starting image

cropped from left 60%

cropped from right 60%

cropped from right 60%

cropped from left 60%

cropped from top 66%

cropped from bottom 66%

cropped from bottom 66 %

cropped from top 66 %

Advertisements

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