473,324 Members | 2,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

How to locate a pixels position?

Thekid
145 100+
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
6 11986
bvdet
2,851 Expert Mod 2GB
I don't understand your question. You can determine a particular pixel with the getpixel((col,row)) method.
May 6 '09 #2
Thekid
145 100+
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
2,851 Expert Mod 2GB
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
145 100+
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
2,851 Expert Mod 2GB
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
145 100+
Ah....! Thank you :)
May 16 '09 #7

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

Similar topics

33
by: LRW | last post by:
http://gto.ie-studios.net/index.php When you view the above site in IE, if the 1st of the three product images is tall enough to push the cell down a couple of pixels, IE somehow doesn't show...
2
by: hikums | last post by:
how can I use a function inside a function, for example I want to select col1, col2 from table1 where locate(dayofweek(current date),col3) =dayofweek(current date) col3 is a field that contains a...
6
by: Craig Parsons | last post by:
Folks, I have an image of a circle, which I am trying to straighten out into a flat line. I am essentially wanting to look at the image, and read a straight line from the centre, and then plot...
8
by: Stacey | last post by:
I've been trying for the last couple of days to read some pixel values from a window of an independent application, but, even though I got close, I just can't figure the last part out. I am able...
2
by: David Wake | last post by:
I am a complete Javscript newbie trying to produce a user interface similar to Google Maps -- the user should be able to click anywhere on the map and drag it in any direction. The Javascript...
4
by: Jaap Bos | last post by:
In VB2005 I use a rectangle (50,50,910,600) into which to display my graphics. After graphing my X-Y data I want to do some measurements on that curve using a crosshair cursor. So I a made a...
3
by: 4.spam | last post by:
Hello. v8.2.1 Try this (and any 3-d argument): --- select locate('','string',5) from sysibm.sysdummy1; --- Is it a feature or a bug? Sincerely,
2
by: Marcel Brekelmans | last post by:
Hello, I save and restore the position and dimensions of a form using 'this.DeskTopBounds'. However, every time I open the form its Height grows with 19 or so pixels! Is there something I should...
1
by: Kevin Killion | last post by:
I am building a website for a school, but running into a display problem between IE and CSS. (The site is http://stmary-stthomas.org/ ) The main horizontal menu bar looks just fine on Mac and PC...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.