您的位置:首页 > 编程语言 > Python开发

初识python PIL与pytesser

2016-07-11 23:42 344 查看
1、关于安装pytesser,安装前要安装PIL,具体的文件下载可以翻墙到官网去

值得注意的问题是尽量保持一致,譬如windows下的PIL为32位的(PIL-1.1.7.win32-for-python2.7.exe)

所以Python的版本也尽量装32位的,不然可以会遇到麻烦。pytesser直接解压在Python27\Lib\site-packages\文件夹下即可

2、初步认识pytesser 与 PIL 的一些函数


 分辨率为 100*25
文件名为 demo.jpg

对于上面的验证码,因为字符的大小不太一样所以分割就显的麻烦,其实这种验证码直接用库函数就可以solve。

def demo():
import Image
from pytesser import image_to_string
img = Image.open('demo.jpg') # open and read the image
img = img.convert('L') # convert to other pixel format
# img.save('demo_convert.jpg') # save image
photo = img.resize((200,65)) # Resize image
print image_to_string(photo) # identify image
# end

output>>
vj9vpu

其中
img = img.convert('L') # convert to other pixel format
photo = img.resize((200,65)) # Resize image

这两行代码很关键,
第一行除去了颜色(差不多图片二值化这样子,先可以这样理解)

第二性更改了图片的大小,并非所有的验证码都需要这样子更改,一般码比较密集的,像素比较低的建议更改,至于更改的大小就得看经验或者自己测试咯
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: