爬虫实战---python图片验证码破解,PIL和安装

咱们最开始先安装几个包:

1.首先安装PIL包对图片的支持:

1.1先下载压缩包:Python Imaging Library 1.1.6 Source Kit (all platforms) 我使用的是这个

下载网址:http://www.pythonware.com/products/pil/

1.2然后解压缩,

1.3进入到解压文件目录,cd Imaging-1.1.6

1.4 执行安装,python setup.py install

还可以看这里http://blog.csdn.net/u010258605/article/details/43735159 安装PIL

2.安装Pytesseract 库

 sudo pip install pytesseract

3.光光安装这两个库可不行,

3.1还需要安装:tesseract-ocr

不安装这个库的后果就是:这个错误坑了我老半天+_+


  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory


3.2 pip install tesseract-ocr (貌似会报错,反正我这样没有安装成功的)

3.3 sudo apt-get install tesseract-ocr (亲测有用这个)

安装完了之后开启的图片读取之旅把!!!


先对简单的图片读取:

import pytesseract
fromPIL importImage
#里面的图片路径是你安装pytesseract里面自带图片
image = Image.open(u'/usr/local/lib/python2.7/dist-packages/pytesseract/test.png')
print image 
text = pytesseract.image_to_string(image)
#接下来输出的就是图片中文本,但是识别度比较低
print text


升级版本:加强对图片的识别处理:

import pytesseract
from PIL import Image
#版本二
image_path = '/home/zhan/Desktop/7025.jpeg'
im = Image.open(image_path)
# im.convert('L').show()
imgry = im.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()
#读取出字符串
cap_str = pytesseract.image_to_string(out)
print cap_str
这样可以取出一些全是数字的验证码处理,数字加图片的识别度不高。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值