简体   繁体   中英

How to convert images into numpy array

I faced a problem when converting images into array, when reshaping the array this shows to me

ValueError: cannot reshape array of size 43200 into shape (3,120,160)

This my code:

img_size = 224
i = 0
for _file in train_files:
    img = load_img(data_dir + "/" + _file , target_size=(224,224))  # this is a PIL imageflowers[0], target_size=(224,224))
    img.thumbnail((image_width, image_height))
    # Convert to Numpy Array 
    x = img_to_array(img)  
    x = x.reshape((3, 120, 160))
    # Normalize
    x = (x - 255.0) / 255.0
    dataset[i] = x
    i += 1
    if i % 250 == 0:
        print("%d images to array" % i)
print("All images to array!")

How can I resolve this?

The problem is that your array has 43200 elements, and the shape that you want (3, 120, 160) would require 57600 elements. The product of the dimensions has to be equal to the size of the array for numpy to be able to reshape it.

Maybe if you reshape it to (3, 120, 120) it would work, but i guess you need to check the dimensions of the original image to get the results you want.

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