網站首頁 編程語言 正文
使用python生成一個圖片驗證碼,隨機的,可以由于驗證人機和別的啊,很方便很簡單
導入模塊
import random from PIL import Image,ImageFont,ImageDraw
生成隨機驗證碼
def rndtxt(): txt_list = [] # 大寫字母 txt_list.extend([i for i in range(65,90)]) # 小寫字母 txt_list.extend([i for i in range(97,123)]) # 數字 txt_list.extend([i for i in range(48,57)]) return chr(txt_list[random.randint(0,len(txt_list)-1)])
作為待會生成的圖片背景色和字體色
def rndbgcolor(): # 背景顏色 return (random.randint(64,255),random.randint(64,255),random.randint(64,255)) def rndtxtcolor2(): # 字體顏色 return (random.randint(32,127),random.randint(32,127),random.randint(32,127))
進行生成打印驗證碼并以圖片的形式打開,不保存圖片文件,僅用于一時的驗證碼驗證
def code(): weight = 240 hight = 60 image = Image.new('RGB',(weight,hight),(255,255,255)) font = ImageFont.truetype('msyh.ttc',36) draw = ImageDraw.Draw(image) # 填充背景顏色 for x in range(weight): for y in range(hight): draw.point((x,y),fill=rndbgcolor()) # 生成隨機驗證碼 for T in range(4): rndtxt_2 = rndtxt() print(rndtxt_2) # 打印驗證碼的值 draw.text((60 * T + 10,10),rndtxt_2,font=font,fill=rndtxtcolor2()) image.show()
完整代碼:
#!/usr/bin/env python3 import random from PIL import Image,ImageFont,ImageDraw def rndtxt(): txt_list = [] # 大寫字母 txt_list.extend([i for i in range(65,90)]) # 小寫字母 txt_list.extend([i for i in range(97,123)]) # 數字 txt_list.extend([i for i in range(48,57)]) return chr(txt_list[random.randint(0,len(txt_list)-1)]) def rndbgcolor(): # 背景顏色 return (random.randint(64,255),random.randint(64,255),random.randint(64,255)) def rndtxtcolor2(): # 字體顏色 return (random.randint(32,127),random.randint(32,127),random.randint(32,127)) def code(): weight = 240 hight = 60 image = Image.new('RGB',(weight,hight),(255,255,255)) font = ImageFont.truetype('msyh.ttc',36) draw = ImageDraw.Draw(image) # 填充背景顏色 for x in range(weight): for y in range(hight): draw.point((x,y),fill=rndbgcolor()) # 生成隨機驗證碼 for T in range(4): rndtxt_2 = rndtxt() print(rndtxt_2) # 打印驗證碼的值 draw.text((60 * T + 10,10),rndtxt_2,font=font,fill=rndtxtcolor2()) image.show() code()
原文鏈接:https://blog.csdn.net/weixin_46625757/article/details/122531906
相關推薦
- 2022-06-30 Pyqt實現簡易計算器功能_python
- 2022-11-20 解析在Tomcat中啟用虛擬線程特性_Tomcat
- 2023-11-14 使用python獲取指定進程的CPU/內存情況;Python獲取指定進程的CPU和內存使用情況
- 2022-09-13 Nginx報錯104:Connection?reset?by?peer問題的解決及分析_nginx
- 2022-07-24 .Net創建型設計模式之建造者、生成器模式(Builder)_基礎應用
- 2023-04-19 nginx: [error] CreateFile() “D:\nginx-1.21.6/logs/
- 2023-02-06 shell腳本實戰之部署nginx腳本實例_nginx
- 2022-11-05 Flutter實現一個支持漸變背景的Button示例詳解_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支