444,002 Members | 1,050 Online
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?

 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 from PIL import Image   i = Image.open('Img.bmp') for pixel in i.getdata():     if pixel [2] > 0:         print pixel   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
6 Replies

 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

 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

 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 from PIL import Image import operator   img = Image.open(r'D:\Miscellaneous\BVLogo.png')   # List the pixels having RGB values in defined range   upper = (255,50,50) lower = (200,0,0)   output = []   for col in range(img.size[0]):     for row in range(img.size[1]):         pixel = img.getpixel((col,row))         if False not in map(operator.lt,lower,pixel) \         and False not in map(operator.gt,upper,pixel):             output.append((col,row))   for pixel in output:     print pixel May 7 '09 #4

 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 from PIL import Image   i = Image.open('10x.bmp') # I save image as a bmp for pixel in i.getdata():     if pixel [2] > 0:         print pixel   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

 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 from PIL import Image   img = Image.open('10x.png') output = [] for col in range(img.size[0]):     for row in range(img.size[1]):         pixel = img.getpixel((col,row))         if pixel[2] > 0:             output.append((col,row))   for pixel in output:     print pixel   Expand|Select|Wrap|Line Numbers >>> len(output) 88 >>> img.getpixel((122,73)) (17, 0, 9) >>>  May 16 '09 #6

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