基于Python Pillow库生成随机验证码
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import random class ValidCodeImg:
"""
可以生成一个经过降噪后的随机验证码的图片
:param width: 图片宽度 单位px
:param height: 图片高度 单位px
:param code_count: 验证码个数
:param font_size: 字体大小
:param point_count: 噪点个数
:param line_count: 划线个数
:param img_format: 图片格式
"""
__str = ""
def __init__(self, width=150, height=30, code_count=5, font_size=32, point_count=20, line_count=3, img_format='png'):
image = Image.new('RGB', (width, height), self.getRandomColor())
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('kumo.ttf', size=font_size)
for i in range(code_count):
random_str = self.getRandomStr()
self.__str += random_str
draw.text((10 + i * height, 0), random_str, self.getRandomColor(), font=font)
for i in range(line_count):
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=self.getRandomColor())
for i in range(point_count):
draw.point([random.randint(0, width), random.randint(0, height)], fill=self.getRandomColor())
x = random.randint(0, width)
y = random.randint(0, height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=self.getRandomColor())
image.save(open('test.%s'%img_format, 'wb'), img_format)
def getstr(self):
return self.__str
def getRandomColor(self):
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
def getRandomStr(self):
random_number = str(random.randint(0, 9))
random_low_alpha = chr(random.randint(97, 122))
random_uppper_alpha = chr(random.randint(65, 90))
return random.choice([random_number, random_low_alpha, random_uppper_alpha])
其中ImageFont.truetype()的第一个参数为字体类型, 需为用户有的*.ttf格式的字体文件。
效果:
基于Python Pillow库生成随机验证码的更多相关文章
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
- Python利用PIL生成随机验证码图片
安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...
- 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使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- PHP5 GD库生成图形验证码(汉字)
PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
随机推荐
- .NET成年了,然后呢?
作者|Lex Li 编辑|郭蕾 这可能是唯一一篇系统回顾 .NET 发展的文章..NET 的成年礼到了,你会送它什么? 2014 年 11 月 12 日,美国纽约曼哈顿,多云,气温适宜.微软公司执行副 ...
- Choosing web framework: ASP.NET MVC vs Django Python vs Ruby on Rails(转载)
来源:http://podlipensky.com/2012/06/choosing-web-framework-asp-net-mvc-vs-django-python-vs-ruby-on-rai ...
- angular 2+ 变化检测系列一(基础概念)
什么是变化检测? 变化检测的基本功能就是获取应用程序的内部状态(state),并且是将这种状态对用户界面保持可见.状态可以是javascript中的任何的数据结构,比如对象,数组,(数字,布尔,字符串 ...
- Spring Security 架构与源码分析
Spring Security 主要实现了Authentication(认证,解决who are you? ) 和 Access Control(访问控制,也就是what are you allowe ...
- python基础——list和tuple(列表和元组)
1.list的定义,插入insert,append,按位置索引. >>> name = ['Macal','lily','lucy','bob'] --初始化>>> ...
- 使用impala操作kudu之创建kudu表(内部表和外部表)
依次启动HDFS.mysql.hive.kudu.impala 登录impala的shell控制端: Impala-shell 1:使用该impala-shell命令启动Impala Shell .默 ...
- Python学习(十八)—— 数据库(三)
转载自http://www.cnblogs.com/linhaifeng/articles/7356064.html 一.数据操作 1.插入数据INSERT 1. 插入完整数据(顺序插入) 语法一: ...
- HttpWatch入门使用教程
HttpWatch V10.0.20.0 官方免费版 HttpWatch是强大的网页数据分析工具.集成... HttpWatch Professional V10.0.20.0 官方下载 HttpWa ...
- ERROR 1215 (HY000): Cannot add foreign key constraint
MySQL中在为一个varchar类型数据列添加外键时,会发生上面所示的错误,这里我google了一下,感觉它们碰到的问题跟我这个说的有点不相干,尝试了多种方式后来才发现是:主表(table1)所对应 ...
- HYSBZ 4034 【树链剖分】+【线段树 】
<题目链接> 题目大意: 有一棵点数为 N 的树,以点 1 为根,且树点有权值.然后有 M 个 操作,分为三种: 操作 1 :把某个节点 x 的点权增加 a . 操作 2 :把某个节点 x ...