Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 677
Rory Lester Avatar asked Sep 03 '12 07:09

Rory Lester


2 Answers

You can use numpy.asarray():

>>> import Image, numpy
>>> numpy.asarray(Image.open('1.jpg').convert('L'))
like image 109
Blender Avatar answered Oct 19 '22 01:10

Blender


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
like image 35
MatthieuW Avatar answered Oct 19 '22 03:10

MatthieuW