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

时间:2018-09-19 06:34:29

标签: python-3.x opencv python-imaging-library cv2

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

二进制图片

enter image description here

我需要这种方式:

enter image description here

我已经尝试过了,这不起作用

from PIL import Image as im

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

1 个答案:

答案 0 :(得分:2)

我仍然不明白为什么要使用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')

enter image description here

相关问题