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

5

我试图在我的png图像上绘制图像轮廓(边框)。

原始图片 Photoshop 预期的 19 像素描边样式结果

我在imagemagick论坛上找到了有用的脚本。(imagemagick 论坛

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

但是我的最佳尝试与Photoshop的结果相距甚远。 im_res

我尝试了许多Radius * Sigma的组合,但似乎模糊技巧得到的效果最好..(对于这个例子使用“-blur 9x11”)

问题
* 我能否从Imagemagick、PIL或其他CLI工具中获得更好的图像描边效果?
* 如果可以,如何操作?
非常感谢您阅读这个问题。


1
你的命令 convert source.png -background '#2a7940' -alpha background -channel A -blur 9x11 stroked.png 无法复制你展示的图片! - fmw42
@fmw42 谢谢您让我知道!我之前使用的是7.0.8-47,现在我知道它有一些模糊问题了,谢谢! - sngjuk
1
在Imagemagick 7中,当模糊图像时,会模糊透明通道,除非您在-模糊之前添加-channel rgb,然后再在之后添加+channel。@sngjuk - fmw42
1
相关:https://dev59.com/01sX5IYBdhLWcg3wY-kh - Ciro Santilli OurBigBook.com
3个回答

3

当你需要在使用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”副本返回并将其合成到修改后的绿色部分上来完成。

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

编辑以添加输出图像...enter image description here


这绝对是最优质的!!感谢有用的技巧(+寄存器使用),再次感谢! - sngjuk

2
这可能会让你有一个起点:
convert bomb-source.png \
  \( +clone -alpha extract -morphology edge-out disk:19 -fill green -opaque white \) \
  -compose lighten -composite -transparent black result.png

enter image description here

“-transparent black” 不是最佳选择,如果您的中心图像包含黑色,则需要再考虑一下... 如果在单词 “disk:19” 后添加 “-write stroke.png”,则会生成一个名为 “stroke.png”的新文件,显示形态学正在进行的操作。”

1
不要急着接受这个答案 - 我认为还有一些人会有更好的想法 :-) - Mark Setchell

2
这里有另一种使用-模糊来反锯齿ImageMagick中描边的方法。
输入: enter image description here 最初的回答:
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。

最初的回答。


这是最优质的品质,处理时间合理!非常感谢来自地球另一端的您!! - sngjuk

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接