前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python3实现验证码

Python3实现验证码

作者头像
py3study
发布2020-01-10 17:07:34
3330
发布2020-01-10 17:07:34
举报
文章被收录于专栏:python3

Python3 实现创建验证码图片

一:准备工作,需要安装PIL,安装方式,pip install Pillow 二:具体实现 #!/usr/bin/env python3 # coding:UTF-8 """" 文件说明: """ from PIL import Image, ImageDraw, ImageFont import random import string import os def get_code(width=100, height=40, fontSize=35): """ width: 背景图片的宽度 height:背景图片的高度 fontsize:验证码的字体大小 """ img = Image.new("RGB", (width, height), getColor()) # 创建指定大小,背景颜色,模式的图片 draw = ImageDraw.Draw(img) # 创建画刷 path = get_Font() # 得到一个系统下的随机字体路径 # 获取指定路径的字体 font = ImageFont.truetype(font=path, size=fontSize) content = myrandom() # 获取随机生成的验证码的值 # 将验证码画到图片上 draw.text((width * 0.1, height * 0.15), content, fill=getColor(), font=font) # 画干扰线 for i in range(5): x = random.randint(0, 20) y = random.randint(0, height) z = random.randint(width - 20, width) w = random.randint(0, height) draw.line(((x, y), (z, w)), fill=getColor()) # 返回验证码图片与文本内容 return img, content def getColor(): '''随机生成一个元组类型的 RGB颜色''' color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) return color def myrandom(count=5): myList = list(string.ascii_letters + string.digits) # 指定要生成验证码的集合,数字,大小写字母 # 在指定的mylist集合中随机取出count个集合 lists = random.sample(myList, count) # 用指定的字符串连接集合中的内容 return "".join(lists) def get_Font(split='.ttf'): """返回操作系统下的指定后缀的字体""" if os.name == 'nt': path = 'C:\Windows\Fonts'.replace("\\", '/') + '/' else: # 根据不同的操作系统,系统字体在不同的文件下,Ubuntu下可以在/usr/share/fonts/ 下找到很多文件类型的字体 path = '/usr/share/fonts/truetype/freefont/' listFont = os.listdir(path) # 获取指定后缀的字体,默认是.ttf类型的后缀 fontList = [path + x for x in listFont if os.path.splitext(x)[1] == split] # 返回系统字体列表,以及所在的路径 # return fontList, path # 随机返回一个字体 path = random.sample(fontList, 1) return path[0] if __name__ == '__main__': img, content = get_code() img.show() 三:上述代码可以直接运行

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/08/22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Python3 实现创建验证码图片
相关产品与服务
验证码
腾讯云新一代行为验证码(Captcha),基于十道安全栅栏, 为网页、App、小程序开发者打造立体、全面的人机验证。最大程度保护注册登录、活动秒杀、点赞发帖、数据保护等各大场景下业务安全的同时,提供更精细化的用户体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档