简体   繁体   中英

How to convert binary image to RGB with PIL?

I have PIL Image in binary and I need to convert it in RGB. I did this diskew image

binary image

在此处输入图片说明

I need this way:

在此处输入图片说明

I already tried this which is not working

from PIL import Image as im

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

I still don't understand why you convert to RGBA if you want RGB, but this code converts your image to RGB as you ask:

#!/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')

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM