6

我想使用 rmagick 从图像中切出一个圆圈。

这是我希望能够完成的示例:

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

似乎我想使用http://studio.imagemagick.org/RMagick/doc/draw.html#circle来切割一个圆圈,然后使用 clip_path 来掩盖它,但文档不是很清楚。有人能指出我正确的方向吗?

4

2 回答 2

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'
于 2012-11-11T09:33:47.053 回答
1

这就是我使用 Imagemagick 和 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");

您应该能够按照流程进行操作吗?

之后清理临时图像 - 我倾向于将临时图像保存为 .miff 格式,然后编写一个循环来删除所有 .miff 图像。或者只留下它们,如果您对临时图像使用相同的名称,则每次运行代码时它们都会被覆盖。

于 2012-06-27T21:07:01.873 回答