varsha_holla
varsha_holla

Reputation: 862

How to convert BGR image to RGB image using PIL in Python?

I wanted to display the BGR image to RGB, so I followed one of the procedure mentioned in this PIL rotate image colors (BGR -> RGB) still I'm getting the BGR images.

My code is:

i = 0
for i inrange(6):
  img = Image.fromarray(resizelist[val])
  im = img.convert("RGBA")
  resized_img = im.resize((200, 200),Image.ANTIALIAS)
  tkimage1 = ImageTk.PhotoImage(resized_img)
  myvar2 = Label(new, image=tkimage1)
  myvar2.image = tkimage1

Here resizelist[val] contains 70 images. And I'm getting the output, but its in BGR format only.

Thanks in advance!

Upvotes: 1

Views: 5522

Answers (2)

AtotheK
AtotheK

Reputation: 1

An shorter version would be:

from PIL import Image
import numpy as np

path_to_image = 'path_to_image.png'

image_PIL_RGB = Image.open(path_to_image)
image_np_RGB = np.array(image_PIL_RGB)

image_np_BGR = np.flip(image_np_RGB ,-1)
image_PIL_BGR = Image.fromarray(image_np_BGR)

Upvotes: 0

dnlcrl
dnlcrl

Reputation: 5150

Have you tried using numpy as suggested here?

from PIL import Image
import numpy as np
import sys 

sub = Image.open(sys.argv[1])
sub = sub.convert("RGBA")
data = np.array(sub) 
red, green, blue, alpha = data.T 
data = np.array([blue, green, red, alpha])
data = data.transpose()
sub = Image.fromarray(data)

Upvotes: 3

Related Questions