| | |
counting pixels in an image(ocr)
Thread Solved |
•
•
Join Date: Nov 2008
Posts: 43
Reputation:
Solved Threads: 0
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'.
Any suggestions?
Python Syntax (Toggle Plain Text)
import Image #mypic.jpg is the circle im=image.open("mypic.jpg") im.getcolors(R) # if statement here
Any suggestions?
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:
I know when getting colours i used something like:
python Syntax (Toggle Plain Text)
import Image im = image.open("myPic.jpg") for pixel in im.getdata(): #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
Check out my Site | and join us on IRC | Python Specific IRC
Played around with it a little ...
python Syntax (Toggle Plain Text)
# use PIL to count pixels in letters I and O # image file needs to be a bitmap file to maintain # red (255,0,0) and black (0,0,0) pixel colors correctly from PIL import Image # img_I.bmp --> 50x50 black bg + red I (times new roman, bold, 24) img_I = Image.open("img_I.bmp").getdata() # img_O.bmp --> 50x50 black bg + red O (times new roman, bold, 24) img_O = Image.open("img_O.bmp").getdata() # count red pixels of the I # list(img_I) is a list of (r,g,b) color tuples for each pixel # where (255,0,0) would be the red pixel red_I = 0 for red in list(img_I): if red == (255,0,0): red_I += 1 print red_I # 76 # count red pixels of the O red_O = 0 for red in list(img_O): if red == (255,0,0): red_O += 1 print red_O # 126
May 'the Google' be with you!
![]() |
Other Threads in the Python Forum
- Previous Thread: python and xitami
- Next Thread: Python-Tuple Help!
| Thread Tools | Search this Thread |
address alarm anydbm app beginner cipher conversion coordinates curves cx-freeze data development dictionary directory dynamic examples excel feet file float format function generator getvalue gui halp handling homework images import input ip itunes java keycontrol line linux list lists loan loop maintain maze millimeter mouse mysqldb number numbers output parsing path port prime programming projects py2exe pygame pyglet pymailer python queue random recursion recursive screensaverloopinactive script scrolledtext searchingfile shebang slicenotation split ssh string strings table terminal text thread threading time tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 variable variables ventrilo verify vigenere web webservice wx.wizard wxpython xlwt






