Python
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
Go Back   Web Development Archives Mailing Lists Python

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Web Development Archives Sponsor:
  #1  
Old July 1st, 2007, 11:30 AM
elis aeris
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #2  
Old July 1st, 2007, 01:30 PM
Alan Gauld
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #3  
Old July 1st, 2007, 03:30 PM
Roel Schroeven
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #4  
Old July 1st, 2007, 03:30 PM
elis aeris
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #5  
Old July 1st, 2007, 06:00 PM
Roel Schroeven
Guest
Dev Archives Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
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

Reply With Quote
  #6  
Old June 1st, 2008, 05:45 PM
ellesar272 ellesar272 is offline
Registered User
Dev Archives Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 1 ellesar272 User rank is Just a Lowly Private (1 - 20 Reputation Level)  
Time spent in forums: 3 m 24 sec
Reputation Power: 0
Similar Problem

Quote:
Originally Posted by Roel Schroeven
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




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.

Reply With Quote
Reply

Viewing: Web Development Archives Mailing Lists Python > im.getdata()


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek