使用PIL将RGB图像变成纯黑白图像 [英] Using PIL to turn a RGB image into a pure black and white image

查看:65
本文介绍了使用PIL将RGB图像变成纯黑白图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using the Python Imaging Library for some very simple image manipulation, however I'm having trouble converting a greyscale image to a monochrome (black and white) image. If I save after changing the image to greyscale (convert('L')) then the image renders as you would expect. However, if I convert the image to a monochrome, single-band image it just gives me noise as you can see in the images below. Is there a simple way to take a colour png image to a pure black and white image using PIL / python?

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)

推荐答案

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

收益

这篇关于使用PIL将RGB图像变成纯黑白图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆