Python matplotlib更改jpg图像颜色

Python matplotlib更改jpg图像颜色,python,python-3.x,matplotlib,imread,Python,Python 3.x,Matplotlib,Imread,我正在使用matplotlib imread函数从文件系统读取图像。但是,当显示这些图像时,它会更改jpg图像的颜色。[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'+'

我正在使用matplotlib imread函数从文件系统读取图像。但是,当显示这些图像时,它会更改jpg图像的颜色。[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()

它为所有图像添加了蓝色/绿色色调。有什么错误吗?

matplotlib.image.imread
matplotlib.pyplot.imread
将图像作为无符号整数数组读取。
然后将其隐式转换为
float

matplotlib.pyplot.imshow
以不同的格式解释数组

  • 浮点数组在
    0.0
    (无颜色)和
    1.0
    (全色)之间进行解释
  • 整数数组在
    0
    255
    之间进行解释
因此,您有两个选择:

  • 使用整数数组

    test_imgs = np.empty((5,32,32,3), dtype=np.uint8)
    
  • 将数组除以255。绘图前:

    test_imgs = test_imgs/255.
    

  • Matplotlib以RGB格式读取图像,而如果使用opencv,它将以BGR格式读取图像。 首先在RGB中转换.jpg图像,然后尝试显示它。
    它对我很有用。

    我使用了:test\u imgs[I]=(mpimg.imread(imgs\u path+'/'+img\u name)*255)。astype('uint8')我认为这没有意义。imread为您提供一个整数类型的图像,其值介于0和255之间。如果你再乘以255,你会得到太大的值。