Hi,
I know I need to use PIL, but how would I count all of the red pixels in a given image? The image is simply a vertical red line on a black background. -
import PIL
-
-
im = image.open("mypic.jpg")
-
im.getdata()
-
Something like that maybe but not sure how to get it to count the red pixels.
4 27981 bvdet 2,851
Expert Mod 2GB
Let us assume a pixel is red if each RGB value is in excess of 200. - from PIL import Image
-
import operator
-
-
img = Image.open('your_image')
-
-
# Count the pixels having RGB values in defined range
-
-
upper = (255,255,255)
-
lower = (200,200,200)
-
-
print len([pixel for pixel in img.getdata() \
-
if False not in map(operator.lt,lower,pixel) \
-
and False not in map(operator.gt,upper,pixel)])
I would do it this way: -
from PIL import Image
-
im = Image.new("RGBA",(512,512))
-
-
# create a funny picture... (with some red)
-
-
for i in range(512):
-
for j in range(512):
-
im.putpixel((j,i),(j^i)%256)
-
-
# calculate the number of "reds"
-
cnr = 0
-
for i in range(512):
-
for j in range(512):
-
if((im.getpixel((j,i)))[0] == 255):
-
cnr+=1
-
-
print "nr of red pixels : " + str(cnr)
-
print "presentage of red pixels : " + str((cnr / (512.0*512.0))*100.0) + "%"
-
-
im.save("pattern.png") # look at it, it looks funny
-
bvdet 2,851
Expert Mod 2GB
That's cool kudos. Thanks for sharing.
bvdet 2,851
Expert Mod 2GB
I did a red count on my avatar. - from PIL import Image
-
import operator
-
-
img = Image.open('image.png')
-
-
upper = (256,40,40)
-
lower = (190,0,0)
-
-
print '\n'.join([str(pixel) for pixel in img.getdata() \
-
if False not in map(operator.lt,lower,pixel) \
-
and False not in map(operator.gt,upper,pixel)])
-
-
# Output:
-
>>> (210, 13, 7)
-
(202, 21, 7)
-
(201, 9, 9)
-
(198, 11, 11)
-
(196, 26, 6)
-
(198, 15, 15)
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: baruch |
last post by:
Hello!
I'm using PHP Version 4.3.3RC5-dev with GD Version bundled (2.0.15
compatible) to generated some graphics based on mysql data and to my
surprise when I use the GD function imageline($im,...
|
by: Pierre |
last post by:
Hi... again (it's a bad day)
Here is the point,
I draw ellipse on an image (a map) in order to map something.
But what I really don't know is how to erase the ellipse if I don't want to
map the...
|
by: serdar c |
last post by:
hi everyone, i am writing a program for my scool project and i need to count
the number of black pixels in a white empty picture box.. in the old visual
basic there was a really simple command...
|
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: Ma Xiaoguang |
last post by:
Dear ladies and gentlemen:
How to access all the pixels of a bitmap with C#? I am working with
Delphi before. In Delphi, you can use TBitmap.Scanline property to access
all the pixels of a...
|
by: Chris Saunders |
last post by:
I am fairly new to .Net and C# but am familiar with the Win32 API.
I wish to set the pixels of the client area of a window one at a time
with a specific color. I have so far discovered that...
|
by: headware |
last post by:
I have to create some vertical text for a web page that allows users
to print to labels. We decided to do this by creating an <imgtag on
the web page and having the src attribute point to another...
|
by: Tim Streater |
last post by:
If I create an image thus:
var myImg = new Image ();
I can then do the following:
myImg.src = "someimage.jpg";
But, I can't seem to find a way to manipulate the pixels of the image in...
|
by: n3ts3cur3 |
last post by:
I have an array of int pixels in my c# program and i want to convert it into an image. The problem is i am converting java source code for a program into equivalent c# code. In java the line reads...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
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,...
|
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...
| |