简体   繁体   English

将图像清洁成仅黑色

[英]Cleaning an image to only black

I have an image. 我有一张图片。

I would like to go over that image, pixel by pixel, and any pixel that is not black should be turned to white. 我想逐个像素浏览该图像,任何非黑色的像素都应变为白色。 How do I do this? 我该怎么做呢?

(Python). (蟒蛇)。

Thanks! 谢谢!

The most efficient way is to use the point function 最有效的方法是使用点功能

def only_black(band):
    if band > 0:
        return 255
    return 0
result = im.convert('L').point(only_black)

This is what the PIL documentation has to say about this: 这是PIL文档对此要说的:

When converting to a bilevel image (mode "1"), the source image is first converted to black and white. 当转换为双层图像(模式“ 1”)时,源图像首先转换为黑白图像。 Resulting values larger than 127 are then set to white, and the image is dithered. 然后将大于127的结果值设置为白色,并对图像进行抖动处理。 To use other thresholds, use the point method. 要使用其他阈值,请使用点方法。

You should use the point function, which exists specifically for this reason. 您应该使用为此而专门存在的point函数。

converter= ( (0,) + 255*(255,) ).__getitem__
def black_or_white(img):
    return img.convert('L').point(converter)

You might want to check out the following library: 您可能要签出以下库:

http://effbot.org/imagingbook/image.htm http://effbot.org/imagingbook/image.htm

Especially: 特别:

im.getpixel(xy) => value or tuple

and

im.putpixel(xy, colour)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM