943,708 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 1638
  • Python RSS
You are currently viewing page 1 of this multi-page discussion thread
May 13th, 2009
0

Locating pixels 'x' position in image

Expand Post »
I'm trying to locate the 'x' positions of certain pixels. Here's and example image:
http://img404.imageshack.us/img404/3418/10x.png

if I run this code it gives me the 88 'odd' pixels that I'm looking for:
Python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2.  
  3. i = Image.open('10x.bmp') # I save it as a bmp
  4. for pixel in i.getdata():
  5. if pixel [2] > 0:
  6. print pixel

Now that I have the pixels, I can't figure out how to locate the 'x' positions. How is this done?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 13th, 2009
0

Re: Locating pixels 'x' position in image

I can't seem to find a way to get the width of the image (which is a little retarded).

If you can find the width and I'm just a giant noob, then you can code it something like this:
python Syntax (Toggle Plain Text)
  1. x = 0
  2. y = 0
  3. for pixel in i.getdata():
  4. x += 1
  5. print x
  6.  
  7. if x == width:
  8. x = 0
  9. y += 1

But then, being a giant noob, what do I know?
Featured Poster
Reputation Points: 975
Solved Threads: 140
Posting Virtuoso
scru is offline Offline
1,624 posts
since Feb 2007
May 13th, 2009
0

Re: Locating pixels 'x' position in image

Yeah its not too hard to find the size.. There isnt a function but when you open an image it stores it in the size variable.

So
python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2.  
  3. f = Image.open("10x.png")
  4. print f.size

Hope that helps
Last edited by Paul Thompson; May 13th, 2009 at 6:01 pm.
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
May 13th, 2009
1

Re: Locating pixels 'x' position in image

The easiest way is to create a dictionary where the key is the (x,y) location tuple and the value is the (r,g,b) color tuple of the pixel:
python Syntax (Toggle Plain Text)
  1. #!/usr/bin/env python
  2.  
  3. # image data analysis using PIL, keep track of pixel value
  4. # (r,g,b) and location (x,y) with a (x,y):(r,g,b) dictionary
  5.  
  6. from PIL import Image
  7.  
  8. # the french_flag test file is a 24X16 bitmap and
  9. # has a simple blue-white-red, left to right pattern
  10. img = Image.open("french_flag.bmp")
  11. width, height = img.size
  12.  
  13. #print width, height
  14.  
  15. pixel_list = list(img.getdata())
  16.  
  17. # create a (x,y):(r,g,b) dictionary
  18. # where (x,y) are the location coordinates an image point
  19. # and (r,g,b) is the value of the pixel at that point
  20. pixel_dict = {}
  21. ix = 0
  22. # rows
  23. for y in range(height):
  24. # columns
  25. for x in range(width):
  26. pixel_dict[(x, y)] = pixel_list[ix]
  27. #print x, y, ix
  28. ix += 1
  29.  
  30. # testing ...
  31. # show contents of the first row
  32. y = 0
  33. for x in range(width):
  34. print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )
  35.  
  36. '''
  37. print('-'*20)
  38.  
  39. # show contents of the dictionary sorted by (x, y) coordinates
  40. for k, v in sorted(pixel_dict.items()):
  41. print k, v
  42. '''
  43.  
  44. print('-'*20)
  45.  
  46. # get the pixel (r, g, b) value at point (x, y)
  47. x = 1
  48. y = 10
  49. print(pixel_dict[(x, y)])
Attached Images
File Type: bmp french_flag.bmp (1.2 KB, 29 views)
Last edited by sneekula; May 13th, 2009 at 8:02 pm. Reason: image attached
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
May 14th, 2009
0

Re: Locating pixels 'x' position in image

Thanks for the replies. Sneekula, that's along the lines of what I'm after but when I run that on the image I have, it doesn't print out the B values like with the first code I posted.
My code on my image results in:
(partial list)
(77, 0, 8)
(89, 0, 8)
(41, 0, 10)
(50, 0, 7)
(42, 0, 5)
(55, 0, 9)
(112, 0, 5)
(105, 0, 9)
(113, 0, 8)
(97, 0, 6)
Which is just printing out the R,G,B where 'B' is > 0.
but running your code on my image I get:
(partial list)
(0, 0): (0, 0, 0)
(1, 0): (1, 0, 0)
(2, 0): (2, 0, 0)
(3, 0): (3, 0, 0)
(4, 0): (4, 0, 0)
(5, 0): (5, 0, 0)
(6, 0): (6, 0, 0)
(7, 0): (7, 0, 0)
(8, 0): (8, 0, 0)
(9, 0): (9, 0, 0)
(10, 0): (10, 0, 0)

with all B values showing '0' with the exception of one at 'x' position 77 which shows:
(77, 0): (77, 0, 8)

How can I get the list to print out all 'B' values > 0. I tried somehow incorporating mine with yours but I can't get it to work right. Thanks
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 15th, 2009
0

Re: Locating pixels 'x' position in image

Wait a sec.....after playing around with sneekula's code for a while, I realized that it works, it's just printing out 1 row.
Python Syntax (Toggle Plain Text)
  1. # testing ...
  2. # show contents of the first row
  3. y = 0
  4. for x in range(width):
  5. print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )

so I guess I just need a way to make it loop through all rows.
I figured this out by adding:
y += 1 below y=0 and saw that it printed out the 2nd row. What's the way to get it to loop through all rows?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 15th, 2009
0

Re: Locating pixels 'x' position in image

I wanted to edit the above thread but anyway.....I got it to loop through so now the last thing I need to work out is how to get it to print out just the values where 'B' is > 0, like in the first code I posted. I'm trying to adjust the code but haven't gotten it yet.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 20th, 2009
0

Re: Locating pixels 'x' position in image

I'm bumping this thread to see if I can get a reply....the code by sneekula works for what I need but I can't figure out how to get it to just print out the ones where the 'B' value is greater than 0, which in the image I gave would be this one:
(77, 0): (77, 0, 8)

I guess I would need to add an 'if pixel[2] >0:' statement in and I've tried that in several locations but keep getting error messages.
any help?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
May 20th, 2009
0

Re: Locating pixels 'x' position in image

Click to Expand / Collapse  Quote originally posted by kiddo39 ...
I guess I would need to add an 'if pixel[2] >0:' statement in and I've tried that in several locations but keep getting error messages.
any help?
That's right... so what are the error messages?

By showing us your mistakes we can help you resolve them. That way, you learn and grow as a programmer. Then you won't have to ask about it next time you need to do something similar.

So everybody wins!
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008
May 20th, 2009
0

Re: Locating pixels 'x' position in image

When I run this:
Python Syntax (Toggle Plain Text)
  1. from PIL import Image
  2.  
  3. img = Image.open('10x.bmp')
  4. for pixel in img.getdata():
  5. if pixel[2] > 0:
  6. print pixel
it works but doesn't have the x,y positions. When I try sneelula's code it works but I need to narrow down the list. So...things I've tried:
Python Syntax (Toggle Plain Text)
  1. for y in range(height):
  2. # columns
  3. for x in range(width):
  4. for pixel in pixel_list:
  5. if pixel[2] > 0:
  6. pixel_dict[(x, y)] = pixel_list[ix]
  7. #print x, y, ix
  8. ix += 1
pixel_dict[(x, y)] = pixel_list[ix]
IndexError: list index out of range
or changing pixel_list to pixel:
Python Syntax (Toggle Plain Text)
  1. for pixel in img.getdata():
  2. if pixel[2] > 0:
  3. print pixel
  4.  
  5.  
  6. # create a (x,y):(r,g,b) dictionary
  7. # where (x,y) are the location coordinates an image point
  8. # and (r,g,b) is the value of the pixel at that point
  9. pixel_dict = {}
  10. ix = 0
  11.  
  12. # rows
  13. for y in range(height):
  14. # columns
  15. for x in range(width):
  16. pixel_dict[(x, y)] = pixel[ix]
  17. #print x, y, ix
  18. ix += 1
pixel_dict[(x, y)] = pixel[ix]
IndexError: tuple index out of range
or changing this part:
Python Syntax (Toggle Plain Text)
  1. y = 0
  2. for x in range(width):
  3. for pixel in img.getdata():
  4. if pixel[2] > 0:
  5. print( "(%d, %d): %s"% (x, y, pixel_dict[(x, y)]) )
that just prints out a very long list.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to store info for later use
Next Thread in Python Forum Timeline: Having an issue downloading proper tools for Python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC