问题描述
我正在使用 matplotlib imread 函数从文件系统读取图像.但是,当显示这些图像时,它将更改jpg图像的颜色.[Python 3.5,Anaconda3 4.3,matplotlib2.0]
I'm reading images from filesystem using matplotlib imread function. However, it changes jpg image color when it displays those images. [Python 3.5, Anaconda3 4.3, matplotlib2.0]
# reading 5 color images of size 32x32
imgs_path = 'test_images'
test_imgs = np.empty((5,32,32,3), dtype=float)
img_names = os.listdir('test_images'+'/')
for i, img_name in enumerate(img_names):
#reading in an image
image = mpimg.imread(imgs_path+'/'+img_name)
test_imgs[i] = image
#Visualize new raw images
plt.figure(figsize=(12, 7.5))
for i in range(5):
plt.subplot(11, 4, i+1)
plt.imshow(test_imgs[i])
plt.title(i)
plt.axis('off')
plt.show()
它将为所有图像添加蓝色/绿色调.我在做任何错误吗?
It is adding a bluish/greenish tint to all the images. Any mistake I'm doing?
推荐答案
matplotlib.image.imread
或 matplotlib.pyplot.imread
将图像读取为无符号整数数组.
然后,您将其隐式转换为 float
.
matplotlib.image.imread
or matplotlib.pyplot.imread
read the image as unsigned integer array.
You then implicitely convert it to float
.
matplotlib.pyplot.imshow
以不同的方式解释两种格式的数组.
matplotlib.pyplot.imshow
interpretes arrays in both formats differently.
- 浮点数组在
0.0
(无颜色)和1.0
(全色)之间解释. - 整数数组在
0
和255
之间解释.
- float arrays are interpreted between
0.0
(no color) and1.0
(full color). - integer arrays are interpreted between
0
and255
.
因此您有两个选择:
-
使用整数数组
Use an integer array
test_imgs = np.empty((5,32,32,3), dtype=np.uint8)
在绘制之前将数组除以 255:
divide the array by 255. prior to plotting:
test_imgs = test_imgs/255.
这篇关于matplotlib 更改 jpg 图像颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!