Locating pixels 'x' position in image

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Locating pixels 'x' position in image

 
0
  #1
May 13th, 2009
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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Locating pixels 'x' position in image

 
0
  #2
May 13th, 2009
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:
  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?
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 908
Reputation: Paul Thompson has a spectacular aura about Paul Thompson has a spectacular aura about 
Solved Threads: 145
Sponsor
Paul Thompson's Avatar
Paul Thompson Paul Thompson is offline Offline
previously paulthom12345

Re: Locating pixels 'x' position in image

 
0
  #3
May 13th, 2009
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
  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.
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,279
Reputation: sneekula has a spectacular aura about sneekula has a spectacular aura about 
Solved Threads: 176
sneekula's Avatar
sneekula sneekula is offline Offline
Nearly a Posting Maven

Re: Locating pixels 'x' position in image

 
1
  #4
May 13th, 2009
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:
  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)])
Last edited by sneekula; May 13th, 2009 at 8:02 pm. Reason: image attached
Attached Images
File Type: bmp french_flag.bmp (1.2 KB, 4 views)
No one died when Clinton lied.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: Locating pixels 'x' position in image

 
0
  #5
May 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: Locating pixels 'x' position in image

 
0
  #6
May 15th, 2009
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.
  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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: Locating pixels 'x' position in image

 
0
  #7
May 15th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: Locating pixels 'x' position in image

 
0
  #8
May 20th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,054
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 265
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Locating pixels 'x' position in image

 
0
  #9
May 20th, 2009
Originally Posted by kiddo39 View Post
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!
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 43
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

Re: Locating pixels 'x' position in image

 
0
  #10
May 20th, 2009
When I run this:
  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:
  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:
  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:
  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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC