Python 反转二值图像; .invert() 失败

标签 python image python-2.7 numpy python-imaging-library

我有一个二值图像,尺寸为 64x63,其中每个像素都是 1 或 0。

import struct
from PIL import Image
import numpy

...

i1 = Image.frombytes('1', (64, 63), r[3], 'raw')

我怎样才能反转这个图像?

编辑

我尝试了建议的解决方案:

from PIL import Image
import PIL.ImageOps    

i1 = PIL.ImageOps.invert(i1)

然而,这导致了错误:

raise IOError("not supported for this image mode")
IOError: not supported for this image mode

我认为这是因为图像既不是 RGB 也不是 L(灰度)。相反,它是一个二进制图像文件,其中每个像素只有 0 或 1。

最佳答案

另一种选择是在反转之前将图像转换为支持的反转格式,即:

import PIL.ImageOps
inverted_image = PIL.ImageOps.invert( image.convert('RGB') )

关于Python 反转二值图像; .invert() 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41421033/

相关文章:

python - 使用版本控制 : versioned url retrieval 的 DRF 测试 View

python - 如何在 python 中使用 Mosquitto 发布文件?

c++ - 如何从 python 中包装的 c++ 类中读取列表<对象>

python - 如何从 Python 脚本在终端中执行命令?

python - 删除字符串中包含除 A、C、T、G、N 之外的其他字母的行

python - Pandas get_dummies 使用分隔符 = ' ' 和字符级别分隔创建一个热

html - 我需要在 img2 上使用这些样式吗?

image - 有没有办法在不读取整个文件的情况下推断文件是什么图像格式?

.net - 在 vb.net 应用程序中打开图像后无法删除图像

rundeckrun 的 Python 2.6 兼容性