繁体   English   中英

如何将图像转换为 numpy 数组

[英]How to convert images into numpy array

我在将图像转换为数组时遇到了一个问题,在重塑数组时,这显示给我

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

这是我的代码:

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!")

我该如何解决这个问题?

问题是您的数组有 43200 个元素,而您想要的形状 (3, 120, 160) 需要 57600 个元素。 维度的乘积必须等于数组的大小,numpy 才能对其进行整形。

也许如果你将它重塑为 (3, 120, 120) 它会起作用,但我想你需要检查原始图像的尺寸以获得你想要的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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