[Tutor] im.getdata()

Roel Schroeven rschroev_nospam_ml at fastmail.fm
Sun Jul 1 21:42:32 CEST 2007

elis aeris schreef:
> I am capturing a screen shot, but then the image was already  
> image,   how do I .load it ?
> it's not image.load()

I see. In that case, you can't use the method I described; that only 
works if you load the image from disk (as far as I know). I think you'd 
better use getdata() as Alan described.

Something like this, if you need to access the pixels column-by-column 
(Is that really what you want? You still haven't told us):

# ...
image = ImageGrab.grab ((rect.left, rect.top, rect.right, rect.bottom))
pixels = image.getdata()
for x in xrange(0, 1024):
   for y in xrange(0, 768):
     print pixels[x + y * 768]

Note: pixel indices start from 0, not from 1.

If you want to access them row-by-row (as is generally more often the 
case) and you don't have a special need to have the x and y available:

image = ImageGrab.grab ((rect.left, rect.top, rect.right, rect.bottom))
for pixel in image.getdata():
   print pixel

Anyway, you're printing a lot of pixels (786432 actually)... is that 
really what you want?

If I have been able to see further, it was only because I stood
on the shoulders of giants.  -- Isaac Newton

Roel Schroeven

More information about the Tutor mailing list