Convert image to array with same dimensions as image resolution

  arrays, numpy, python, python-imaging-library

I am trying to convert an image into an array where, for example, if I have an image of 20×20 pixels I would like to have an array of 20 rows and 20 columns.
I would like to have each unique colour to be represented by a number.
So basically this code but with unique numbers ( could be an r, g, or b value ) instead of zeros.

import numpy as np
from PIL import Image

img = np.asarray(Image.open('test.png'))

Y = np.array(np.zeros((20,20)))

print(Y)

I know this is incorrect, but it gives the exact visual layout I want to achieve, except that I would like to put a comma instead of full stop between each item.

Then this code gives me the correct values of the image but I cannot figure out how to get it into that format like above:

from PIL import Image
from numpy import asarray


img = Image.open('test.png')
arraydata = asarray(img)


print(arraydata)  

Any pointers are much appreciated.

Source: Python Questions

LEAVE A COMMENT