Python 如何使用keras加载图像并显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44841869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load an image and show the image using keras?
提问by user1159517
%matplotlib inline
from keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
img = np.random.rand(224,224,3)
plt.imshow(img)
plt.show()
img_path = "image.jpeg"
img = image.load_img(img_path, target_size=(224, 224))
print(type(img))
x = image.img_to_array(img)
print(type(x))
print(x.shape)
plt.imshow(x)
I have some code like this which should print the image. But it shows the image in wrong channels. What am i missing here?
我有一些这样的代码应该打印图像。但它在错误的通道中显示图像。我在这里错过了什么?
采纳答案by vijay m
This is a image scaling issue. The input to the imshow() expects it to be in the 0-1 range, while you are passing it a [0-255] range input. Try to view it as:
这是图像缩放问题。imshow() 的输入期望它在 0-1 范围内,而您传递给它的是 [0-255] 范围输入。尝试将其视为:
plt.imshow(x/255.)