如何使用 ImageMagick 在图像上绘制多个覆盖矩形

How can I draw multiple overlay rectangles on an image using ImageMagick

我使用以下代码在另一张图片上绘制覆盖框

        convert                 \
         -background '#0002'    \
         -gravity Center        \
         -fill white            \
         -size ${page_width}x30     \
          caption:""      \
          "${img}"              \
         +swap                  \
         -geometry +0+100            \
         -composite             \
          "${img}"  

但是,我想在多个位置绘制它,例如从图像的顶部到底部每隔 100 个像素绘制一次。 我可以为此使用循环,但我想知道是否有更好的命令或解决方案来解决这个问题?

您可以使用 Imagemagick 在图像上平铺您的图案。

  • 使用 -size ... xc:"#0002" 创建一个“#0002”图像
  • 以类似的方式创建间距高度的完全透明图像
  • 垂直附加两张图片
  • 将它们平铺在输入图像的大小上
  • 在输入图像上合成

输入:

# Line 1: read the input
# Line 2: create the tile (spacing set to 50 in transparent section)
# Line 3: tile it out over the size of the input by replacing it on the input
# Line 4: do the composite
# Line 5: save the output

convert lena.png \
\( -size 256x30 xc:"#0002" -size 256x50 xc:none -append -write mpr:tile +delete \) \
\( -clone 0 -tile mpr:tile -draw "color 0,0 reset" \) \
-compose over -composite \
result.png


结果: