Cut circle from image using RMagick

I want to cut a circle from an image using rmagick.

Here is an example of what I would like to achieve:

http://img375.imageshack.us/img375/1153/walterf.jpg  -> http://img15.imageshack.us/img15/8129/circlethumb.png

I seem to want to use http://studio.imagemagick.org/RMagick/doc/draw.html#circle to cut a circle and then clip_path to mask it, but the docs are not very clear. Can anyone point me in the right direction?

+5
source share
2 answers
require 'rmagick'

im = Magick::Image.read('walter.jpg').first

circle = Magick::Image.new 200, 200
gc = Magick::Draw.new
gc.fill 'black'
gc.circle 100, 100, 100, 1
gc.draw circle

mask = circle.blur_image(0,1).negate

mask.matte = false
im.matte = true
im.composite!(mask, Magick::CenterGravity, Magick::CopyOpacityCompositeOp)

im.write 'walter_circle.png'
+12
source

Here's how I will do it with Imagemagick and php:

// Canvas the same size as the final image
exec("convert -size 800x533 xc:white white.jpg");
// The mask 
exec("convert -size 800x533 xc:none -draw \"fill black circle 400,265 400,50\"  write_mask.png");
// Cut the whole out of the canvas  
exec("composite -compose Dst_Out write_mask.png white.jpg -matte step.png");
// Put the canvas over the image and trim off excess white background   
exec("convert IMG_5745.jpg  step.png -composite -trim final.jpg");

Do you need to follow the process?

- .miff, .miff. , tempory, .

+1

All Articles