如何使用 PIL 将二值图像转换为 RGB?

How to convert binary image to RGB with PIL?

我有二进制的 PIL 图像,我需要将其转换为 RGB。我做了这个diskew图像

二进制图像

我需要这样:

我已经试过了,但没用

from PIL import Image as im

img = im.fromarray((255 * Image).astype("uint8")).convert("RGB")

我仍然不明白为什么你想要 RGB 就转换为 RGBA,但是这段代码会按照你的要求将你的图像转换为 RGB:

#!/usr/local/bin/python3

import numpy as np
from PIL import Image

# Open input image
im = Image.open('text.png').convert('RGB')

# Invert
npim = 255 - np.array(im)

# Save
Image.fromarray(npim).save('result.png')