详解OpenCV和PIL读取和显示图像的差异
作者:鸡啄米的时光机 时间:2021-07-06 22:17:22
本博客演示使用OpenCV和PIL读取和显示图像的差异。
首先来看一下原始的bgr图像 src.jpg
1. 使用cv2读取src.jpg并转为rgb格式的src_rgb.jpg并显示和保存
#首先读入并保存为rgb图像
src_path = 'C:\\Users\\Administrator\\Desktop\\cv2_PIL\\src.png' # bgr
img = cv2.imread(src_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
cv2.imshow('src_rgb.png', img)
cv2.imwrite('src_rgb.png', img)
cv2.waitKey(5000)
2. 使用cv2读取src_rgb.jpg并显示, 发现仍为rgb格式
# 读入rgb格式的图像并保存
src_path = 'C:\\Users\\Administrator\\Desktop\\cv2_PIL\\src_rgb.png' # rgb
img = cv2.imread(src_path)
cv2.imshow('src_rgb.png', img)
cv2.waitKey(5000)
由此可见cv2读取bgr格式图片为bgr格式读入;读取rgb格式图片为rgb格式读入。
3. 使用PIL读取src.jpg并显示
# 使用PIL读取bgr格式图片并显示
src_path = 'C:\\Users\\Administrator\\Desktop\\cv2_PIL\\src.png' # bgr
img = Image.open(src_path)#.convert("RGB")
print(img.mode)
img.show("src.png")
由此可见PIL读入bgr格式图片为读入后是rgb格式
4. 使用PIL读取src.jpg并转为numpy的array格式图片并显示
# 使用PIL读取bgr格式图片并显示
src_path = 'C:\\Users\\Administrator\\Desktop\\cv2_PIL\\src.png' # bgr
img = Image.open(src_path)#.convert("RGB")
print(img.mode)
img = np.array(img) # rgb
cv2.imshow("src.png",img)
cv2.waitKey(5000)
所以进一步坐实了PIL读入bgr格式图片为读入后是rgb格式
5. 使用PIL读取rgb格式src_rgb.jpg并显示
# 使用PIL读取rgb格式图片并显示
src_path = 'C:\\Users\\Administrator\\Desktop\\cv2_PIL\\src_rgb.png' # rgb
img = Image.open(src_path)#.convert("RGB")
print(img.mode)
# img1 = img.load()
# print(img1[0,0])
#img.show()
img = np.asarray(img) # bgr
#print(img)
cv2.imshow("src_.png",img)
cv2.waitKey(5000)
可见PIL读入rgb顺序图片后虽然为RGB格式,但是其实通道顺序为BGR格式图片
总结:
cv2读取BGR图片 | img = cv2.imread('src.png'),其中img为BGR通道顺序的图片 |
cv2读取RGB图片 | img = cv2.imread('src.png'),其中img为RGB通道顺序的图片 |
PIL读入BGR图片 | img = Image.open('src.png'),显示img为RGBA格式,其中img为RGBA通道顺序的图片; img = img.convert('RGB')后,img为RGBA通道顺序的图片 |
PIL读入RGB图片 | img = Image.open('src_rgb.png'),虽然显示img为RGB格式,但是img为通道顺序为BGR格式的图片。 |
来源:https://blog.csdn.net/qq_33221533/article/details/115839964
标签:OpenCV,PIL,读取,显示
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python性能优化的20条建议
2021-05-20 15:24:12
tensorflow可视化Keras框架中Tensorboard使用示例
2023-08-09 01:39:27
![](https://img.aspxhome.com/file/2023/2/64692_0s.png)
几个SQL SERVER应用问题解答
2008-01-01 19:12:00
python 通过dict(zip)和{}的方式构造字典的方法
2023-10-03 00:05:12
Python字符串拼接六种方法介绍
2021-07-30 01:34:14
Oracle 自增(auto increment) 或 标识字段的建立方法
2009-03-06 11:15:00
go语言csrf库使用实现原理示例解析
2023-08-07 03:34:38
![](https://img.aspxhome.com/file/2023/7/97167_0s.jpg)
如何才能有效对抗MySQL数据库的解密高手
2009-05-22 18:21:00
支持PyTorch的einops张量操作神器用法示例详解
2023-10-17 23:13:06
Python OpenCV去除字母后面的杂线操作
2023-08-02 15:18:47
![](https://img.aspxhome.com/file/2023/7/60947_0s.jpg)
python中几个常用函数的正确用法-lambda/filter/map/reduce
2022-02-17 14:52:45
详解python使用金山词霸的翻译功能(调试工具断点的使用)
2021-06-27 03:33:52
![](https://img.aspxhome.com/file/2023/7/111907_0s.png)
ORCAL 临时创建表与删除表
2023-07-12 19:30:05
使用apidoc管理RESTful风格Flask项目接口文档方法
2022-11-24 10:05:14
![](https://img.aspxhome.com/file/2023/8/75888_0s.jpg)
Python实现分数序列求和
2023-10-07 12:44:54
用python计算文件的MD5值
2023-06-20 21:28:27
Asp生成RSS的类_给网站加上RSS
2011-04-19 11:06:00
Pyhon Flask框架:第一个Flask程序
2023-01-09 16:06:34
![](https://img.aspxhome.com/file/2023/3/81833_0s.png)
解决TensorFlow训练模型及保存数量限制的问题
2022-08-06 08:22:27
asp如何读取服务器上的驱动器?
2009-11-18 20:50:00