RSS Forums RSS

counting pixels in an image(ocr)

Please support our Python advertiser: Programming Forums
Thread Solved
Reply
Posts: 39
Reputation: kiddo39 is an unknown quantity at this point 
Solved Threads: 0
kiddo39 kiddo39 is offline Offline
Light Poster

counting pixels in an image(ocr)

  #1  
Dec 3rd, 2008
I have a small image with a black background and red lines in it and I need to count the red pixels. I know I need PIL, but I'm not sure which method to use. Then once I have the counts I need to equate them to a character, so if the red lines in the image form a circle and have x amount of red pixels, its = '0', and if the image forms a straight line and is found to have x amount of pixels it's = 'l'.

import Image
#mypic.jpg is the circle
im=image.open("mypic.jpg")
im.getcolors(R)
# if statement here

Any suggestions?
AddThis Social Bookmark Button
Reply With Quote  
Posts: 655
Reputation: paulthom12345 will become famous soon enough paulthom12345 will become famous soon enough 
Solved Threads: 91
paulthom12345's Avatar
paulthom12345 paulthom12345 is offline Offline
Practically a Master Poster

Re: counting pixels in an image(ocr)

  #2  
Dec 4th, 2008
Could you test how many pixels are red? Because a circle will require many more pixels to draw then a line.
I know when getting colours i used something like:
  1. import Image
  2. im = image.open("myPic.jpg")
  3. for pixel in im.getdata():
  4. #check if it is red
Make it idiot proof and someone will make a better idiot.
Check out my Site | and join us on IRC | Python Specific IRC
Reply With Quote  
Posts: 2,947
Reputation: vegaseat is a jewel in the rough vegaseat is a jewel in the rough vegaseat is a jewel in the rough 
Solved Threads: 254
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: counting pixels in an image(ocr)

  #3  
Dec 4th, 2008
Played around with it a little ...
  1. # use PIL to count pixels in letters I and O
  2. # image file needs to be a bitmap file to maintain
  3. # red (255,0,0) and black (0,0,0) pixel colors correctly
  4.  
  5. from PIL import Image
  6.  
  7. # img_I.bmp --> 50x50 black bg + red I (times new roman, bold, 24)
  8. img_I = Image.open("img_I.bmp").getdata()
  9.  
  10. # img_O.bmp --> 50x50 black bg + red O (times new roman, bold, 24)
  11. img_O = Image.open("img_O.bmp").getdata()
  12.  
  13. # count red pixels of the I
  14. # list(img_I) is a list of (r,g,b) color tuples for each pixel
  15. # where (255,0,0) would be the red pixel
  16. red_I = 0
  17. for red in list(img_I):
  18. if red == (255,0,0):
  19. red_I += 1
  20.  
  21. print red_I # 76
  22.  
  23. # count red pixels of the O
  24. red_O = 0
  25. for red in list(img_O):
  26. if red == (255,0,0):
  27. red_O += 1
  28.  
  29. print red_O # 126
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.



Other Threads in the Python Forum
Views: 657 | Replies: 2 | Currently Viewing: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:28 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC