基于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"> ...
随机推荐
- IntelliJ Idea 配置Tomcat提示Port is not specified
修改Debug这里的端口就好了
- 现在k8s新版里,如何在每个node上运行一个带privileged的daemonset
以前,我们会在kubelet上加--allow-prividged启动参数来实现. 而现在,更推荐的是用pod secureity policy来实现.前面的那种方式以后会被废弃. https://k ...
- 常用js正则表达式大全
常用js正则表达式大全.一.校验数字的js正则表达式 1 数字:^[0-9]*$ 2 n位的数字:^\d{n}$ 3 至少n位的数字:^\d{n,}$ 4 m-n位的数字:^\d{m,n}$ 5 零和 ...
- 【AtCoder】ARC072
ARC072 C - Sequence 直接认为一个数是正的,或者第一个数是负的,每次将不合法的负数前缀和改成+1正数前缀和改成-1 #include <bits/stdc++.h> #d ...
- excel追加数据
原本是想通过读取已存在的文件的行然后直接添加保存,发现结果会被覆盖. 后来查找方法发现需要复制原表. 函数参数: list:要添加的数据 filename:目标文件 sheet_index:默认表的第 ...
- python 将列表(也可以是file.readlines())输出多个文件
open_diff = open('yanggao.txt', 'r',encoding='utf-8')diff_line = open_diff.readlines() diff_match_sp ...
- k8s 英文文档翻译
https://medium.com/google-cloud/understanding-kubernetes-networking-pods-7117dd28727
- ThinkPHP页面跳转success与error方法
首先是控制器中,可以使用下代码: config配置如下: 'TMPL_ACTION_ERROR' => 'Public:error', // 默认错误跳转对应的模板文件 'TMPL_ACTION ...
- AtCoder Grand Contest 1~10 做题小记
原文链接https://www.cnblogs.com/zhouzhendong/p/AtCoder-Grand-Contest-from-1-to-10.html 考虑到博客内容较多,编辑不方便的情 ...
- POJ1469 COURSES 二分图匹配 匈牙利算法
原文链接http://www.cnblogs.com/zhouzhendong/p/8232649.html 题目传送门 - POJ1469 题意概括 在一个大矩阵中,有一些障碍点. 现在让你用1*2 ...