我有一个二进制图像,尺寸为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。

最佳答案

如果您愿意将i1转换为numpy数组,则可以执行以下操作

i1 = 1 - numpy.asarray(i1)

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

10-12 00:23
查看更多