Rory Lester
Rory Lester

Reputation: 2918

Python get image matrix PIL

i am trying to get to load an image, convert it and print the matrix. I have the following code ;

im = Image.open("1.jpg")
im = im.convert("L")
print im

when i print 'im' i get this <PIL.Image.Image image mode=L size=92x112 at 0x2F905F8> . How can i get to see the image matrix?

Upvotes: 11

Views: 28165

Answers (3)

MatthieuW
MatthieuW

Reputation: 2372

Function load will give you access to pixels like this:

b = im.load()
print b[x,y]
b[x,y] = 128    # or a tupple if you use another color mode

Upvotes: 5

Blender
Blender

Reputation: 298256

You can use numpy.asarray():

>>> import Image, numpy
>>> numpy.asarray(Image.open('1.jpg').convert('L'))

Upvotes: 17

mhawke
mhawke

Reputation: 87084

im.show() will display it in a pop-up window.

im.tostring() will dump the image as a byte string.

im.save() to save it to a file.

Upvotes: 2

Related Questions