1 验证基础知识
1.1 Python生成随机验证码,需要使用PIL模块.

# 安装
pip3 install pillow

1.2 创建图片

from PIL import Image
img = Image.new(mode="RGB", size=(120, 30), color=(125, 255, 255)) # 保存图片到本地
with open("code.png",'wb') as f:
img.save(f,format="png")

1.3 创建画笔

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")

1.4 画点

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一个参数:表示坐标
# 第二个参数:表示颜色
draw.point([100,20], fill="red")
draw.point([50,10], fill=(255, 135, 255))
with open("code.png",'wb') as f:
img.save(f,format="png")

1.5 画线

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一个参数:表示起始坐标和结束坐标
# 第二个参数:表示颜色
draw.line((10, 10, 10, 30), fill="red")
draw.line((10, 10, 30, 10), fill=(255, 135, 255))
with open("code.png",'wb') as f:
img.save(f,format="png")

1.6 画圆

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(150, 150), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一个参数:表示起始坐标和结束坐标(圆要画在中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.arc((50, 50, 100, 100), 0, 360, fill="red")
with open("code.png",'wb') as f:
img.save(f,format="png")

1.7 写文本

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
draw.text([20, 10], "python", fill="red")
with open("code.png",'wb') as f:
img.save(f,format="png")

1.8 设置特殊字体

from PIL import Image, ImageDraw, ImageFont
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一个参数:表示字体路径
# 第二个参数:表示字体大小
font = ImageFont.truetype("kumo.ttf", 25)
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
# 第四个参数:表示字体
draw.text([30, 0], "python", fill="red", font=font)
with open("code.png",'wb') as f:
img.save(f,format="png")

2 应用

2.1 应用方法

import random
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
f = BytesIO()
img = Image.new(mode='RGB', size=(width, height),
color=(random.randint(50, 250), random.randint(50, 255), random.randint(50, 255)))
draw = ImageDraw.Draw(img, mode='RGB') char_list = []
# 画字
for i in range(char_length):
char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)), chr(random.randint(97, 122)), ])
font = ImageFont.truetype("blog/static/fonts/"+font_file, font_size)
draw.text([i * 24, 0], char, (random.randint(200, 255), random.randint(0, 50), random.randint(0, 50)),
font=font)
char_list.append(char) def rndColor():
"""
生成随机颜色
:return:
"""
return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255)) # 写干扰点
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor()) # 写干扰圆圈
for i in range(40):
draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor()) # 画干扰线
for i in range(5):
x1 = random.randint(0, width)
y1 = random.randint(0, height)
x2 = random.randint(0, width)
y2 = random.randint(0, height)
draw.line((x1, y1, x2, y2), fill=rndColor()) img.save(f, "png")
data = f.getvalue()
s_code = ''.join(char_list)
return data,s_code

2.2 调用方法

def get_valid_img(request):
'''
登录和注册的验证码
:param request:
:return:
'''
data,s_code = check_code()
request.session["valid_code"] = s_code
return HttpResponse(data)

2.3 效果展示

python实现图片验证码的更多相关文章

  1. python随机图片验证码的生成

    Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Im ...

  2. 字符识别Python实现 图片验证码识别

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  3. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  4. python添加图片验证码

    1.创建验证码模块 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDra ...

  5. python爬虫20 | 小帅b教你如何使用python识别图片验证码

    当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...

  6. 【Python实现图片验证码】

    "```python import base64 import random from PIL import Image from PIL import ImageDraw # 画笔对象 f ...

  7. Python实现图片验证码识别

    转载地址:https://blog.csdn.net/EB_NUM/article/details/77060009 具体想要实现上面的代码需要安装两个包和一个引擎 在安装之前需要先安装好Python ...

  8. 字符型图片验证码识别完整过程及Python实现

    字符型图片验证码识别完整过程及Python实现 1   摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...

  9. 【python】带图片验证码的登录自动化实战

    近期在跟进新项目的时候,整体的业务线非常之长,会一直重复登录退出不同账号的这个流程,所以想从登录开始实现部分的自动化.因为是B/S的架构,所以采用的是selenium的框架来实现.大致实现步骤如下: ...

随机推荐

  1. C++ STL标准入门

    C++:STL标准入门汇总 第一部分:(参考百度百科) 一.STL简介 STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.它是由Alexand ...

  2. 一个爬取https和http通用的工具类(JDK自带的URL的用法)

    今天在java爬取天猫的时候因为ssl报错,所以从网上找了一个可以爬取https和http通用的工具类.但是有的时候此工具类爬到的数据不全,此处不得不说python爬虫很厉害. package cn. ...

  3. php隐藏WEBSHELL技巧

    把shell添加到网站logo图片里: cat logo.png shell.php > logo.png 在网站任意一个php文件里添加下面的最简单方法: fputs(fopen('/home ...

  4. java基础3 循环语句:While 循环语句、do while 循环语句、 for 循环语句 和 break、continue关键字

    一.While循环语句 1.格式 while(条件表达式){ 执行语句: } 2.要点 1,先判断后执行 2,循环次数不定 3,避免死循环 3.举例 题目1:输出0-100之间的所有数 class D ...

  5. HDU 3342 Legal or Not(拓扑排序判断成环)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3342 题目大意:n个点,m条有向边,让你判断是否有环. 解题思路:裸题,用dfs版的拓扑排序直接套用即 ...

  6. csu 1503: 点到圆弧的距离

    1503: 点到圆弧的距离 Time Limit: 1 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 614  Solved: 101[Submit] ...

  7. IEEEXtreme 10.0 - Flower Games

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.com/contests/ieeextreme-c ...

  8. copy深浅拷贝

    我们在很多方法里都看到copy()方法,这是对变量的复制,赋值,下面来看一下实例: 复制方法调用的是copy模块中的方法: import copy copy.copy()         #前拷贝 c ...

  9. vue中methods、watch、computed之间的差别对比以及适用场景

    首先要说,methods,watch和computed都是以函数为基础的,但各自却都不同: 一.computer 当页面中有某些数据依赖其他数据进行变动的时候,可以使用计算属性. <p id=& ...

  10. 在CentOS7命令行模式下安装虚拟机

    转载:https://blog.csdn.net/sunnyfg/article/details/51493602 1.主机环境描述: 操作系统:CentOS7 系统GUI:无 CPU:Intel4代 ...