View Single Post
Join Date: Oct 2004
Posts: 3,860
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 868
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: counting pixels in an image(ocr)

 
0
  #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