使用Python生成基础验证码教程
pillow是Python平台事实上的图像处理标准库。PIL功能非常强大,但API却非常简单易用。 所以我们使用它在环境里做图像的处理。
第一步 下载pillow
#运行命令 pip install pillow
第二部 编写代码
1>创建一个类,初始化并为类添加属性
我们可能需要的属性有:验证码图片宽高,干扰点线数量,我们要出现多少个验证码等
2>随机生成背景颜色和字体颜色,在此建议将背景色生成范围定为浅色(0-120),字体色为深色(120-255)易于人眼识别
3>创建画布并依次画线点字,如果需要将字体倾斜旋转需要拷贝原图旋转再与原图合成
4>返回验证码图片和验证码答案字符串
例:
from PIL import Image,ImageDraw,ImageFont
import random
import io class code:
def __init__(self):
self.width=120 //生成验证码图片的宽度
self.height=40 //生成验证码图片的高度
self.im=None
self.lineNum=None //生成干扰线的数量
self.pointNum=None //生成干扰点的数量
self.codecon="QWERTYUPASDFGHJKZXCVBNMqwertyupadfhkzxcvbnm0123456789" //验证码出现的字符
self.codelen=4 //验证码出现字符的数量
self.str=""
def randBgColor(self):
return (random.randint(0,120),random.randint(0,120),random.randint(0,120))
def randFgColor(self):
return (random.randint(120, 255), random.randint(120, 255), random.randint(120, 255))
def create(self):
self.im = Image.new('RGB', size=(self.width, self.height), color=self.randBgColor())
def lines(self):
lineNum=self.lineNum or random.randint(3,6)
draw = ImageDraw.Draw(self.im)
for item in range(lineNum):
place=(random.randint(0,self.width),random.randint(0,self.height),random.randint(0,self.height),random.randint(0,self.height))
draw.line(place,fill=self.randFgColor(),width=random.randint(1,3))
def point(self):
pointNum = self.pointNum or random.randint(30, 60)
draw = ImageDraw.Draw(self.im)
for item in range(pointNum):
place=(random.randint(0,self.width),random.randint(0,self.height))
draw.point(place,fill=self.randFgColor())
def texts(self):
draw = ImageDraw.Draw(self.im)
for item in range(self.codelen):
x=item*self.width/self.codelen+random.randint(-self.width/15,self.width/15)
y=random.randint(-self.height/10,self.height/10)
text=self.codecon[random.randint(0,len(self.codecon)-1)]
self.str+=text
fnt = ImageFont.truetype('ARVO-REGULAR.TTF', random.randint(30,38))
draw.text((x,y),text,fill=self.randFgColor(),font=fnt,rotate="")
def output(self):
self.create()
self.texts()
self.lines()
self.point()
bt=io.BytesIO()
self.im.save(bt,"png")
return bt.getvalue()
5>将验证码渲染到网页中,以Flask为例
<img src="/codeimg" alt="" width="120" height="40">
@app.route('/codeimg')
def codeimg():
codeobj=code()
res=make_response(codeobj.output())
session["code"]=codeobj.str.lower()
res.headers["content-type"]="image/png"
return res
简单的输入式验证码就完成了,如有错误之处欢迎指正。
破解验证码时我们要用到第三方库。
解决思路:因为这种是最简单的一种验证码,只要识别出里面的内容,然后填入到输入框中即可。这种识别技术叫OCR,这里推荐使用Python的第三方库,tesserocr。对于有嘈杂的背景的验证码这种,直接识别识别率会很低,遇到这种我们就得需要先处理一下图片,先对图片进行灰度化,然后再进行二值化,再去识别,这样识别率会大大提高。
同样也可以参考使用pillow处理识别,链接https://blog.csdn.net/qq_35923581/article/details/79487579
使用Python生成基础验证码教程的更多相关文章
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- python生成随机图形验证码
使用python生成随机图片验证码,需要使用pillow模块 1.安装pillow模块 pip install pillow 2.pillow模块的基本使用 1.创建图片 from PIL impor ...
- Python生成随机验证码,大乐透号码
实例笔记之生成随机号码 扩展知识 - yield(生成器) 随机生成验证码 示例代码: import random # 导入标准模块中的random if __name__ == '__main__' ...
- python生成中文验证码,带旋转,带干扰噪音线段
# -*- coding: utf-8 -*- """ Created on Sun Oct 4 15:57:46 2015 @author: keithguofan & ...
- Python运算符 - Python零基础入门教程
目录 一.算术运算符 二.赋值运算符 三.比较运算符 四.运算符的优先等级 五.重点总结 六.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python ...
- Python break/continue - Python零基础入门教程
目录 一.break 二.continue 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python wh ...
- Python for循环 - Python零基础入门教程
目录 一.for 循环语法 二.for 循环实战 三.重点总结 四.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 在 Python ...
- python 生成图形验证码
文章链接:https://mp.weixin.qq.com/s/LYUBRNallHcjnhJb1R3ZBg 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...
随机推荐
- Elasticsearch系列(五)----JAVA客户端之TransportClient操作详解
Elasticsearch JAVA操作有三种客户端: 1.TransportClient 2.JestClient 3.RestClient 还有种是2.3中有的NodeClient,在5.5.1中 ...
- D05——C语言基础学PYTHON
C语言基础学习PYTHON——基础学习D05 20180815内容纲要: 1 模块 2 包 3 import的本质 4 内置模块详解 (1)time&datetime (2)datetime ...
- iOS 关于布局问题的一些认识
///更新约束和布局 更新约束布局相关的API - (void)updateConstraintsIfNeeded 调用此方法,如果有标记为需要重新布局的约束,则立即进行重新布局,内部会调用upda ...
- WebDriverAPI(1)
访问某网页地址 被测网址http:http://www.baidu.com Java语言版本的API实例代码 方法一: @Test public void visitURL(){ String bas ...
- Winform—C#读写config配置文件
现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager.并且AppSettings属性是只读的,并不支持修改属性值. 一.如 ...
- (转)Python标准库:内置函数print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
原文:https://blog.csdn.net/caimouse/article/details/44133241 https://www.cnblogs.com/owasp/p/5372476.h ...
- 问题记录 | PyLint not recognizing cv2 members
问题记录 | PyLint not recognizing cv2 members VScode中安装了pylint,总是提示cv2的一些成员函数找不到, 如这样的问题: Module 'cv2' h ...
- 2-nginx 安装
1, nginx简介: •Nginx("engine x") 是一个高性能的HTTP 和反向代理服务器,也是一个IMAP/POP3/SMTP 代理服务器.•第一个公开版本0.1.0 ...
- php里的二进制安全
二进制安全功能(binary-safe function)是指在一个二进制文件上所执行的不更改文件内容的功能或者操作.这能够保证文件不会因为某些操作而遭到损坏.二进制数据是按照一串0和 1的形式编码的 ...
- java将list转为树形结构的方法
目录 1.通过转化成json封装数据 2.通过java8 stream转换 1.通过转化成json封装数据 原始数据如下 [ { "name":"甘肃省", & ...