我正在使用Python图像库进行一些非常简单的图像处理,但是我在将灰度图像转换为单色(黑白)图像时遇到了问题。如果在将图像更改为灰度(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解决方案:
只是
你得到一个抖动的图像。
从左到右输入图像、黑白转换结果和抖动结果:
您可以单击图像以查看未缩放版本。
收益率
另一个选项(在需要使用分段遮罩时,这对于科学目的很有用)是简单地应用阈值:
对于
./binarize.py -i convert_image.png -o result_bin.png --threshold 200
,它看起来是这样的:相关问题 更多 >
编程相关推荐