PIL rotate image colors (BGR -> RGB)

Solution 1:

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

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

Solution 2:

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)

Solution 3:

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...

Solution 4:

Adding a solution using the ellipsis

image = image[...,::-1]

In this case, the ellipsis ... is equivalent to :,: while ::-1 inverts the order of the last dimension (channels).