这里有新鲜出炉的 Python 教程,程序狗速度看过来!
Python 是一种面向对象、解释型计算机程序设计语言,由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年。Python 语法简洁而清晰,具有丰富和强大的类库。它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起。
这篇文章主要介绍了 Python 编程实现的图片识别功能, 涉及 Python PIL 模块的安装与使用技巧, 需要的朋友可以参考下
本文实例讲述了 Python 编程实现的图片识别功能。分享给大家供大家参考,具体如下:
1. 安装 PIL,官方没有 WIN64 位,Pillow 替代
pip install Pillow-2.7.0-cp27-none-win_amd64.whl
2. 安装 Pytesser
下载 pytesser_v0.0.1.zip,解压后复制进 Python27\Lib\site-packges\pytesser 路径下,无 pytesser 则新建
在 Python27\Lib\site-packges\pytesser 中新建一 pytesser.pth 文件, 内容为 pytesser
在 pytesser 内,修改三点
① pytesser.py 修改成__init.py__
② 修改 pytesser.py
- import Image
改为
- from PIL import Image
tesseract_exe_name = 'tesseract' 改为 tesseract_exe_name = 'Python27\\Lib\\site-packges\\pytesser\\tesseract' 注意 \ 转义
③ 安装 Tesseract
下载 Tesseract OCR engine:http://code.google.com/p/tesseract-ocr/ ,
下载后解压,找到 tessdata 文件夹,用其替换掉 pytesser 解压后的 tessdata 文件夹即可。
不过除了测试用验证码之外,其余的系统验证码的识别率很低。
附测试代码
- from pytesser import *
- from PIL import Image, ImageEnhance
- im = Image.open('D:\Python27\Lib\site-packages\pytesser\phototest.tif')
- im2 = Image.open(r'D:\Python27\Lib\site-packages\pytesser\fnord.tif','r')
- im3 = Image.open(r'F:\PROJECT\python\code\Study_1\src\20170424\cp.jpg','r') #文件读写模式以防报错
- #图片处理1::黑白处理
- enhancer = ImageEnhance.Contrast(im3)
- image2 = enhancer.enhance(5)
- image2.show()
- print image_to_string(image2)
- #图片处理2: 降噪处理
- imgry = im3.convert('L') #灰度处理
- #灰度处理基础上二值化处理
- threshold = 140
- table = []
- for i in range(256):
- if i < threshold:
- table.append(0)
- else:
- table.append(1)
- out = imgry.point(table, '1')
- out.show()
- text = image_to_string(out)
- if text.isspace() :
- print "FAILE"
- else:
- print text
- #print text
希望本文所述对大家 Python 程序设计有所帮助。
来源: http://www.phperz.com/article/17/0822/344580.html