基于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"> ...
随机推荐
- XAML绑定到资源文件字符串时失败
参考:https://stackoverflow.com/questions/19586401/error-in-binding-resource-string-with-a-view-in-wpf ...
- Greenplum和Postgresql的主键自增
参考:https://blog.csdn.net/u011042248/article/details/49422305 1.第一种情况就是创建数据表的时候创建主键自增,由于业务需要自己的数据表已经创 ...
- Caused by: java.net.ConnectException: Connection refused/Caused by: java.lang.RuntimeException: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
1.使用sqoop技术将mysql的数据导入到Hive出现的错误如下所示: 第一次使用命令如下所示: [hadoop@slaver1 sqoop--cdh5.3.6]$ bin/sqoop impor ...
- UE4 UPROPERTY UFUNCTION
http://blog.csdn.net/sinat_27456831/article/details/52800514
- java keystore
JAVA有一个keystore用来存放私钥和证书,该文件是伴随JDK默认存在的,路径默认是/lib/security/cacerts,默认密码是changeit,实际上空密码也可以直接访问 其中cac ...
- C# 之 HttpRequest 类
Request对象派生自HttpRequest类,使 ASP.NET 能够读取客户端在 Web 请求期间发送的 HTTP 值,从客户端获取信息,浏览器的种类,用户输入表单的数据,Cooki ...
- WebApi 得到提交过来的 post 数据
byte[] byts = new byte[System.Web.HttpContext.Current.Request.InputStream.Length]; System.Web.HttpCo ...
- jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法
新年第一编文章 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法 $("#myELement") 选择id值等于myElement的元素,id值 ...
- 安卓开发中SpannableString之富文本显示效果
SpannableString其实和String一样,都是一种字符串类型,SpannableString可以直接作为TextView的显示文本,不同的是SpannableString可以通过使用其方法 ...
- 使用Chrome浏览器设置XX-net的方法
以下介绍使用Chrome浏览器设置XX-net的方法 1.下载并安装谷歌浏览器. 2.打开https://github.com/XX-net/XX-Net/blob/master/code/d ...