Ask coding questions

← Back to all posts
Upload image as a pixel matris
reutersward (0)

Hi!
I want to upload an image from my computer, and get the corresponding pixel matrix. No idea how to start.

Commentshotnewtop
mwilki7 (186)

Try

from PIL import Image
im = Image.open('Saga.jpg')
pixelMap = im.load()

img = Image.new( im.mode, im.size)
pixelsNew = img.load()

for i in range(img.size[0]):
    for j in range(img.size[1]):
        # your code here

img.show()

Technically pixelsNew is an N by 1 matrix, but it gets you the pixel data which you can then put it in whatever sized matrix you want.
https://stackoverflow.com/questions/38198379/how-to-change-the-pixel-values-of-an-image

Masy (21)

Exactly what Mwilki Said, that should work, Mark them as the correct answer :)