简体   繁体   中英

How to create numpy array of images

Just a beginner to python.. I'm trying to create array of images.

import cv2
import os
import numpy as np

PATH = os.getcwd()
data_path = PATH + '/data1'
data_path_folder = os.listdir(data_path)

X_data = []

for image in data_path:
   img = cv2.imread(data_path + "/" +image, cv2.IMREAD_COLOR)
   img_resize = cv2.resize(img,(128,128))
   X_data.append(img_resize)

img_data = np.array(X_data)
img_data = img_data.astype('float32')
img_data /=255
print(img_data.shape)

ERROR cv2.error: C:\\projects\\opencv-python\\opencv\\modules\\imgproc\\src\\resize.cpp:4044: error: (-215) ssize.width > 0 && ssize.height > 0 in function cv::resize

It seems a tiny error.... error msg says there are invalid input for resize.

I think you have to get correct image file name from correct list.

import cv2
import os
import numpy as np

PATH = os.getcwd()
data_path = PATH + '/data1'
data_path_folder = os.listdir(data_path)

X_data = []

for image in data_path_folder:  # ADD '_folder'
   img = cv2.imread(data_path + "/" +image, cv2.IMREAD_COLOR)
   img_resize = cv2.resize(img,(128,128))
   X_data.append(img_resize)

img_data = np.array(X_data)
img_data = img_data.astype('float32')
img_data /=255
print(img_data.shape)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM