|
|
|
|
|||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
im.getdata()
Googling "PIL Handbook" yields this page
and, as I said, """ getdata *im.getdata()* =sequence Returns the contents of an image as a sequence object containing pixel values. The sequence object is flattened, so that values for line one follow directly after the values of line zero, and so on. Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations, including iteration and basic sequence access. To convert it to an ordinary sequence (e.g. for printing), use *list(im.getdata())*. How do I find out how the list look like? Is it list[x][y] ? or rather, how did you find out how this internal data type look like? how do I access it ? Tutor maillist - Tutor (AT) python (DOT) org |
|
#2
|
|||
|
|||
|
im.getdata()
"elis aeris" <hunter92383 (AT) gmail (DOT) comwrote
*im.getdata()* =sequence > Returns the contents of an image as a sequence object Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations, including iteration and basic sequence access. How do I find out how the list look like? > Is it list[x][y] ? or rather, how did you find out how this internal data type look like? I'm not sure why you care what it looks like internally, you can use indexing to access the pixels and you can iterate over them. What more do you need? The indexing will be one dimensional since the help page told you that the data was flattened. Thus 123 456 789 becomes 123456789 But mostly you won't care because you can iterate over the list using a standard for loop. And as ever in Python the easiest way to find out how things work is just to try it in the interpreter. Use a small bitmap (16x16 say?) and load it into a PIL image, use getdata and see what it looks like when you index it, iterate over it and print it. Working with the interpreter can eliminate most uncertainties when working with Python. HTH, -- Alan Gauld Author of the Learn to Program web site Tutor maillist - Tutor (AT) python (DOT) org |
|
#3
|
|||
|
|||
|
im.getdata()
elis aeris schreef:
Googling "PIL Handbook" yields this page <> and, as I said, """ getdata *im.getdata()* =sequence If you're using PIL >= 1.1.6, there's also a pixel access object you can use (the documentation is on that same page, in the 'load' section: "(New in 1.1.6) In 1.1.6 and later, load returns a pixel access object that can be used to read and modify pixels. The access object behaves like a 2-dimensional array, so you can do: pix = im.load() print pix[x, y] pix[x, y] = value Access via this object is a lot faster than getpixel and putpixel." Perhaps that's more useful to you? A lot depends on exactly what you want to do with the pixel values. I think you would do everyone (including yourself) a favor by just telling us what you're going to do with the pixel values. -- If I have been able to see further, it was only because I stood on the shoulders of giants. -- Isaac Newton Roel Schroeven Tutor maillist - Tutor (AT) python (DOT) org |
|
#4
|
|||
|
|||
|
im.getdata()
I am capturing a screen shot, but then the image was already
image, how do I .load it ? it's not image.load() import time import ImageGrab # Part of PIL from ctypes import * class RECT(Structure): _fields_ = [ ('left', c_ulong), ('top', c_ulong), ('right', c_ulong), ('bottom', c_ulong) ] GetForegroundWindow = GetWindowRect = windll.user32.GetWindowRect rect = RECT() foreground_window = GetForegroundWindow() GetWindowRect(foreground_window, byref(rect)) image = ImageGrab.grab((rect.left, rect.top, rect.right, rect.bottom)) print time.time() pix = image.load() print pix[x, y] x = 0 y = 0 for x in xrange(1,1024,1): for y in xrange(1,768,1): print pix[10,10] Tutor maillist - Tutor (AT) python (DOT) org |
|
#5
|
|||
|
|||
|
im.getdata()
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 Tutor maillist - Tutor (AT) python (DOT) org |
|
#6
|
|||
|
|||
|
Similar Problem
Quote:
I am having a similar problem. I need to iterate over all the pixels in the image in order to modify them. How do I get the range in order to do a for loop over them. I hope this thread is still active. |
![]() |
| Viewing: Web Development Archives > Mailing Lists > Python > im.getdata() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|