I am reading thousands of images (all three channels), one by one, in form of a numpy ndarray and append them to a list. At the end I want to convert this list into a numpy array:
import numpy as np
from PIL import Image
def read_image_path(path, img_size=227):
img = Image.open(path)
img = np.array(img.resize([img_size, img_size]))
return img
I read each image path from a dictionary that looks like:
{1:{'img_path': 'path-to-image', 'someOtherKeys':'...'}, 2:{...}}
images = []
for key in key:
img = read_image_path(dataset_dictionary[key]['img_path'])
images.append(img)
Up to here it's all fine. I have a list of ndarray image matrices of size (227,227,3). But when I try to convert "images" to numpy array and return it from the function, it gives the following error:
return np.array(images)
return np.array(images)
ValueError: could not broadcast input array from shape (227,227,3) into shape (227,227)
I will be grateful to have anyone's idea about this.
Most likely you have a img (or images) which has the shape of (227,227) instead of (227,227,3).
The following code should tell you which image is the offender.
for key in key:
img = read_image_path(dataset_dictionary[key]['img_path'])
if img.shape != (227,227,3):
print(key)
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.