工作于运维时间长了,安全问题一直是重点。今天来研究一下简单的验证码可能带来的安全问题。
mac python2.7安装PIL.Image模块
mac python Image PIL
要想在python中操作图片,比如引入PIL(Python Imaging Library)库。
在python安装第三库时,可以使用工具easy_install或pip,我推荐使用pip这个工具。针对mac电脑,pip是不需要手动安装,在命令行可以直接输入pip验证是否已经安装。
若是pip没有安装,可以使用命令sudo easy_install pip(保证mac连接互联网)
安装pip之后,可以使用命令sudo pip install PIL来安装操作图片的模块了。
若是安装正常,那皆大欢喜了。在我电脑安装时,却出现了问题。
could not find a version that satisfies the requirement PIL.(form versions:)
No matching distribution found for PIL.
这个是说明PIL已经找不到,其实现在已经用Pillow代替了PIL,在使用方面没有不同,API都是相同的。
既然如此,咱们就直接安装Pillow模块吧,执行sudo pip install Pillow
安装这个模块时,发现它会依赖另外一个模块:multiprocessing
只能先把multiprocessing模块安装好再执行上面的命令了,sudo pip install multiprocessing即可正常安装,非常小的一个模块
接着再执行sudo pip install Pillow命令,就可以正常安装模块了,自动安装好之后,就可以正常使用了。
使用时需要注意的是引入模块要按照下面的方式写(注意大小写)
第一种:from PIL import Image
第二种:from PIL.Image(用这种方式时,下面使用时也得写成PIL.Image.open('1.png'),个人觉得不太好看,可以在引入时修改下模块名,如from PIL.Image as image)
引入之后就可以正常使用了。
安装步骤:
sudo pip install multiprocessing
sudo pip install Pillow
brew install --with-training-tools tesseract
使用:
1.命令行方式:
tesseract test.png output.txt #识别test.png的图片,把结果放到output.txt中
2. python 代码:
mac-temp:py test$ cat vcode.py
#coding=utf-8
from PIL import Image
import pytesseract
im = Image.open('./test2.png')
print pytesseract.image_to_string(im)
mac-temp:py test$ python ./vcode.py
Hello world!
1234
以上只能识别简单的图片:
复杂一点的就不能识别:
所以,复杂一点的验证码需要做更多处理。
参考:
https://stackoverflow.com/questions/28741563/pytesseract-no-such-file-or-directory-error
https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/00140767171357714f87a053a824ffd811d98a83b58ec13000
http://blog.csdn.net/huangzhang_123/article/details/72819061