Python PIL 旋转图像颜色 (BGR -> RGB)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4661557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PIL rotate image colors (BGR -> RGB)
提问by Claudiu
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?
我有一张图片,颜色是 BGR。如何转换我的 PIL 图像以有效地交换每个像素的 B 和 R 元素?
采纳答案by Joe Kington
Assuming no alpha band, isn't it as simple as this?
假设没有alpha波段,是不是就这么简单?
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...
嗯...似乎 PIL 在这方面有一些错误...im.split()似乎不适用于最新版本的 PIL (1.1.7)。它可能 (?) 仍然适用于 1.1.6,不过......
回答by Amber
回答by sberry
This was my best answer. This does, by the way, work with Alpha too.
这是我最好的答案。顺便说一下,这也适用于 Alpha。
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)
回答by Martin Beckett
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.
加载新的 cv2 界面图像现在自动成为 numpy 数组。
但是 openCV cv2.imread() 将图像加载为 BGR,而 numpy.imread() 将它们加载为 RGB。
The easiest way to convert is to use openCV cvtColor.
最简单的转换方法是使用 openCV cvtColor。
import cv2
srcBGR = cv2.imread("sample.png")
destRGB = cv2.cvtColor(srcBGR, cv2.COLOR_BGR2RGB)
回答by user2692263
import cv2
srcBGR = cv2.imread("sample.png")
destRGB = cv2.cvtColor(srcBGR,cv2.COLOR_BGR2RGB)
Just to clarify Martin Beckets solution, as I am unable to comment. You need cv2. in front of the color constant.
只是为了澄清 Martin Beckets 的解决方案,因为我无法发表评论。你需要cv2。前面的颜色不变。
回答by BlaZerTech
Using the ideas explained before... using numpy you could.
使用之前解释过的想法......使用 numpy 你可以。
bgr_image_array = numpy.asarray(bgr_image)
B, G, R = bgr_image_array.T
rgb_image_array = np.array((R, G, B)).T
rgb_image = Image.fromarray(rgb_image_array, mode='RGB')
Additionally it can remove the Alpha channel.
此外,它还可以删除 Alpha 通道。
assert bgra_image_array.shape == (image_height, image_width, 4)
B, G, R, _ = bgra_image_array.T
rgb_image_array = np.array((R, G, B)).T
回答by Peter9192
I know it's an old question, but I had the same problem and solved it with:
我知道这是一个老问题,但我遇到了同样的问题并通过以下方式解决了它:
img = img[:,:,::-1]
回答by Andrei Antonov
im = Image.frombuffer('RGB', (width, height), bgr_buf, 'raw', 'BGR', 0, 0)
回答by azygous
Adding a solution using the ellipsis
使用省略号添加解决方案
image = image[...,::-1]
image = image[...,::-1]
In this case, the ellipsis ...is equivalent to :,:while ::-1inverts the order of the last dimension (channels).
在这种情况下,省略号...相当于:,:while::-1反转最后一个维度(通道)的顺序。

