代碼如下:
#coding:utf-8from PIL import Imageimport pytesseractdef test(): im = Image.open(r"pic.gif") vcode = pytesseract.image_to_string(im) PRint vcode
執(zhí)行以上代碼進(jìn)行簡單驗(yàn)證碼識(shí)別的時(shí)候會(huì)拋出一個(gè)異常:
Traceback (most recent call last): File "D:/test/vcode.py", line 15, in <module> main() File "D:/test/vcode.py", line 9, in main test() File "D:/test/test.py", line 8, in test vcode = pytesseract.image_to_string(im) File "build/bdist.win32/egg/pytesseract/pytesseract.py", line 143, in image_to_string File "D:/Program Files (x86)/Python/Python27/lib/site-packages/PIL/Image.py", line 1749, in split self.load() File "D:/Program Files (x86)/Python/Python27/lib/site-packages/PIL/ImageFile.py", line 232, in load "(%d bytes not processed)" % len(b))IOError: image file is truncated (5 bytes not processed)
解決辦法是,再添加如下2句代碼:
from PIL import ImageFileImageFile.LOAD_TRUNCATED_IMAGES = True
最終,完整的代碼如下:
#coding:utf-8from PIL import Imageimport pytesseractfrom PIL import ImageFileImageFile.LOAD_TRUNCATED_IMAGES = Truedef test(): im = Image.open(r"pic.gif") vcode = pytesseract.image_to_string(im) print vcode
關(guān)于利用python進(jìn)行驗(yàn)證碼識(shí)別的一些想法:http://m.survivalescaperooms.com/xiaowuyi/archive/2012/09/10/2675286.html
python利用pytesser模塊實(shí)現(xiàn)圖片文字識(shí)別:http://www.jinglingshu.org/?p=9281
驗(yàn)證碼圖片字符識(shí)別兩種python實(shí)現(xiàn)方法:http://vipscu.blog.163.com/blog/static/18180837220134234528457/
python模擬登陸登陸一:驗(yàn)證碼與cookies的同步處理思路:http://www.dabu.info/python-login-crawler-captcha-cookies.html
原文地址:http://m.survivalescaperooms.com/hongfei/p/4436767.html
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注