gpt4 book ai didi

python - 使用 PIL 将 RGB 图像变成纯黑白图像

转载 作者:IT老高 更新时间:2023-10-28 20:33:46 38 4
gpt4 key购买 nike

我正在使用 Python Imaging Library 进行一些非常简单的图像处理,但是我无法将灰度图像转换为单色(黑白)图像。如果我在将图像更改为灰度 (convert('L')) 后保存,则图像会按照您的预期呈现。但是,如果我将图像转换为单色单波段图像,它只会给我带来噪点,如下图所示。有没有一种简单的方法可以使用 PIL/python 将彩色 png 图像转换为纯黑白图像?

from PIL import Image 
import ImageEnhance
import ImageFilter
from scipy.misc import imsave
image_file = Image.open("convert_image.png") # open colour image
image_file= image_file.convert('L') # convert image to monochrome - this works
image_file= image_file.convert('1') # convert image to black and white
imsave('result_col.png', image_file)

Original Image Converted Image

最佳答案

from PIL import Image 
image_file = Image.open("convert_image.png") # open colour image
image_file = image_file.convert('1') # convert image to black and white
image_file.save('result.png')

产量

enter image description here

关于python - 使用 PIL 将 RGB 图像变成纯黑白图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9506841/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com