如何组合这些命令以在ImageMagick中实现循环裁剪?

时间:2017-01-31 13:55:12

标签: imagemagick

如何组合这些命令以在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 个答案:

答案 0 :(得分:7)

这个问题被问了很多。

给定大于圆的图像。

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

答案 1 :(得分:1)

不确定裁剪圈,但是如果你想让除了中心圆之外的所有圈都透明,你可以这样做......

开始图片

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

相关问题