【发布时间】:2012-03-19 09:28:17
【问题描述】:
我正在使用 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)
【问题讨论】:
-
来自PIL documentation: """当转换为双层图像(模式“1”)时,源图像首先转换为黑白。结果值大于127然后设置为白色,并且图像抖动。要使用其他阈值,请使用点方法。"""这听起来很相关,但我不熟悉 PIL 和图像处理。
标签: python python-imaging-library python-2.7