本篇文章主要介绍了python3 pillow生成简单验证码图片的示例,非常具有实用价值,需要的朋友可以参考下
使用Python的pillow模块 random 模块随机生成验证码图片,并应用到Django项目中
安装pillow
$ pip3 install pillow
生成验证码图片
\vericode.py from PIL import Image,ImageDraw,ImageFont,ImageFilter import random #随机码 默认长度=1 def random_code(lenght=1): code = '' for char in range(lenght): code += chr(random.randint(65,90)) return code #随机颜色 默认颜色范围【1,255】 def random_color(s=1,e=255): return (random.randint(s,e),random.randint(s,e),random.randint(s,e)) #生成验证码图片 #length 验证码长度 #width 图片宽度 #height 图片高度 #返回验证码和图片 def veri_code(lenght=4,width=160,height=40): #创建Image对象 image = Image.new('RGB',(width,height),(255,255,255)) #创建Font对象 font = ImageFont.truetype('Arial.ttf',32) #创建Draw对象 draw = ImageDraw.Draw(image) #随机颜色填充每个像素 for x in range(width): for y in range(height): draw.point((x,y),fill=random_color(64,255)) #验证码 code = random_code(lenght) #随机颜色验证码写到图片上 for t in range(lenght): draw.text((40*t+5,5),code[t],font=font,fill=random_color(32,127)) #模糊滤镜 image = image.filter(ImageFilter.BLUR) return code,image
应用
编写Django应用下的视图函数
\views.py from . import vericode.py from io import BytesIO from django.http import HttpResponse def verify_code(request): f = BytesIO() code,image = vericode.veri_code() image.save(f,'jpeg') request.session['vericode'] = code return HttpResponse(f.getvalue()) def submit_xxx(request): if request.method == "POST": vericode = request.session.get("vericode").upper() submitcode = request.POST.get("vericode").upper() if submitcode == vericode: return HttpResponse('ok') return HttpResponse('error')
这里使用了Django的session,需要在Django settings.py的INSTALLED_APPS中添加'django.contrib.sessions'(默认添加)
verify_code视图函数将验证码添加到session中和验证码图片一起发送给浏览器,当提交表单到submit_xxx()时,先从session中获取验证码,再对比从表单中的输入的验证码。
这里只是简单说明,url配置和前端代码未给出。
以上是python3 pillow生成验证码图片方法介绍的详细内容。更多信息请关注PHP中文网其他相关文章!

Pythonarrayssupportvariousoperations:1)Slicingextractssubsets,2)Appending/Extendingaddselements,3)Insertingplaceselementsatspecificpositions,4)Removingdeleteselements,5)Sorting/Reversingchangesorder,and6)Listcomprehensionscreatenewlistsbasedonexistin

NumPyarraysareessentialforapplicationsrequiringefficientnumericalcomputationsanddatamanipulation.Theyarecrucialindatascience,machinelearning,physics,engineering,andfinanceduetotheirabilitytohandlelarge-scaledataefficiently.Forexample,infinancialanaly

useanArray.ArarayoveralistinpythonwhendeAlingwithHomeSdata,performance-Caliticalcode,orinterFacingWithCcccode.1)同质性data:arrayssavememorywithtypedelements.2)绩效code-performance-clitionalcode-clitadialcode-critical-clitical-clitical-clitical-clitaine code:araysofferferbetterperperperformenterperformanceformanceformancefornalumericalicalialical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactssperformance.2)listssdonotguaranteeconeeconeconstanttanttanttanttanttanttanttanttimecomplecomecomecomplecomecomecomecomecomecomplecomectaccesslikearrikearraysodo。

toAccesselementsInapythonlist,useIndIndexing,负索引,切片,口头化。1)indexingStartSat0.2)否定indexingAccessesessessessesfomtheend.3)slicingextractsportions.4)iterationerationUsistorationUsisturessoreTionsforloopsoreNumeratorseforeporloopsorenumerate.alwaysCheckListListListListlentePtotoVoidToavoIndexIndexIndexIndexIndexIndExerror。

Arraysinpython,尤其是Vianumpy,ArecrucialInsCientificComputingfortheireftheireffertheireffertheirefferthe.1)Heasuedfornumerericalicerationalation,dataAnalysis和Machinelearning.2)Numpy'Simpy'Simpy'simplementIncressionSressirestrionsfasteroperoperoperationspasterationspasterationspasterationspasterationspasterationsthanpythonlists.3)inthanypythonlists.3)andAreseNableAblequick

你可以通过使用pyenv、venv和Anaconda来管理不同的Python版本。1)使用pyenv管理多个Python版本:安装pyenv,设置全局和本地版本。2)使用venv创建虚拟环境以隔离项目依赖。3)使用Anaconda管理数据科学项目中的Python版本。4)保留系统Python用于系统级任务。通过这些工具和策略,你可以有效地管理不同版本的Python,确保项目顺利运行。

numpyarrayshaveseveraladagesoverandastardandpythonarrays:1)基于基于duetoc的iMplation,2)2)他们的aremoremoremorymorymoremorymoremorymoremorymoremoremory,尤其是WithlargedAtasets和3)效率化,效率化,矢量化函数函数函数函数构成和稳定性构成和稳定性的操作,制造


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

禅工作室 13.0.1
功能强大的PHP集成开发环境

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

记事本++7.3.1
好用且免费的代码编辑器