如何将图像区域转换为白色或透明? [英] How to convert image areas to white or transparent?

查看:71
本文介绍了如何将图像区域转换为白色或透明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将下图中的某些矩形区域转换为白色或透明.

I'm trying convert to white or transparent some rectangles areas within the below image.

我可以使用以下命令使用ImageMagick进行创建,该命令首先使所需的颜色透明,最后将其余的颜色用取反"转换为黑色.

I'm able to make it with ImageMagick with the following command, that first makes transparent desired colors and finally convert to black the rest with "negate".

convert input.png \
-transparent '#4B8DF8'  \
-transparent '#27A9E3'  \
-transparent '#2295C9'  \
-transparent '#E7191B'  \
-transparent '#C91112'  \
-transparent '#28B779'  \
-transparent '#17A769'  \
-transparent '#852B99'  \
-transparent '#751E88'  \
-transparent '#D84A38'  \
-transparent '#B4CEF8'  \
-transparent '#17A76A'  \
-transparent '#CA1112'  \
-transparent '#2296CA' \
-transparent '#DDE8FA' \
-alpha extract -negate out_convert.png

到目前为止,我拥有的Python/Wand脚本是这样的:

The Python/Wand script I have so far is this:

from wand.image import Image

with Image(filename='input.png') as img:
    img.transparent_color('#4B8DF8', alpha=0.0)
    img.transparent_color('#27A9E3', alpha=0.0)
    img.transparent_color('#2295C9', alpha=0.0)
    img.transparent_color('#E7191B', alpha=0.0)
    img.transparent_color('#C91112', alpha=0.0)
    img.transparent_color('#28B779', alpha=0.0)
    img.transparent_color('#17A769', alpha=0.0)
    img.transparent_color('#852B99', alpha=0.0)
    img.transparent_color('#751E88', alpha=0.0)
    img.transparent_color('#D84A38', alpha=0.0)
    img.transparent_color('#B4CEF8', alpha=0.0)
    img.transparent_color('#17A76A', alpha=0.0)
    img.transparent_color('#CA1112', alpha=0.0)
    img.transparent_color('#2296CA', alpha=0.0)
    img.transparent_color('#DDE8FA', alpha=0.0)
    img.negate()
    img.save(filename='out_python.png')

然后在下面显示使用 convert 命令(仅黑白)得到的输出以及使用 python/wand 脚本得到的输出(除了其他颜色外)黑色和白色).

And below I show the output I get using convert command (only black and white) and the output with python/wand script (it still has some other colors beside black and white).

为了获得与ImageMagick相同的输出,脚本中缺少什么?Wand是一个很好的python库,还是可以用另一个库完成?

What is missing in my script in order to get the same output as I get with ImageMagick? Is Wand a good python library for this or could be done with another one?

推荐答案

只需将ImageMagick中的图像阈值化,即可将所有颜色(黑色)转换为白色.

You can convert to white all colors but black simply by thresholding your image in ImageMagick.

convert rectangles.png -threshold 0 rectangles2.png

或者只将白色以外的所有颜色都变成白色

Or just make all colors but white into white

convert rectangles.png -fill white +opaque white rectangles3.png

或者只是将图像着色为白色

Or just colorize the image to white

convert rectangles.png -fill white -colorize 100 rectangles4.png

我会误会你的问题吗?

如果您已识别所有颜色,但是在代码中未将它们转换为白色,这是因为某些颜色略有不同.因此,只需在第一个-transparent命令之前添加-fuzz XX%.尝试XX = 0%开始并根据需要增加.

If you have identified all colors, but they are not getting converted to white in your code, it is because some of the colors are very slightly different. So just add -fuzz XX% before your first -transparent command. Try XX=0% to start and increase as needed.

添加:

我怀疑这就是您想要的.你近了.您只需要添加一些模糊值即可.但是我没有使用透明的方法,而是使用opaque_fill()直接将其转换为白色.

I suspect this is what you want. You were close. You just needed to add some fuzz value. But rather than making it transparent, I convert directly to white using opaque_fill().

输入:

from wand.image import Image
from wand.display import display

with Image(filename='color_rectangles.png') as img:
    img.opaque_paint(target='#5f8bfc', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.opaque_paint(target='#43ad49', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.opaque_paint(target='#831d98', fill='white', fuzz=0.30*img.quantum_range, invert=False)
    img.save(filename='color_rectangles_fill_white.png')
    display(img)

请注意,一个人可能只使用0.05的Quantum_range因子即可更改大部分颜色,但是由于在绘制框时使用了抗锯齿功能,因此您需要尽可能增加它的大小,以在不更改其他颜色的情况下删除轮廓.

Note that one could use as little as 0.05 factor of quantum_range and get most of the color changed, but due to antialiasing in drawing your boxes, you need to increase it as much as possible to remove the outline without changing other colors.

这篇关于如何将图像区域转换为白色或透明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆