2012-06-27 9 views
5

Quiero cortar un círculo de una imagen usando rmagick.Corte el círculo de la imagen con RMagick

He aquí un ejemplo de lo que me gustaría ser capaz de lograr:

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

Parece que quiero usar http://studio.imagemagick.org/RMagick/doc/draw.html#circle para cortar un círculo, y luego clip_path para enmascarar pero los documentos no son muy claros. ¿Alguien podría apuntarme en la dirección correcta?

Respuesta

12
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' 
+0

¿Cómo harías esto con dos imágenes? En lugar de un círculo, con una imagen transparente –

+0

Esta solución no funciona para una imagen png con canal alfa – Dmitry

1

Esta es la forma en que lo haría con Imagemagick y 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"); 

usted debería ser capaz de seguir el proceso?

Limpiar imágenes temporales después - Tiendo a guardar las imágenes temporales en un formato .miff y luego escribir un ciclo para eliminar todas las imágenes .miff después. Alternativamente déjelos y si usa el mismo nombre para las imágenes temporales, se sobrescribirán cada vez que se ejecute el código.

+0

¿Podría dar una referencia de Rmagic? – Almaron

Cuestiones relacionadas