本篇文章主要介紹了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.ArarayoveralistinpythonwhendeAlingwithHomoGeneData,performance-Caliticalcode,orinterfacingwithccode.1)同質性data:arraysSaveMemorywithTypedElements.2)績效code-performance-calitialcode-calliginal-clitical-clitical-calligation-Critical-Code:Arraysofferferbetterperbetterperperformanceformanceformancefornallancefornalumericalical.3)

不,notalllistoperationsareSupportedByArrays,andviceversa.1)arraysdonotsupportdynamicoperationslikeappendorinsertwithoutresizing,wheremactsperformance.2)listssdonotguaranteeconecontanttanttanttanttanttanttanttanttanttimecomplecomecomplecomecomecomecomecomecomplecomectacccesslectaccesslecrectaccesslerikearraysodo。

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
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器