How to locate a pixels position?  | Member | | Join Date: Feb 2007
Posts: 110
| |
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): -
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.
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,564
| | | re: How to locate a pixels position?
I don't understand your question. You can determine a particular pixel with the getpixel((col,row)) method.
|  | Member | | Join Date: Feb 2007
Posts: 110
| | | re: How to locate a pixels position?
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.
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,564
| | | re: How to locate a pixels position?
Set the upper and lower limits of the pixels you want to find. The following is geared toward red. - 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
|  | Member | | Join Date: Feb 2007
Posts: 110
| | | re: How to locate a pixels position?
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: -
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?
|  | Moderator | | Join Date: Oct 2006 Location: Nashville, TN
Posts: 1,564
| | | re: How to locate a pixels position?
You are not using the code I suggested. In your case, you want the pixel positions that have "B" values greater than 0. - 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
-
- >>> len(output)
-
88
-
>>> img.getpixel((122,73))
-
(17, 0, 9)
-
>>>
|  | Member | | Join Date: Feb 2007
Posts: 110
| | | re: How to locate a pixels position?
Ah....! Thank you :)
|  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|