-1

the following:

image = Image.open(name, 'r')
data = np.array(image.getdata())
data.reshape(image.size)

returns:

Traceback (most recent call last):
  File "/home/usr/colorviewer/main.py", line 207, in <module>
    print(getPalette())
  File "/home/usr/colorviewer/main.py", line 152, in getPalette
    data.reshape(image.size)
ValueError: cannot reshape array of size 921600 into shape (640,480)

why does it say the array size is 921600 instead of 307200 as the size would suggest, and how might the image data be reshaped into its normal resolution?

PING
  • 11
  • 2
  • every pixel uses three values `R,G,B` and `640*480*3` gives `921600` bytes. And image can be transparent then it may have `640*480*4` bytes. So you need reshape into `(640,480,3)`. – furas Sep 01 '22 at 01:29

1 Answers1

0

Every pixel uses three values R,G,B so you have 640*480*3 which gives 921600 bytes.

So you need reshape into (640,480,3). And it needs * to unpack size.

data.reshape(*image.size, 3)

If image can be transparent then it may have 640*480*4 bytes. And it can be safer to reshape (640,480,-1) and it should automatically ise 3 or 4

data.reshape(*image.size, -1)

Or you should skip .getdata() and you get already reshaped array

data = np.array(image)
furas
  • 134,197
  • 12
  • 106
  • 148