# Cannot reshape array of size 12288 into shape (64,64)

I have an image I loaded with the image.load_img() function but when I try to reshape it I get this error:

ValueError: cannot reshape array of size 12288 into shape (64,64)


Here is my code:

test_image = image.load_img('xray_dataset_covid19/test/PNEUMONIA/streptococcus-pneumoniae-pneumonia-temporal-evolution-1-day2.jpg'
, target_size = (64, 64))
test_image = image.img_to_array(test_image)
test_image = np.expand_dims(test_image, axis = 0)
result = model.predict(test_image)
if result[0][0] == 1:
img = Image.fromarray(test_image.reshape(64,64) ,'L')
img.show()

• For future questions: the key information that would have helped in the question (that I found in comments below) was the output of test_image.shape. Also I have assumed you were using the PIL library: it helps to show all your imports at the top of the code snippet. stackoverflow.com/help/minimal-reproducible-example is even better! Aug 17 at 8:59

$$64\times 64 = 4096$$. You're short about $$8000$$ pixels.

• If you have a third dimension, say RGB images, with three layers of $64\times 64$, you wind up with the desired number of pixels. I suspect your bug is something like that.
– Dave
Aug 13 at 15:52
• i get this : ValueError: Too many dimensions: 3 > 2. Aug 13 at 15:55
• I don't follow your Python code well enough to be able to diagnose much further, but perhaps your method calls expect 2-tensors (e.g., black and white images) instead of 3-tensors (e.g., RGB images).
– Dave
Aug 13 at 15:57
• when I print(test_image.shape) I get (1, 64, 64, 3) Aug 13 at 16:55

when I print(test_image.shape) I get (1, 64, 64, 3)

What you probably wanted was:

if result[0][0] == 1:
img = Image.fromarray(test_image.reshape(64,64,3))
img.show()


I.e. specify the ,3, because you have RGB data, and drop the ,'L' because that means you have B/W data.

If you actually wanted greyscale or b/w change the last line to one of:

img.convert('L').show()
img.convert('1').show()


Stepping back a bit, you could have used test_image directly, and not needed to reshape it, except it was in a batch of size 1. A better way to deal with it, and not have to explicitly state the image dimensions, is:

if result[0][0] == 1:
img = Image.fromarray(test_image.squeeze(0))
img.show()


squeeze() removes any dimensions of size 1; squeeze(0) avoids surprises by being more specific: if the first dimension is of size 1 remove it, otherwise do nothing.

Yet another way to do it, that ties in with how you use result, is:

if result[0][0] == 1:
img = Image.fromarray(test_image[0])
img.show()