I tried various things such as loading and manipulating images in keras.preprocessing.image.
You can load the image using the load_img function. Since the image is read in PIL format, it is necessary to convert it to numpy array type when using it for NN training, prediction, etc.
python
import keras
keras.preprocessing.image.load_img(
path, grayscale=False, color_mode="rgb", target_size=None, interpolation="nearest"
)
argument | Description | How to specify |
---|---|---|
path | Image path(Absolute or relative) | Path string |
grayscale | Grayscale setting but officially deprecated, color_Should be specified in mode | True or False |
color_mode | You can specify the format of the loaded image.'grayscale','rgb','rgba'There isDefaultIs'rgb' | 'grayscale'Or'rgb'Or'rgba' |
target_size | Specify the size of the image to be read with an int tuple | ( width , height ) |
interpolation | How to complement the image. Original image and target_Complement the image when there is a difference in size."nearest", "bilinear", "bicubic", "lanczos", "box", "hamming"Types of complementation methods are supported.DefaultThen"nearest" | "nearest"Or"bilinear"Or"bicubic"Or"lanczos"Or"box"Or"hamming" |
Loading images
from keras.preprocessing.image import load_img
import matplotlib.pyplot as plt
#Basically path,color_code,target_Just set the size
#The loading itself is completed only here!
img = load_img("taiju.jpg ", color_mode='rgb', target_size=(700,700))
array = img_to_array(img)
gray_img = load_img("taiju.jpg ", color_mode='grayscale', target_size=(28,28))
gray_array = img_to_array(gray_img)
testlist=[]
testlist.append(array)
testlist.append(gray_array)
imglist=[]
imglist.append(img)
imglist.append(gray_img)
#Format when used in neural networks using mnist
print("array format:",array.shape)
print("gray_array format:",gray_array.shape)
#Displaying images using matplot
for i in range(2):
plt.subplot(1,5,i+1)
plt.imshow(imglist[i],'gray')
plt.show
When you do this, you get: The first image is the image read with ** img = load_img ("taiju.jpg ", color_mode ='rgb', target_size = (700,700)) **, and is read with the original image size of 700 pixels in full color. Because it is, it is displayed clearly. The second image is the image read with ** gray_img = load_img ("taiju.jpg ", color_mode ='grayscale', target_size = (28,28)) **, converted to grayscale and reduced to 28 pixels vertically and horizontally. Is displayed. Therefore, the image looks unclear and rough. In addition, it can be converted to numpy array type by using ** img_to_array ** function, and it can be used for prediction by model created by neural network.
Recommended Posts