Nirali Khoda
Nirali Khoda

Reputation: 388

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

enter image description here

I need this way:

enter image description here

I already tried this which is not working

from PIL import Image as im

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

Upvotes: 3

Views: 6156

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207873

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')

enter image description here

Upvotes: 3

Related Questions