python实现图片验证码
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实现图片验证码的更多相关文章
- python随机图片验证码的生成
Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 from PIL import Im ...
- 字符识别Python实现 图片验证码识别
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- python 识别图片验证码报IOError
说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...
- python添加图片验证码
1.创建验证码模块 #!/usr/bin/env python # -*- coding:utf-8 -*- import random from PIL import Image, ImageDra ...
- python爬虫20 | 小帅b教你如何使用python识别图片验证码
当你在爬取某些网站的时候 对于你的一些频繁请求 对方会阻碍你 常见的方式就是使用验证码 验证码的主要功能 就是区分你是人还是鬼(机器人) 人 想法设法的搞一些手段来对付技术 而 技术又能对付人们的想法 ...
- 【Python实现图片验证码】
"```python import base64 import random from PIL import Image from PIL import ImageDraw # 画笔对象 f ...
- Python实现图片验证码识别
转载地址:https://blog.csdn.net/EB_NUM/article/details/77060009 具体想要实现上面的代码需要安装两个包和一个引擎 在安装之前需要先安装好Python ...
- 字符型图片验证码识别完整过程及Python实现
字符型图片验证码识别完整过程及Python实现 1 摘要 验证码是目前互联网上非常常见也是非常重要的一个事物,充当着很多系统的 防火墙 功能,但是随时OCR技术的发展,验证码暴露出来的安全问题也越 ...
- 【python】带图片验证码的登录自动化实战
近期在跟进新项目的时候,整体的业务线非常之长,会一直重复登录退出不同账号的这个流程,所以想从登录开始实现部分的自动化.因为是B/S的架构,所以采用的是selenium的框架来实现.大致实现步骤如下: ...
随机推荐
- phpmywind目录结构
phpmywind目录结构了解 admin/ 后台管理目录 admin/editor/ 后台编辑器存放目录 admin/inc/ 后台公用文件引用目录 admin/plugin/ 后台插件存放目录 a ...
- RTM,RTW,GA等软件版本号详解
一直以来,对于新手而言,软件的版本号都是个比较困扰人的问题,什么Beta.RC,再来个RTM.RTW....头大了吧?RTM和RTW有什么区别?借此机会,就给大家介绍一下这方面的小知识吧. 1.软件开 ...
- 【swupdate文档 三】SWUpdate: 嵌入式系统的软件升级
SWUpdate: 嵌入式系统的软件升级 概述 本项目被认为有助于从存储媒体或网络更新嵌入式系统.但是,它应该主要作为一个框架来考虑,在这个框架中可以方便地向应用程序添加更多的协议或安装程序(在SWU ...
- Oracle-AWR报告简介及如何生成【转】
AWR报告 awr报告是oracle 10g及以上版本提供的一种性能收集和分析工具,它能提供一个时间段内整个系统资源使用情况的报告,通过这个报告,我们就可以了解Oracle数据库的整个运行情况,比如硬 ...
- elasticsearch删除索引报错【原】
如果elasticsearch删除索引报错 curl -X DELETE 'http://10.73.26.66:9200/httpd-34-2017.08.15' {"error" ...
- Linux运维常见问题解决集锦【转】
作为linux运维,多多少少会碰见这样那样的问题或故障,用点心,平时多注意积累,水平肯定越来越高. 下面就是常见问题解决集锦: 1.shell脚本不执行 问题:某天研发某同事找我说帮他看看他写的s ...
- 剑指offer算法题
数组中只出现一次的数字(一个整型数组里除了两个数字之外,其他的数字都出现了两次.请写程序找出这两个只出现一次的数字): 解法在于位运算中的异或,直接异或可以得到这两个数的异或,按照最后的有效数字位可以 ...
- ubuntu上安装MySQL详解
1. 安装 在终端输入 sudo apt-get install mysql-server mysql-client 回车 2.安装完成后检测MySQL的状态 systemctl status my ...
- 出现ERROR: While executing gem ... (Gem::FilePermissionError)这种错误的解决办法
重新安装ruby即可解决 brew install ruby
- csu 1549: Navigition Problem(几何,模拟)
1549: Navigition Problem Time Limit: 1 Sec Memory Limit: 256 MBSubmit: 305 Solved: 90[Submit][Stat ...