PIL旋转图像颜色(BGR-> RGB) [英] PIL rotate image colors (BGR -> RGB)

查看:2577
本文介绍了PIL旋转图像颜色(BGR-> RGB)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像,其中的颜色是BGR。如何转换我的PIL图像以有效的方式交换每个像素的B和R元素?

解决方案

band,不是这么简单吗?

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

/ p>

嗯...似乎PIL在这方面有一些错误... im.split()似乎与最新版本的PIL(1.1.7)一起工作。它可能(?)仍然使用1.1.6,虽然...



或者,如果你有numpy可用,你可以使用它:
(再次,我在此处假设为RGB图像,不是 RGBA图像!):

  data = np.asarray(im)
im = Image.fromarray(np.roll(data,1,axis = -1))

如果 im.split()只是工作,它肯定是一个更简单,更可读的选项!


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?

解决方案

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

Alternatively, if you have numpy available, you can use it to do this: (Again, I'm assuming an RGB image here, not an RGBA image!):

data = np.asarray(im)
im = Image.fromarray(np.roll(data, 1, axis=-1))

If im.split() would just work, it's certainly a simpler and more readable option!

这篇关于PIL旋转图像颜色(BGR-> RGB)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆