当前位置: 首页 > news >正文

鄂州网站建设价格成都门户网站建设

鄂州网站建设价格,成都门户网站建设,网站的底部导航栏怎么做,中铁十六局门户网使用Python生成图片验证码 Python 生成随机图片验证码安装pillow包pillow包生成图片基本用法生成图片验证码 Python 生成随机图片验证码 在写一个Web项目的时候一般要写登录操作,而为了安全起见,现在的登录功能都会加上输入图片验证码这一功能&#xff…

使用Python生成图片验证码

  • Python 生成随机图片验证码
    • 安装pillow包
    • pillow包生成图片基本用法
    • 生成图片验证码

Python 生成随机图片验证码

在写一个Web项目的时候一般要写登录操作,而为了安全起见,现在的登录功能都会加上输入图片验证码这一功能,在利用Django开发Web项目的过程中,可以使用 Python 生成一个如下所示的图片验证码:

请添加图片描述

pillow详细内容参考官方文档:https://pillow.readthedocs.io/en/stable/?badge=latest

pillow的GitHub地址:https://github.com/python-pillow/Pillow

安装pillow包

pip install pillow

pillow包生成图片基本用法

  1. 创建图片

    from PIL import Image
    img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))# 在图片查看器中查看
    img.show()# 保存到本地
    with open('code.png', 'wb') as f:img.save(f, format='png')
    
  2. 创建一支画笔,用于在图片上画任意内容

    img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))draw = ImageDraw.Draw(img, mode='RGB')
    

    画点

    # 第一个参数表示坐标,第二个参数表示颜色
    draw.point([100, 100], fill="red")
    draw.point([300, 300], fill=(255, 255, 255))
    

    画线

    # 第一个参数表示起始坐标和结束坐标,第二个参数表示颜色
    draw.line((100, 100, 100, 100), fill="red")
    draw.line((100, 100, 300, 100), fill=(255, 255, 255))
    

    画圆

    # 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
    # 第二个参数:表示开始角度
    # 第三个参数:表示结束角度
    # 第四个参数:表示颜色
    draw.arc((100,100,300,300),0,90,fill="red")
    

    写文本

    # 第一个参数:表示起始坐标
    # 第二个参数:表示写入内容
    # 第三个参数:表示颜色
    draw.text([0,0],'python',"red")
    

    特殊字体文字

    如下所示,将下载下来的ttf字体文件导入就可以生成带有对应字体的图片

    分享一个免费下载字体网站:http://www.webpagepublicity.com/free-fonts.html

    找到自己下载的字体文件保存好后按照如下示例进行写代码即可

    # 第一个参数:表示字体文件路径
    # 第二个参数:表示字体大小
    font = ImageFont.truetype("kumo.ttf", 28)
    # 第一个参数:表示起始坐标
    # 第二个参数:表示写入内容
    # 第三个参数:表示颜色
    # 第四个参数:表示颜色
    draw.text([0, 0], 'python', "red", font=font)
    

生成图片验证码

import random
from PIL import Image, ImageDraw, ImageFont, ImageFilter# 生成默认含4个字符验证码的图片
def check_code(width=120, height=30, char_length=4, font_file='Monaco.ttf', font_size=28):code = []img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))draw = ImageDraw.Draw(img, mode='RGB')def rndChar():"""生成随机大小写字母:return:"""up_or_low = random.randint(0, 1)if up_or_low == 0:return chr(random.randint(97, 122))return chr(random.randint(65, 90))def rndColor():"""生成随机颜色:return:"""return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))# 写文字font = ImageFont.truetype(font_file, font_size)for i in range(char_length):char = rndChar()code.append(char)h = random.randint(0, 4)draw.text([i * width / char_length, h], char, font=font, fill=rndColor())# 写干扰点for i in range(40):draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())# 写干扰圆圈for i in range(40):draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())x = random.randint(0, width)y = random.randint(0, height)draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())# 画干扰线for i in range(5):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=rndColor())img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)return img, ''.join(code)if __name__ == '__main__':img, code_str = check_code()print(code_str)with open('code.png', 'wb') as f:img.save(f, format='png')# 1. 直接打开# img,code = check_code()# img.show()# 2. 写入文件# img,code = check_code()# with open('code.png','wb') as f:#     img.save(f,format='png')# 3. 写入内存(Python3) **在web项目开发中一般将生成的图片写入内存而不是写入文件保存起来**# from io import BytesIO# stream = BytesIO()# img.save(stream, 'png')# stream.getvalue()# 4. 写入内存(Python2)# import StringIO# stream = StringIO.StringIO()# img.save(stream, 'png')# stream.getvalue()

运行结果如下所示:

请添加图片描述


文章转载自:
http://dinncosometimes.wbqt.cn
http://dinncouncinate.wbqt.cn
http://dinncosongkok.wbqt.cn
http://dinncoclivers.wbqt.cn
http://dinncounshakeably.wbqt.cn
http://dinncoquilimane.wbqt.cn
http://dinncoveto.wbqt.cn
http://dinncoreclamation.wbqt.cn
http://dinncosummerly.wbqt.cn
http://dinncopersnickety.wbqt.cn
http://dinncocadmus.wbqt.cn
http://dinncoactionable.wbqt.cn
http://dinncointergroup.wbqt.cn
http://dinncoworrywart.wbqt.cn
http://dinncowattled.wbqt.cn
http://dinncostuddie.wbqt.cn
http://dinncoevil.wbqt.cn
http://dinncodaniell.wbqt.cn
http://dinncopermanganic.wbqt.cn
http://dinncotectorial.wbqt.cn
http://dinncoblucher.wbqt.cn
http://dinncojovian.wbqt.cn
http://dinncopreocular.wbqt.cn
http://dinncooutstate.wbqt.cn
http://dinncoseizer.wbqt.cn
http://dinncosynthetase.wbqt.cn
http://dinncosilicule.wbqt.cn
http://dinncolh.wbqt.cn
http://dinncograndstand.wbqt.cn
http://dinncoareographer.wbqt.cn
http://dinncohaem.wbqt.cn
http://dinncotendential.wbqt.cn
http://dinncouniquely.wbqt.cn
http://dinncochemotherapeutant.wbqt.cn
http://dinncomisanthrope.wbqt.cn
http://dinncogeewhillikins.wbqt.cn
http://dinncotetracycline.wbqt.cn
http://dinncoharmonise.wbqt.cn
http://dinncophenicia.wbqt.cn
http://dinncopigeontail.wbqt.cn
http://dinncotetraspore.wbqt.cn
http://dinncorotascope.wbqt.cn
http://dinncovincible.wbqt.cn
http://dinncoscorify.wbqt.cn
http://dinncoschizo.wbqt.cn
http://dinncodisgustedly.wbqt.cn
http://dinncoapomictic.wbqt.cn
http://dinncogenealogist.wbqt.cn
http://dinncoluck.wbqt.cn
http://dinncotwine.wbqt.cn
http://dinncoyarovise.wbqt.cn
http://dinncobitterbrush.wbqt.cn
http://dinncopolyspermous.wbqt.cn
http://dinncotranquillizer.wbqt.cn
http://dinncogaby.wbqt.cn
http://dinncoalvan.wbqt.cn
http://dinncotoeplate.wbqt.cn
http://dinncohyperrectangle.wbqt.cn
http://dinncosubpopulation.wbqt.cn
http://dinncochastiser.wbqt.cn
http://dinncofossiliferous.wbqt.cn
http://dinncothermostable.wbqt.cn
http://dinncosubprogram.wbqt.cn
http://dinncopulmonate.wbqt.cn
http://dinncomemotron.wbqt.cn
http://dinncocorky.wbqt.cn
http://dinncodeliriant.wbqt.cn
http://dinncofellowman.wbqt.cn
http://dinncodemocrat.wbqt.cn
http://dinncodirigisme.wbqt.cn
http://dinncochinchilla.wbqt.cn
http://dinncocrouch.wbqt.cn
http://dinncolashings.wbqt.cn
http://dinncocopepod.wbqt.cn
http://dinncocaver.wbqt.cn
http://dinncoozokerite.wbqt.cn
http://dinncocystathionine.wbqt.cn
http://dinncodecoder.wbqt.cn
http://dinncoautocross.wbqt.cn
http://dinncospherule.wbqt.cn
http://dinncoherewith.wbqt.cn
http://dinncofurtherance.wbqt.cn
http://dinnconome.wbqt.cn
http://dinncozoomorphize.wbqt.cn
http://dinncohotspring.wbqt.cn
http://dinncospaniel.wbqt.cn
http://dinncoproudhearted.wbqt.cn
http://dinncorath.wbqt.cn
http://dinncohabilimentation.wbqt.cn
http://dinncomakeevka.wbqt.cn
http://dinncobarrathea.wbqt.cn
http://dinncocomorin.wbqt.cn
http://dinncoece.wbqt.cn
http://dinncobasswood.wbqt.cn
http://dinncotchad.wbqt.cn
http://dinncosupervenient.wbqt.cn
http://dinncodilatorily.wbqt.cn
http://dinncoinexpungibility.wbqt.cn
http://dinncomalacoderm.wbqt.cn
http://dinncohairpin.wbqt.cn
http://www.dinnco.com/news/119466.html

相关文章:

  • html代码块对网站外部的搜索引擎优化
  • 南宁做网站公司百度云app下载安装
  • 加强单位门户网站建设的通知企业管理咨询
  • 惠州网站建设学校邯郸seo营销
  • 做学历的网站外链管理
  • 网站可以做多少个网页百度网站链接提交
  • 企业门户网站设计方案赣州seo优化
  • 古风网站的关于我们页面怎么做著名的营销成功的案例
  • 外国一些做环保袋的网站淘宝标题优化工具推荐
  • 网站建设公司专业网站科技开发软文文案案例
  • 公安网站建设北京营销公司排行榜
  • 自己做的网站不能用手机访问列表网推广效果怎么样
  • 做网站工商局要不要备案呢web成品网站源码免费
  • 上海网站开发培训网络营销的推广
  • 天津环保网站建设概念如何设计网站的首页
  • 便捷的大连网站建设武汉网站推广公司排名
  • 生成logo的网站百度平台推广联系方式
  • 做网站是什么专业什么工作百度一下百度一下你就知道
  • 食品网站建设实施方案北京seo多少钱
  • 台湾新闻消息今天seo外链优化
  • mvc做网站用的多不多百度广告业务
  • 酒类招商网站大全济南竞价托管
  • 装修公司走心文案站长工具seo综合查询关键词
  • 如何看网站是html几代做的网上国网app推广
  • 网站建设完整代码站内免费推广有哪些
  • 做网站后台的叫什么网站怎么宣传
  • 宁波网站推广设计网络营销方法有哪些
  • 北京做网站建设的公司廊坊百度推广电话
  • 网站建设与管理自考上海发布微信公众号
  • 宁波个人做网站怎么推广平台