如何将这些命令组合起来使用 ImageMagick 实现圆形裁剪?

10

我该如何结合这些命令在ImageMagick中实现圆形裁剪?

这个命令可以实现:

convert -size 200x200 xc:none -fill samia.jpg -draw "circle 100,100 100,1" circle_thumb.png

上述代码将拍摄一张照片,并在其上制作圆形裁剪,但该裁剪基于照片的左上角而不是中心点。此命令也可用于裁剪:
convert *.jpg -resize 200x200^ -gravity Center -crop 200x200+0+0 +repage out.png

上述代码将根据图像中心制作一个正方形切割图像。我的目标是:编写一个命令,接受图片作为输入并基于图片中心而非左上角进行圆形裁剪。请问有哪位熟悉IM技术的大佬能帮我解决这个问题吗?Vesa,Ubuntu 15.10更新:我尝试了Mark Setchell的解决方案,但收到以下错误消息:
axx@axx-VPCEA3S1E:~/Desktop/circular-crop$ magick samia.png \( +clone -threshold 101% -fill white -draw 'circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:80+int(h/2)]' \) -channel-fx '| gray=>alpha'   circle.png
magick: no decode delegate for this image format `PNG' @ error/constitute.c/ReadImage/509.
magick: no image to apply a property "%w" @ warning/property.c/GetMagickPropertyLetter/2561.
magick: unknown image property "%w" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%h" @ warning/property.c/GetMagickPropertyLetter/2449.
magick: unknown image property "%h" @ warning/property.c/InterpretImageProperties/3499.
magick: no image to apply a property "%m" @ warning/property.c/GetMagickPropertyLetter/2480.
magick: unknown image property "%m" @ warning/property.c/InterpretImageProperties/3499.
axx@axx-VPCEA3S1E:~/Desktop/circular-crop$ 
2个回答

16
这个问题经常被问到。
给定一个大于圆形的图像。
convert -size 300x300 plasma: input.png

input.png

我们可以绘制一个形状,将值转换为alpha通道,并将其与输入图像合成。
convert input.png \
        -gravity Center \
        \( -size 200x200 \
           xc:Black \
           -fill White \
           -draw 'circle 100 100 100 1' \
           -alpha Copy \
        \) -compose CopyOpacity -composite \
        -trim output.png

output

现在,如果您打算裁剪许多资源,我强烈建议您创建一个遮罩层。需要时可以重复使用该遮罩层。
convert -size 200x200 xc:Black -fill White -draw 'circle 100 100 100 1' -alpha Copy mask.png

for f in $(ls *.jpg)
do
  convert $f -gravity Center mask.png -compose CopyOpacity -composite -trim ${f}_output.png
done

嗨,emcconville。那是一个很酷的解决方案。只有一件事,输出图像就像我想要的,但被一个大的透明区域所包围。是否可能将图像输出自动裁剪成一个整洁的正方形,仅覆盖圆形边缘?如果你明白我的意思的话。 - Vesa
1
简单。只需在最后一个操作中添加“-trim”。我会更新示例。 - emcconville
在这种情况下,“Center mask.png”是什么?是否应该改为“Copy mask.png”? - Folger Fonseca
1
100 100 100 1 是什么意思?我正在考虑裁剪一个半径为 r,中心点为 (x,y) 的圆形。 - user14554732
x y w z 的意思是:以 x,y 为圆心,经过 w,z 的点画一个圆。因此,100 100 100 1 是一个以 200x200 正方形中心为圆心,半径为99的圆。为什么他们不直接用三个数字 x,y,r 呢,我也不知道... - undefined

7

对于“剪裁圆形”的操作不确定,但如果您想要只保留中心圆以外的部分透明,可以按照以下步骤进行操作:

原始图像

enter image description here

使用以下代码提取半径为80的圆形:

magick start.png \( +clone -threshold 101% -fill white -draw 'circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:80+int(h/2)]' \) -channel-fx '| gray=>alpha'   circle.png

enter image description here


1
喜欢这个FX解决方案! - emcconville
1
已更新至ImageMagick 7.x。再次测试后出现新的错误信息:magick samia.png ( +clone -threshold 101% -fill white -draw 'circle %[fx:int(w/2)],%[fx:int(h/2)] %[fx:int(w/2)],%[fx:80+int(h/2)]' ) -channel-fx '| gray=>alpha' circle.pngmagick: 在error/constitute.c/ReadImage/509处找不到此图像格式的解码代理。 magick: 没有图像应用属性"%w" @ warning/property.c/GetMagickPropertyLetter/2561。 magick: 未知的图像属性"%w" @ warning/property.c/InterpretImageProperties/3499... - Vesa
在注释中阅读错误消息真的很困难,您能否在原始问题下单击“编辑”并将它们放在那里?您的起始文件是PNG还是JPEG?您是否像我展示的那样在括号周围留有空格? - Mark Setchell
在原始问题中添加错误消息。该文件为PNG格式。我复制粘贴了您的命令行输入,只更改了文件名,但这应该没问题。 - Vesa
4
对我来说不起作用:转换:不符合要求的绘图原语定义 `circle' @ error/draw.c/RenderMVGContent/4466 - Kred
显示剩余7条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接