473,569 Members | 3,015 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to locate a pixels position?

Thekid
145 New Member
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 12023
bvdet
2,851 Recognized Expert Moderator Specialist
I don't understand your question. You can determine a particular pixel with the getpixel((col,r ow)) method.
May 6 '09 #2
Thekid
145 New Member
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,r ow)), it seems that may be what I want I just don't know how to implement it.
May 7 '09 #3
bvdet
2,851 Recognized Expert Moderator Specialist
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 New Member
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 Recognized Expert Moderator Specialist
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 New Member
Ah....! Thank you :)
May 16 '09 #7

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

Similar topics

33
4744
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 that happening. But if you look at it in Firefox you can see the small gap of white where the semi-circle image is broken. I've tried changing...
2
2843
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 string like '12 4567', or '1 4567'. This field specifies what day in the week the job executes, and if there is a match, I want to report col1 and...
6
2976
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 this on a graph. Then I want to rotate the image 1 degree and read the next line down, if you see what I mean. My question is, how do I...
8
6860
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 to read the coordinates (GetWindowRect), but the GetPixel always returns some weird incremental value. Is there something wrong with my...
2
5773
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 works OK when the map is only dragged a few pixels. If the map is dragged any significant distance, however, the cursor changes to a document-like...
4
1090
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 MouseDoubleClick event with the following code: Dim position as Point position=Control.MousePostion tijd=position.X
3
4055
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
1879
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 subtract from the DeskTopBounds Rectangle's Height value? Thanks, Marcel
1
2492
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 with most browsers. But on the PC with IE7, the menu bar winds up about 20 pixels too high. The menu is drawn in a DIV with class "navbar",...
0
7703
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7930
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8138
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7983
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6290
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5514
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5228
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.