无法将BMP图像数据格式化为PIL中的每个像素1位 [英] Can't format BMP image data to 1 bit per pixel in PIL

查看:85
本文介绍了无法将BMP图像数据格式化为PIL中的每个像素1位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个黑白bmp图像,我需要一次访问1个像素.我在网上找到了以下代码,但是不是每个位的值为1或0,而是255或0.

I have a black and white bmp image that I need to access 1 pixel at a time. I've found the following code online, but instead of each bit having a value of 1 or 0, its 255 or 0.

import Image
img = Image.open(filename)
img = img.convert('1')
pix = img.load()

我自己逐像素进行转换非常慢.

Doing the conversion myself, pixel by pixel, is extremely slow.

有人可以告诉我为什么上述方法不起作用吗?

can someone tell my why the above methods aren't working?

推荐答案

您可以像这样转换它:

img.point(lambda x: bool(x))

但是为什么首先需要将其设置为0/1(而不是0/255)?

But why do you need it to be 0/1 (rather than 0/255) in the first place?

这篇关于无法将BMP图像数据格式化为PIL中的每个像素1位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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