By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,002 Members | 1,050 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,002 IT Pros & Developers. It's quick & easy.

How to locate a pixels position?

Thekid
100+
P: 145
How can I locate a particular pixel in an image? I need to find 'odd' pixels counts, which I can do but I don't know how to get that particular pixel ( if that makes sense):

Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. i = Image.open('Img.bmp')
  4. for pixel in i.getdata():
  5.     if pixel [2] > 0:
  6.         print pixel
  7.  
That will print out just the ones I want instead of every pixel count in the image and the results are like this:

(77, 0, 8)
(89, 0, 8)
(41, 0, 10)
(50, 0, 7)
(42, 0, 5)
(55, 0, 9)
(112, 0, 5)
(105, 0, 9)

So in this instance I would need the pixel positions of the 3rd numbers in the R,G,B lists.
May 6 '09 #1
Share this Question
Share on Google+
6 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
I don't understand your question. You can determine a particular pixel with the getpixel((col,row)) method.
May 6 '09 #2

Thekid
100+
P: 145
Let's say I have a red image that has 8 blue pixels scattered through out it. If I run the code so it prints out like above, it verifies that they were found, but I don't know how to 'locate' them or find their x,y positions in the image. I'm trying to google getpixel((col,row)), it seems that may be what I want I just don't know how to implement it.
May 7 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
Set the upper and lower limits of the pixels you want to find. The following is geared toward red.
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2. import operator
  3.  
  4. img = Image.open(r'D:\Miscellaneous\BVLogo.png')
  5.  
  6. # List the pixels having RGB values in defined range
  7.  
  8. upper = (255,50,50)
  9. lower = (200,0,0)
  10.  
  11. output = []
  12.  
  13. for col in range(img.size[0]):
  14.     for row in range(img.size[1]):
  15.         pixel = img.getpixel((col,row))
  16.         if False not in map(operator.lt,lower,pixel) \
  17.         and False not in map(operator.gt,upper,pixel):
  18.             output.append((col,row))
  19.  
  20. for pixel in output:
  21.     print pixel
May 7 '09 #4

Thekid
100+
P: 145
I've tried the suggested code but can't seem to get it to work. I even tried switching around the R,G,B values but still nothing. Here's an example image for what I'm using:
http://img404.imageshack.us/img404/3418/10x.png

If I run this code on it:
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. i = Image.open('10x.bmp') # I save image as a bmp
  4. for pixel in i.getdata():
  5.     if pixel [2] > 0:
  6.         print pixel
  7.  
I get 88 results, which is what I'm after but I now need their 'x' positions.
What would I be doing wrong with bvdet's code that it doesn't work for me?
May 13 '09 #5

bvdet
Expert Mod 2.5K+
P: 2,851
You are not using the code I suggested. In your case, you want the pixel positions that have "B" values greater than 0.
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2.  
  3. img = Image.open('10x.png')
  4. output = []
  5. for col in range(img.size[0]):
  6.     for row in range(img.size[1]):
  7.         pixel = img.getpixel((col,row))
  8.         if pixel[2] > 0:
  9.             output.append((col,row))
  10.  
  11. for pixel in output:
  12.     print pixel
  13.  
Expand|Select|Wrap|Line Numbers
  1. >>> len(output)
  2. 88
  3. >>> img.getpixel((122,73))
  4. (17, 0, 9)
  5. >>> 
May 16 '09 #6

Thekid
100+
P: 145
Ah....! Thank you :)
May 16 '09 #7

Post your reply

Sign in to post your reply or Sign up for a free account.