PIL rotate image colors (BGR -> RGB) PIL rotate image colors (BGR -> RGB) python python

PIL rotate image colors (BGR -> RGB)


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

img = img[:,:,::-1]


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 cv2srcBGR = cv2.imread("sample.png")destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB)


Assuming no alpha band, isn't it as simple as this?

b, g, r = im.split()im = Image.merge("RGB", (r, g, b))

Edit:

Hmm... It seems PIL has a few bugs in this regard... im.split() doesn't seem to work with recent versions of PIL (1.1.7). It may (?) still work with 1.1.6, though...