簡體   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