Ruby program to scale and crop an Image using Rmagick


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

img = Magick::Image.read(‘/home/anmol/jack.jpg’).first # path of Orignal image that has to be worked upon
puts img.inspect

def try(x,y,width,height)


# converting x,y , width and height to integer values in next four statements
x= x.to_i
y= y.to_i
width= width.to_i
height= height.to_i

# Demonstrate the Image#crop method
@st = ‘/home/anmol/tempimage.jpg’ # path of image written after changing size or not changing also
img = Magick::Image.read(@st)[0]

# Crop the specified rectangle out of the img.
chopped = img.crop(x, y, width,height)

# Go back to the original and highlight the area
# corresponding to the retained rectangle.
rect = Magick::Draw.new
rect.stroke(‘transparent’)
rect.fill(‘black’)
rect.fill_opacity(1.0)
rect.rectangle(x, y, 100+x, 10+y)
rect.draw(img)

img.write(‘/home/anmol/imgbeforecrop.jpg’) #path of image written before cropping

# Create a image to use as a background for
# the “after” image.
bg = Magick::Image.new(img.columns, img.rows)

# Composite the the “after” (chopped) image on the background
bg = bg.composite(chopped, 38,81, Magick::OverCompositeOp)

bg.write(‘/home/anmol/imgaftercrop.jpg’) # path of image written after cropping the desired part

exit

end

puts “Please enter ::
1 = Making image one-fourth of it’s orignal size
2 = Making image half of it’s orignal size
3 = Making image three-fourth of it’s orignal size
4 = Minfing the image
5 = Magnfing the image \n”

print “option selected = ” ; option = gets
option = option.to_i

puts “Please enter x co=ordinate , y co-ordinate , width and height from x and y co-ordinates of the portion that is to be cropped [ALL THE FOUR VALUES TO BE NUMERIC VALUES AND GREATER THAN ZERO] “

print “x co-ordinate ” ; xc= gets ;
print “y co-ordinate ” ; yc= gets ;
print “width ” ; w= gets ;
print “height ” ; h= gets ;

case option
when 1
thumb = img.scale(0.25) #scale image to 25 % of it’s orignal size
thumb.write “/home/anmol/tempimage.jpg”
# path of image written after changing size or not changing also
puts thumb.inspect
try(xc,yc,w,h)

when 2
thumb = img.scale(0.50) #scale image to 50 % of it’s orignal size
thumb.write “/home/anmol/tempimage.jpg”
# path of image written after changing size or not changing also
puts thumb.inspect
try(xc,yc,w,h)

when 3
thumb = img.scale(0.75) #scale image to 75 % of it’s orignal size
thumb.write “/home/anmol/tempimage.jpg”
# path of image written after changing size or not changing also
puts thumb.inspect
try(xc,yc,w,h)

when 4
thumb = img.minify # minify the image
thumb.write “/home/anmol/tempimage.jpg”
# path of image written after changing size or not changing also
puts thumb.inspect
try(xc,yc,w,h)

when 5
thumb = img.magnify #magnify the image
thumb.write “/home/anmol/tempimage.jpg”
# path of image written after changing size or not changing also
try(xc,yc,w,h)

else
puts “the image will remain in it’s orignal size ”
thumb = img
thumb.write “/home/anmol/tempimage.jpg” # path of image written after changing size or not changing also
try(xc,yc,w,h)

end

I said in the last post that I am working on Simple Picture editor for Skid software , So I have posted a liitle bit of code which I have written in Ruby , it’s a small step and a lot has to be done .

This program first asks user weather he/she wants to change the size of the image to one-fourth of it’s orignal size , minify it , magnify it etc . After you specify the size , user is asked about the dimension of the rectangle which is to be cropped from the image . After specifing the rectangle dimension , the image cropped image is written .The program produces 3 images , first image is tempimage.jpg which is created if user agrees to change the size of the image ; second image is imgbeforecrop.jpg which specifies the rectanglular portion in the image which will cropped by blackening that particular portion of the image ; third image is the imgaftercrop.jpg which has only the cropped portion of the image.

This program is not complete yet and lot of work has to be done on it . First thing which I want to do is to incorporate a graph or grid with the picture so that it becomes easier for the user to specify the crop portion otherwise it is a relatively tough task to do .

Other thing is that I want to get the path of the image file from User , but somehow Image.read function doesn’t take User input , so I will try find some other way .

I will subsequently post the newer versions of the program , as soon as I do some work on it that is worth mentioning 😀

PS:: Please don’t forget to change the path of the image , the path of thumb.write (in case statement) and path of @st in try function should match .

Please Install Rmagick and Ruby prior to run the program .

This program was run on Linux and for Windows commands can be slightly different .

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