Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PIL rotate image colors (BGR -> RGB)

I have an image where the colors are BGR. How can I transform my PIL image to swap the B and R elements of each pixel in an efficient manner?

like image 923
Claudiu Avatar asked Jan 11 '11 19:01

Claudiu


People also ask

Does PIL use RGB or BGR?

But for PIL, the input is RGB, while it's BGR for cv2.

How do I change BGR to RGB?

We can use cvtColor() method to convert a BGR image to RGB and vice-versa. Parameter: cv2. COLOR_BGR2RGB – BGR image is converted to RGB.

How do I change RGB to BGR in Python?

The parameter code when converting from RGB to BGR is cv2. COLOR_RGB2BGR . Use this when reading an image file as a PIL. Image , convert it to ndarray , and save it using OpenCV imwrite() .

What is the difference between RGB and BGR?

What's the Difference between RGB versus BGR? The main difference between RGB versus BGR is the arrangement of the subpixels for Red, Green, and Blue. RGB is arranged like that, but BGR is essentially in reverse with no adverse effect on color vibrancy and accuracy.


2 Answers

I know it's an old question, but I had the same problem and solved it with:

img = img[:,:,::-1] 
like image 79
Peter9192 Avatar answered Oct 10 '22 00:10

Peter9192


Just to add a more up to date answer:

With the new cv2 interface images loaded are now numpy arrays automatically.
But openCV cv2.imread() loads images as BGR while numpy.imread() loads them as RGB.

The easiest way to convert is to use openCV cvtColor.

import cv2 srcBGR = cv2.imread("sample.png") destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB) 
like image 29
Martin Beckett Avatar answered Oct 10 '22 02:10

Martin Beckett