0

Using python xy's spyder code editor.

When saving a list of pixel color values to a numpy array using np.asarray(list), it gives me an array with weird dimensions.

The dimensions of the original image being read is (864,1089) (width,height) and is a JPEG image. When converting this list to a numpy array, it returns an array with dimensions (940896, 3), when I expected a one dimensional array with dimensions (940896,). I don't understand where the 3 came from...

img = Image.open('Images\satellite6.jpg')
pix = img.load()
width = img.size[0]
height = img.size[1]
pixels = []

for j in range(height):
    for i in range(width):
        pixels.append(pix[i,j])

pixels = np.array(pixels)
print np.shape(pixels)

(940896, 3)

I am also new to posting questions, so if anything seems unclear, let me know :)

1 Answer 1

1

Try to see what pix[0,0] contains for example:

>>> img = Image.open('./edgewalker-cat.png')
>>> pix = img.load()
>>> pix
<PixelAccess object at 0x7f1afac932f0>
>>> pix[0,0]
(0, 0, 0)

Its tuple, size of 3, so the pixels list, you are constructing one by one item, will be converted to the dimension of (width*height, 3).

UPD: On grayscaled images, you'll get a plain int value:

>>> img = Image.open('./urban-dove-gray.jpg')
>>> pix = img.load()
>>> pix[0,0]
31
Sign up to request clarification or add additional context in comments.

2 Comments

pix[0,0] returns (255,255,255). What I don't understand is that I've done this with other images and it's always returned an array with one dimension. Is it because it's a bigger image?
Don't think the size matters. You probably did it before with grayscaled images. Try to see the result of pix[0,0] on grayscale image - it will give you a single int.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.