Skip to main content
 首页 » 编程设计

python之将图像清理成只有黑色

2025年05月04日10Renyi-Fan

我有一张图片。

我想逐个像素地检查该图像,任何不是黑色的像素都应该变成白色。我该怎么做?

( python )。

谢谢!

请您参考如下方法:

最有效的方法是使用点函数

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

这就是PIL documentation不得不说:

When converting to a bilevel image (mode "1"), the source image is first converted to black and white. Resulting values larger than 127 are then set to white, and the image is dithered. To use other thresholds, use the point method.