将图像 A 的一圈复制到图像 B 中

Copy a circle of image A into image B

我有两张图片:

两张图片都是方形的(100x100 像素)。我想从图像 a.jpg 中剪切一个半径为 50 的圆,并将其粘贴到图像 b.jpg 的中间。我想将结果保存在 c.jpg.

如何使用 Linux 命令行工具执行此操作?我需要在 shell 脚本中完成。

可以使用许多不同的技术。 ImageMagick 有 FX language 可以执行复杂的计算。

convert a.jpg b.jpg -fx 'Wi=w/2; Hi=h/2; hypot(Wi-i, Hi-j) < 50 ? u : v' c.jpg

例如...

convert -size 100x100 PLASMA: a.jpg

convert -size 100x100 GRADIENT:LIME-ORANGE b.jpg

convert a.jpg b.jpg -fx 'hypot(50-i, 50-j) < 50 ? u : v' c.jpg

用另一种技术更新。

一种更快的方法是利用您希望裁剪的形状的图像蒙版,并 compose/composite 在两个图像之间使用它。它将需要一种支持 alpha 通道的格式,但仅用于初始工作。例如...

创建一个圆形遮罩,并将值复制到 alpha 通道。

convert -size 100x100 xc:White -fill Black \
        -draw 'circle 50 50 50 5' -alpha Copy mask.png

convert \( a.png mask.png -alpha Set -compose Dst_Out -composite \) \
        b.png -compose Dst_Atop -composite c.png

Eric 的方法更简洁,而且可能更可取,但无论如何这是另一种方法。我非常注重环保和回收利用 ;-) 他的起始图片:

magick b.jpg \( a.jpg \( +clone -threshold 101% -fill white -draw "circle 49,49, 49,99"  \) -channel-fx '| gray=>alpha' \) -flatten result.png

也就是说……"Load b.jpg as the background. Load a.jpg and then create a transparency mask by cloning the entire a.jpg setting it black and drawing a white circle in it and pushing it into the alpha channel. Then flatten that over the top of b.jpg".

结果和Eric的一样。