使用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 日常在网站使用过程中经常遇到图形验证,今天准备自己做个图形验证码,这算是个简单的功能,也适 ...
随机推荐
- Jmeter测试计划要素
Jmeter中一个脚本就是一个测试计划,也是一个管理单元.Jmeter的请求模拟与并发数(设置线程数,一个线程即代表一个虚拟用户)设置都在脚本文件中一起设置. 测试计划要素如下: 1.脚本中测试计划只 ...
- OpenCV2计算机编程手册(二)基于类的图像处理
1. 在算法设计中使用策略(Strategy)模式 策略设计模式的目标是将算法封装在类中.因此,可以更容易地替换一个现有的算法,或者组合使用多个算法以拥有更复杂的处理逻辑.此外,该模式将算法的复杂度隐 ...
- 【bzoj4589】Hard Nim FWT+快速幂
题目大意:给你$n$个不大于$m$的质数,求有多少种方案,使得这$n$个数的异或和为$0$.其中,$n≤10^9,m≤10^5$. 考虑正常地dp,我们用$f[i][j]$表示前$i$个数的异或和为$ ...
- Swift 基本数据类型与运算符表达式
// // main.swift // LessonSwift01 // // Created by lanouhn on 16/1/25. // Copyright © 2016年 齐彦坤. All ...
- POJ 2371
#include<iostream> #include<stdio.h> #include<string> using namespace std; int com ...
- Spring Security构建Rest服务-1204-Spring Security OAuth开发APP认证框架之Token处理
token处理之一基本参数配置 处理token时间.存储策略,客户端配置等 以前的都是spring security oauth默认的token生成策略,token默认在org.springframe ...
- Linux文件目录管理
Linux文件目录管理 文件的路径 路径: . 表示当此层目录 .. 表示上一层目录 - 代表前一个工作目录 ~ 代表"目前用户身份"所在的文件夹 ~account 代表accou ...
- Vue中router两种传参方式
Vue中router两种传参方式 1.Vue中router使用query传参 相关Html: <!DOCTYPE html> <html lang="en"> ...
- 慕课网Python基础学习整理
# -*- coding: utf-8 -*- """# Python的注释以 # 开头,后面的文字直到行尾都算注释;多行注释开头3个 " 结尾3个 " ...
- Disconf 学习系列之全网最详细的最新稳定Disconf 搭建部署(基于Ubuntu14.04 / 16.04)(图文详解)
不多说直接上干货! https://www.cnblogs.com/wuxiaofeng/p/6882596.html (ubuntu16.04) https://www.cnblogs.com/he ...