2012-06-27 9 views
5

Voglio tagliare un cerchio da un'immagine usando rmagick.Tagliare il cerchio fuori dall'immagine con RMagick

Ecco un esempio di quello che mi piacerebbe essere in grado di realizzare:

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

Sembra che io voglio usare http://studio.imagemagick.org/RMagick/doc/draw.html#circle per tagliare un cerchio, e poi clip_path per mascherare ma i documenti non sono molto chiari. Qualcuno potrebbe indicarmi la giusta direzione?

risposta

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

Come faresti questo con due immagini? Invece di un cerchio, con un'immagine trasparente –

+0

Questa soluzione non funziona per un'immagine png con canale alfa – Dmitry

1

Questo è come vorrei farlo con Imagemagick e 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"); 

si dovrebbe essere in grado di seguire il processo?

Ripulire le immagini temporanee in seguito - Tendo a salvare le immagini temporanee in formato .miff e quindi a scrivere un ciclo per eliminare tutte le immagini .miff in seguito. In alternativa, basta lasciarli e se si utilizza lo stesso nome per le immagini temporanee verranno sovrascritti ogni volta che viene eseguito il codice.

+0

Potresti fornire un riferimento Rmagic? – Almaron