Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load an image and show the image using keras?

%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?

like image 736
user1159517 Avatar asked Jun 30 '17 08:06

user1159517


People also ask

How do I load an image into Python using keras?

Load the Image In Keras, load_img() function is used to load image. The image loaded using load_img() method is PIL object. Certain information can be accessed from loaded images like image type which is PIL object, the format is JPEG, size is (6000,4000), mode is RGB, etc.


1 Answers

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:

plt.imshow(x/255.)
like image 176
vijay m Avatar answered Oct 11 '22 09:10

vijay m