如何使用 Imagemagick 或 PIL 绘制高质量的图像描边(边框)?

How to draw quality image stroke (border) with Imagemagick or PIL?

我试图在我的 png 图像上绘制图像描边(边框)。
(* Photoshop 预期的 19 像素描边图层样式结果)

我从 imagemagick 论坛找到了有用的脚本。 (imagemagick discourse)

convert source.png -background '#2a7940' -alpha background -channel A -blur 9x11 stroked.png

但我最好的尝试与 Photoshop 结果相去甚远。

我尝试了多种 Radius*Sigma 的组合,但它似乎是模糊技巧的最佳结果..(-blur 9x11 这​​个)

问题
* 我可以从 Imagemagick 或 PIL 或其他 CLI 工具获得更好的图像描边吗?
* 如果是这样,如何..?
非常感谢您阅读这个问题。

这可能会让您入门:

convert bomb-source.png \
  \( +clone -alpha extract -morphology edge-out disk:19 -fill green -opaque white \) \
  -compose lighten -composite -transparent black result.png

如果你的中心图像包含黑色,最后的 -transparent black 不是最佳选择,所以我会考虑更多...

如果您在单词 disk:19 后添加 -write stroke.png,您将得到一个名为 stroke.png 的新文件,它向您展示形态学的作用。

当您需要使用 ImageMagick 创建的效果的干净边缘时,您可以将输入图像的大小加倍,运行 操作,然后将其调整回原始输入大小。 运行 操作需要更长的时间,但结果可以大大改善。这是一个示例命令...

convert bomb-source.png -write mpr:in -resize 200% \
   -channel A -morphology dilate disk:38 +channel \
   -fill green -colorize 100 -resize 50% mpr:in -composite result.png

该命令首先读取输入图像并将副本存储在名为“mpr:in”的内存寄存器中。

然后它将输入的大小调整为 200%,并使用“-morphology”将形状扩大大约 38 个像素。在将图像缩小回其输入大小后,这将达到大约 19 个像素。

接下来,它将新形状着色为绿色,并将其大小调整为 50%,恢复到原始大小。

该命令通过带回原始图像的“mpr:in”副本并将其合成到修改后的绿色块上来完成。

增加大小,对其进行处理,并在修改后减小它称为“超级采样”。这是一种使边缘更平滑的常用技术,但它是以牺牲速度为代价的。

已编辑以添加输出图像...

这是我使用 -blur anti-alias ImageMagick 中的笔画的另一种方式。

输入:

Line1 - read input
Line2 - clone it and make it fully opaque green
Line3 - clone the input, extract the alpha channel, dilate it to 19 or 20, then blur by 0x1 to antialias and use -level to remove the inner side of the blur
Line4 - put the dilated alpha into the alpha channel of the green image
Line5 - remove the temporary clones, swap the remaining two images and overlay the original over the green
Line6 - write the output

convert bomb-source.png \
\( -clone 0 -alpha off -fill green -colorize 100 \) \
\( -clone 0 -alpha extract -morphology dilate disk:20 -blur 0x1 -level 50x100% \) \
\( -clone 1,2 -compose copy_opacity -composite \) \
-delete 1,2 +swap -compose over -composite \
result.png

如果您需要更多抗锯齿,请将模糊增加到 0x2。

对于 ImageMagick 7,将 convert 替换为 magick。