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库生成随机验证码的更多相关文章

  1. pillow实例 | 生成随机验证码

    1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...

  2. Python利用PIL生成随机验证码图片

    安装pillow: pip install pillow PIL中的Image等模块提供了创建图片,制作图片的功能,大致的步骤就是我们利用random生成6个随机字符串,然后利用PIL将字符串绘制城图 ...

  3. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  4. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  5. Python使用PIL模块生成随机验证码

    PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...

  6. Django中生成随机验证码(pillow模块的使用)

    Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...

  7. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  8. PHP5 GD库生成图形验证码(汉字)

    PHP5 GD库生成图形验证码且带有汉字的实例分享. 1,利用GD库函数生成图片,并在图片上写指定字符imagecreatetruecolor 新建一个真彩色图像imagecolorallocate ...

  9. C#生成随机验证码例子

    C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...

随机推荐

  1. 在Git.oschina.net中配置TortoiseGit使用sshkey,无需输入账号和密码

    ssh的方式 git@oschina.com:用户名/版本库t.git           此篇文章针对于这种 黄海正在开发的项目位置 https://gitee.com/dslx/BigData.g ...

  2. 【SPFA与Dijkstra的对比】CDOJ 1961 咸鱼睡觉觉【差分约束-负权最短路径SPFA】

    差分约束系统,求最小值,跑最长路. 转自:https://www.cnblogs.com/ehanla/p/9134012.html 题解:设sum[x]为前x个咕咕中至少需要赶走的咕咕数,则sum[ ...

  3. noi.ac 第五场第六场

    t1应该比较水所以我都没看 感觉从思路上来说都不难(比牛客网这可简单多了吧) 第五场 t2: 比较套路的dp f[i]表示考虑前i个数,第i个满足f[i]=i的最大个数 i能从j转移需要满足 j< ...

  4. alpha冲刺1/10

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:来自双十一的爱 团队部分 后敬甲(组长) 过去两天完成了哪些任务 文字描述 Alpha版本的任务细分安排 leangoo ...

  5. 关于appium-doctor运行时提示不是内部或外部的命令

    1.一定要单独配置android_home (我之前是直接将D:\SDK\platform-tools;D:\SDK\tools;加到path里面会导致appnium-doctor运行时失败,原因为A ...

  6. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...

  7. 事件(Event)(onclick,onchange,onload,onunload,onfocus,onblur,onselect,onmuse)【转载】

    ylbtech-Event:事件(Event)对象 事件(Event) HTML 4.0 事件属性 onclick onchange onload onunload onselect onmouse ...

  8. 015 OS模块

    这个部分,也不是很难,就懒得写程序了,粘贴了一个不错的连接. 1.说明 os模块提供了多数操作系统的功能接口函数. 当os模块被导入后,它会自适应于不同的操作系统平台,根据不同的平台进行相应的操作,在 ...

  9. elementui异步后台校验表单,修改重复校验

    elementui简单的form校验这里就不介绍了,这里主要记录下如何通过后台进行指定字段的异步后台校验. 1.导入axios <script src="https://unpkg.c ...

  10. POJ2387 Til the Cows Come Home【Kruscal】

    题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...