6 formas de leer imágenes en Python y convertirlas en datos numpy.ndarray ()

 

import numpy as np
import cv2
from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img
from PIL import Image
import skimage.io as io
import matplotlib.pyplot as plt
import matplotlib.image as mpig
 
 
'' '
方式 : :
OpenCV : np.ndarray
PIL PIL.JpegImagePlugin.JpegImageFile
keras.preprocessing.image PIL.JpegImagePlugin.JpegImageFile
Skimage.io np.ndarray
matplotlib.pyplot np.ndarray
matplotlib.image np.ndarray
'' '/
 
Path = " trainSet / bus / 300.jpg "
 
 
'''
Método 1: Use OpenCV
'' '
img1 = cv2.imread (imagePath)
print ("img1:", img1.shape)
print ("img1:", type (img1))
print ("-" * 10)
 
 
' ''
Método 2: Use PIL
'' '
img2 = Image.open (imagePath)
print ("img2:", img2)
print ("img2:", escriba (img2))
#convertir al formato np.ndarray
img2 = np.array ( img2)
print ("img2:", img2.shape)
print ("img2:", escribe (img2))
print ("-" * 10)
 
 
'' '
Método 3: Usa keras.preprocessing.image
' ''
img3 = load_img (imagePath)
print ("img3:", img3)
print ("img3:", escribe (img3)) #Convertir
a np.formato ndarray, use np.array (), o use img_to_array () en keras #Use
np.array ()
# img3 = np.array (img2)
# 使用 keras 里 的 img_to_array ()
img3 = img_to_array (img3)
print ("img3:", img3.shape)
print ("img3:", escriba (img3))
print ("-" * 10)
 
 
'' '
方式 四: 使用 Skimage.io
'' '
img4 = io.imread (imagePath)
print ("img4:", img4.shape)
print ("img4:", type (img4))
print ("-" * 10)
 
 
' ''
方式 五 : 使用 matplotlib.pyplot
'' '
img5 = plt.imread (imagePath)
print ("img5:", img5.shape)
print ("img5:", type (img5))
print ("-" * 10)
 
 
' ''
方式 六 : 使用 matplotlib.image
'' '
img6 = mpig.imread (imagePath)
print ("img6:", img6.shape)
print ("img6:", escribe (img6))
print ("-" * 10)

Supongo que te gusta

Origin blog.csdn.net/xkx_07_10/article/details/100887736
Recomendado
Clasificación