943,870 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2113
  • Python RSS
Dec 3rd, 2008
0

counting pixels in an image(ocr)

Expand Post »
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'.

Python Syntax (Toggle Plain Text)
  1. import Image
  2. #mypic.jpg is the circle
  3. im=image.open("mypic.jpg")
  4. im.getcolors(R)
  5. # if statement here

Any suggestions?
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
kiddo39 is offline Offline
50 posts
since Nov 2008
Dec 4th, 2008
0

Re: counting pixels in an image(ocr)

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:
python Syntax (Toggle Plain Text)
  1. import Image
  2. im = image.open("myPic.jpg")
  3. for pixel in im.getdata():
  4. #check if it is red
Reputation Points: 264
Solved Threads: 183
Veteran Poster
Paul Thompson is offline Offline
1,095 posts
since May 2008
Dec 4th, 2008
0

Re: counting pixels in an image(ocr)

Played around with it a little ...
python Syntax (Toggle Plain Text)
  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
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python and xitami
Next Thread in Python Forum Timeline: Python-Tuple Help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC