matplotlib库与numpy库数据的相互转换
matplotlib库中读取图片后直接就是numpy中np.array数据
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
img_matplotlib = mpimg.imread('./1.png')
print("img_matplotlib的类型:",type(img_matplotlib))
print("img_matplotlib的维度:",img_matplotlib.shape)
print("img_matplotlib的数据类型:",img_matplotlib.dtype)
print("访问具体坐标:",img_matplotlib[200,100,:])
print("访问具体坐标:",img_matplotlib[200,100,:]*255)
plt.imshow(img_matplotlib)
plt.show()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
注意
- matplotlib读取图片后直接是
np.array数据 - 不同于PIL库,数据类型不是
np.uint8,而是np.float32,是已经将像素值归一化(除以255)之后的值(对应于PIL库的讲解:PIL库Image类型与Numpy类型转换)会发现PIL库中[200,100,:]的像素值为[84 64 39],在matplotlib中访问到的像素为[0.32941177 0.2509804 0.15294118]
![]()
Last、参考文献
【从矩阵到图像的类型转换4】:PIL库Image类型与Numpy类型转换_呆呆象呆呆的博客-CSDN博客
python中plt.imshow(img)显示不了图片_hjxu2016的博客-CSDN博客
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树科学计算基础软件包NumPyNumPy概述416632 人正在系统学习中
评论记录:
回复评论: