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.
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.
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.
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. - 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
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?
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. - 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)
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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...
| |