473,403 Members | 2,354 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,403 software developers and data experts.

Counting pixels in image

Thekid
145 100+
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.

Expand|Select|Wrap|Line Numbers
  1. import PIL
  2.  
  3. im = image.open("mypic.jpg")
  4. im.getdata()
  5.  
Something like that maybe but not sure how to get it to count the red pixels.
Dec 8 '08 #1
4 27981
bvdet
2,851 Expert Mod 2GB
Let us assume a pixel is red if each RGB value is in excess of 200.
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2. import operator
  3.  
  4. img = Image.open('your_image')
  5.  
  6. # Count the pixels having RGB values in defined range
  7.  
  8. upper = (255,255,255)
  9. lower = (200,200,200)
  10.  
  11. print len([pixel for pixel in img.getdata() \
  12.            if False not in map(operator.lt,lower,pixel) \
  13.            and False not in map(operator.gt,upper,pixel)])
Dec 8 '08 #2
kudos
127 Expert 100+
I would do it this way:
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2. im  = Image.new("RGBA",(512,512))
  3.  
  4. # create a funny picture... (with some red)
  5.  
  6. for i in range(512):
  7.  for j in range(512):
  8.   im.putpixel((j,i),(j^i)%256)
  9.  
  10. # calculate the number of "reds"
  11. cnr = 0
  12. for i in range(512):
  13.  for j in range(512):
  14.   if((im.getpixel((j,i)))[0] == 255):
  15.    cnr+=1
  16.  
  17. print "nr of red pixels : " + str(cnr)
  18. print "presentage of red pixels : " + str((cnr / (512.0*512.0))*100.0) + "%"
  19.  
  20. im.save("pattern.png")  # look at it, it looks funny
  21.  
Dec 9 '08 #3
bvdet
2,851 Expert Mod 2GB
That's cool kudos. Thanks for sharing.
Dec 9 '08 #4
bvdet
2,851 Expert Mod 2GB
I did a red count on my avatar.
Expand|Select|Wrap|Line Numbers
  1. from PIL import Image
  2. import operator
  3.  
  4. img = Image.open('image.png')
  5.  
  6. upper = (256,40,40)
  7. lower = (190,0,0)
  8.  
  9. print '\n'.join([str(pixel) for pixel in img.getdata() \
  10.                  if False not in map(operator.lt,lower,pixel) \
  11.                  and False not in map(operator.gt,upper,pixel)])
  12.  
  13. # Output:
  14. >>> (210, 13, 7)
  15. (202, 21, 7)
  16. (201, 9, 9)
  17. (198, 11, 11)
  18. (196, 26, 6)
  19. (198, 15, 15)
  20. >>> 
Dec 11 '08 #5

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

Similar topics

4
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,...
1
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...
4
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...
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...
4
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...
3
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...
3
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...
5
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...
2
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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...
0
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...
0
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,...
0
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...

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.