首先

pip install pillow

然后

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont import random class ValidCodeImg:
def __init__(self, width=175, height=40, code_count=6, font_size=28, point_count=20, line_count=3,
img_format='png'):
'''
可以生成一个经过降噪后的随机验证码的图片
:param width: 图片宽度 单位px
:param height: 图片高度 单位px
:param code_count: 验证码个数
:param font_size: 字体大小
:param point_count: 噪点个数
:param line_count: 划线个数
:param img_format: 图片格式
:return 生成的图片的bytes类型的data
'''
self.width = width
self.height = height
self.code_count = code_count
self.font_size = font_size
self.point_count = point_count
self.line_count = line_count
self.img_format = img_format @staticmethod
def get_random_color():
'''获取一个随机颜色(r,g,b)格式的'''
c1 = random.randint(0, 255)
c2 = random.randint(0, 255)
c3 = random.randint(0, 255)
return (c1, c2, c3) @staticmethod
def get_random_string():
'''获取一个随机字符串,每个字符的颜色也是随机的'''
random_num = str(random.randint(0, 9))
random_low_alpha = chr(random.randint(97, 122))
random_upper_alpha = chr(random.randint(65, 90))
random_char = random.choice([random_num, random_low_alpha, random_upper_alpha])
return random_char def get_valid_code_img(self):
# 获取一个Image对象,参数分别是RGB模式。宽150,高30,随机颜色
image = Image.new('RGB', (self.width, self.height), self.get_random_color()) # 获取一个画笔对象,将图片对象传过去
draw = ImageDraw.Draw(image) # 获取一个font字体对象参数是ttf的字体文件的目录,以及字体的大小
font = ImageFont.truetype("kumo.ttf", size=self.font_size) temp = []
for i in range(self.code_count):
# 循环5次,获取5个随机字符串
random_char = self.get_random_string() # 在图片上一次写入得到的随机字符串,参数是:定位,字符串,颜色,字体
draw.text((4 + i * 30, 10), random_char, self.get_random_color(), font=font) # 保存随机字符,以供验证用户输入的验证码是否正确时使用
temp.append(random_char)
valid_str = "".join(temp) # 噪点噪线
# 划线
for i in range(self.line_count):
x1 = random.randint(0, self.width)
x2 = random.randint(0, self.width)
y1 = random.randint(0, self.height)
y2 = random.randint(0, self.height)
draw.line((x1, y1, x2, y2), fill=self.get_random_color()) # 画点
for i in range(self.point_count):
draw.point([random.randint(0, self.width), random.randint(0, self.height)], fill=self.get_random_color())
x = random.randint(0, self.width)
y = random.randint(0, self.height)
draw.arc((x, y, x + 4, y + 4), 0, 90, fill=self.get_random_color()) # 在内存生成图片
from io import BytesIO
f = BytesIO()
image.save(f, self.img_format)
data = f.getvalue()
f.close() return data, valid_str def create_img(self):
data, valid_str = self.get_valid_code_img()
f = open('test.png', 'wb')
f.write(data)
f.close() return valid_str if __name__ == '__main__':
img = ValidCodeImg()
strs = img.create_img()
print(strs)

封装好了,直接拿去用,另外附字体

kumo.ttf

百度下下来,然后放在文件目录同级目录下即可

python生成图片二维码(利用pillow)的更多相关文章

  1. 使用 Python 生成二维码

    在“一带一路”国际合作高峰论坛举行期间, 20 国青年投票选出中国的“新四大发明”:高铁.扫码支付.共享单车和网购.其中扫码支付指手机通过扫描二维码跳转到支付页面,再进行付款.这种新的支付方式,造就二 ...

  2. 从Scratch到Python——Python生成二维码

    # Python利用pyqrcode模块生成二维码 import pyqrcode import sys number = pyqrcode.create('从Scratch到Python--Pyth ...

  3. 用python生成二维码

    Python生成二维码,可以使用qrcode模块, github地址 我是搬运工 首先安装, 因为打算生成好再展示出来,所以用到Pillow模块 pip install qrcode pip inst ...

  4. Python 实现二维码生成和识别

    今天突然想给自己自己做个头像,然后还是二维码的形式,这样只要扫一扫就可以访问我的主页.然后就开始自己的苦逼之路... 其实实现二维码java,c#,C++等都可以实现:由于自己正在学python,所以 ...

  5. 有关python下二维码识别用法及识别率对比分析

    最近项目中用到二维码图片识别,在python下二维码识别,目前主要有三个模块:zbar .zbarlight.zxing. 1.三个模块的用法: #-*-coding=utf-8-*- import ...

  6. 利用Python制作二维码

    利用简单的Python代码制作二维码 一.制作工具 安装Python环境 + PyCharm编译器. 二.电脑系统 本人win10 + Python3.7.0 + PyCharm. 三.写代码前先下载 ...

  7. Python制作二维码和条形码扫描器 (pyzbar)

    条码在生活中随处可见,其可分为三类:一维条码.二维条码.三维条码 一维条码: 我们平时习惯称为条形码.条形码是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.常见的条 ...

  8. 使用python制作二维码

    python-qrcode是个用来生成二维码图片的第三方模块,主要依赖的是 PIL 模块和 qrcode 库.(PIL模块只支持python2.7及以下版本,python3之后无法使用,官方推荐pyt ...

  9. Python生成二维码脚本

    简单的记录下二维码生成和解析的Python代码 依赖下面三个包: PIL(图像处理包,安装:pip install PIL) qrcode(二维码生成包,安装:pip install qrcode) ...

随机推荐

  1. c# 自定义按钮,渐变颜色(含中心向四周渐变,单方向渐变)

    废话不多言,直接代码: public class RoundButton : Button { bool clickBool = false; //1.设置圆形 //2.设置渐变色 //3.设置too ...

  2. html中利用flex容器书写的布局样式

    首先页面基本样式见下图: 如有兴趣可以打开https://migloo.gitee.io/front 或者 https://www.igloo.xin/front 进行查看

  3. 爬虫(一)基础知识(python)

    1.1 定义 网络爬虫,也叫网络蜘蛛(Web Spider),如果把互联网比喻成一个蜘蛛网,Spider就是一只在网上爬来爬去的蜘蛛.网络爬虫就是根据网页的地址来寻找网页的,也就是URL.举一个简单的 ...

  4. PS 个人常用功能

    PS是什么? Adobe Photoshop,简称"PS",是由Adobe Systems开发和发行的图像处理软件. 不是美工,为什么要学PS? 1)写博客时,有些需要的素材图片有 ...

  5. asp.net调用c++的dll

    只需要把dll文件拷贝到windows的system32目录下(64位系统为SysWOW64目录),如果操作系统为64位而dll为32位,还需在进程池启用32位支持.

  6. xcode添加一个真机设备

    1.首先先安装Xcode并且运行Xcode,点击左角菜单Xcode -> Preferences:点击Accounts+号弹菜单点击Add Apple ID:弹框输入账号密码普通账号行需要发者账 ...

  7. 029 ElasticSearch----全文检索技术04---基础知识详解02-查询

    1.查询 (1)基本查询 基本语法: GET /索引库名/_search { "query":{ "查询类型":{ "查询条件":" ...

  8. SpringBoot扩展点之三:SpringBootServletInitializer扩展

    SpringBootServletInitializer 熟悉了SpringApplication的原理之后,我们再来了解SpringBootServletInitializer的原理就比较容易了. ...

  9. 一个 Java 正则表达式例子

    今天在项目里看到用 Python 正则表达式的时候,用到 group,没有仔细看.正好学习 Java 正则表达式,对 group 多留意了一下. 上代码: import java.util.regex ...

  10. .net core Identity注册用户 出错

    使用微软自带的注册 报 NotSupportedException: No IUserTwoFactorTokenProvider<TUser> named 'Default' is re ...