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. weblogic 开启注意问题

    1.关闭防火墙 service iptables stop chkconfig iptables off 2.weblogic unable to get file lock问题 我的解决办法是ps ...

  2. 64_t6

    texlive-recipebook-svn37026.0-33.fc26.2.noarch.rpm 24-May-2017 15:44 37946 texlive-recipecard-doc-sv ...

  3. 前端nginx时,让后端tomcat记录真实IP【转】

    对于nginx+tomcat这种架构,如果后端tomcat配置保持默认,那么tomcat的访问日志里,记录的就是前端nginx的IP地址,而不是真实的访问IP.因此,需要对nginx.tomcat做如 ...

  4. 84.Largest Rectangle in histogram---stack

    题目链接:https://leetcode.com/problems/largest-rectangle-in-histogram/description/ 题目大意:在直方图中找出最大的矩形面积.例 ...

  5. sicily 1003. Hit or Miss

    Description One very simple type of solitaire game known as "Hit or Miss" (also known as & ...

  6. centos7安装lamp

    一.准备工作 1.   下载并安装CentOS7.2,配置好网络环境,确保centos能上网,可以获取到yum源. centos7.2的网络配置: vim /etc/sysconfig/network ...

  7. PHP扩展插件 imagick 、PDO_MYSQL 安装

    环境准备 echo $LC_ALL echo "export LC_ALL=C" >> /etc/profile source /etc/profile yum ins ...

  8. 20165301 预备作业二:学习基础和C语言基础调查

    <做中学>读后感及C语言学习调查 读<做中学>有感 娄老师在文章中多次提到「做中学(Learning By Doing)」的概念,并通过娄老师自己的减肥经历.五笔练习经历.乒乓 ...

  9. 最直白、最易懂的话带你认识和学会---数据分析基础包之numpy的使用

    前言 numpy是一个很基础很底层的模块,其重要性不言而喻,可以说对于新手来说是最基础的入门必须要学习的其中之一.在很多数据分析,深度学习,机器学习亦或是人工智能领域的模块中,很多的底层都会用到这个模 ...

  10. OpenCV3学习笔记

    http://blog.csdn.net/u010429424/article/details/73691001 http://blog.csdn.net/zhaoxfxy/article/detai ...