【问题标题】:How can I combine these commands to achieve circular crop in ImageMagick?如何结合这些命令在 ImageMagick 中实现圆形裁剪?
【发布时间】:2017-01-31 13:55:12
【问题描述】:

如何结合这些命令在 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 技能的人可以向老兄展示如何解决这个问题?

维萨

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$ 

【问题讨论】:

    标签: imagemagick


    【解决方案1】:

    这个问题被问了很多。

    给定一个大于圆形的图像。

    convert -size 300x300 plasma: 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
    

    现在,如果您打算裁剪 许多 资源,我强烈建议您创建一次蒙版。根据需要重复使用掩码。

    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。这是一个很酷的解决方案。只有一件事,输出图像就像我想要的一样,但被一大片透明区域包围。如果您明白我的意思,是否可以将图像输出自动裁剪成一个几乎没有覆盖圆边缘的整洁正方形。
    • 简单。就在-trim 作为最后一次操作。我会更新示例。
    • 什么是Center mask.png 在这种情况下应该是Copy mask.png 吗?
    【解决方案2】:

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

    开始图片

    提取半径为 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
    

    【讨论】:

    • 喜欢 FX 解决方案!
    • 我对您的解决方案感兴趣,知道为什么会出现错误消息吗?
    • 您正在运行旧版本的 ImageMagick - 早于 v7。
    • 更新到 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.png magick:此图像格式“PNG”没有解码委托@error/constitute.c/ReadImage/509。 magick:没有图像可以应用属性“%w”@warning/property.c/GetMagickPropertyLetter/2561。 magick:未知图像属性“%w”@warning/property.c/InterpretImageProperties/3499...
    • 它对我不起作用:转换:不合格的绘图原语定义 `circle' @error/draw.c/RenderMVGContent/4466
    猜你喜欢
    • 2011-11-14
    • 1970-01-01
    • 2017-03-21
    • 2016-07-26
    • 2021-08-07
    • 2011-09-25
    • 2013-10-05
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多