回答问题

我正在使用 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)

原图转换后的图像

Answers

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')

产量

在此处输入图像描述

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐