简体   繁体   English

如何将每个不是黑色的像素转换为某种颜色?

[英]How can I convert every pixel that is not black to a certain color?

I want every pixel that is not black to be set to white (or any arbitrary color).我希望每个不是黑色的像素都设置为白色(或任何任意颜色)。

I need this in Python (preferably using PIL, but other libraries can also be considered)我在 Python 中需要这个(最好使用 PIL,但也可以考虑其他库)

Thanks谢谢

Try this:尝试这个:

import sys

from PIL import Image

imin = Image.open(sys.argv[1])
imout = Image.new("RGB", imin.size)

imout.putdata(map(
                  lambda pixel: (0,0,0) if pixel == (0,0,0) else (255,255,255),
                  imin.getdata()
                 )
             ) 

imout.save(sys.argv[2])

Try using Image.blend() .尝试使用Image.blend() Suppose your image is im .假设您的图像是im

# conversion matrix: any color to white, black to black
mtx = (1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0)
mask = im.convert("L", mtx) # show() it to get the idea
decal = Image.new("RGB", im.size, (0, 0, 255)) # we fill with blue
Image.blend(im, decal, mask).show() # all black turned blue

This must be way faster than per-pixel lambda calls, especially on large images.这必须比每像素 lambda 调用快得多,尤其是在大图像上。

using PIL使用 PIL

c   = color_of_choice
out = im.point(lambda i: c if i>0 else i)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用opencv python仅更改白色的黑色而无像素操作 - How can i change only black color in white using opencv python without pixel operation 如何将黑白像素图像转换为文本? - How do I convert a black and white pixel image to text? 如何使用python获取此图像的某些颜色(RGB)的像素坐标? - How can I get pixel coordinates of certain color(RGB) of this image using python? 如何将图像的像素颜色信息转换为数字并将其存储在CSV中? - How can I convert an image's pixel color information into numbers and store it in a CSV? 如何在pygame的一行中找到每个像素? - How can I find every pixel on a line with pygame? 如何生成确定 python 中每个像素颜色的图像? - How can i generate an image determining the color of each pixel in python? 如何获取桌面上像素的颜色? (Linux)的 - How can I grab the color of a pixel on my desktop? (Linux) 如何去除没有 alpha 层的图像的黑色 bg 颜色 - how can i remove the black bg color of an image with no alpha layer 如何在Emacs中为某些东西着色? - How can I color certain things in Emacs? matplotlib:如何将XYZ散射转换为像素图像? - matplotlib: how can I convert a XYZ scatter to a pixel image?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM