網站首頁 編程語言 正文
最近研究驗證碼識別,需要生成大量驗證碼,最方便的是使用captcha
庫來生成驗證碼,網上代碼僅僅使用默認設置,但是它還有很多參數可以設定,于是我又添加了一些參數,方便使用。
效果圖
參數解析
在上述案例中,完整命令為
python captcha_gen.py w=210 h=100 n=6 c=2 fontsize=[40,50,60]
其中:
-
w=210
指定驗證碼圖片寬度 -
h=100
指定驗證碼圖片高度 -
n=6
指定驗證碼字符長度 -
c=2
指定生成的驗證碼圖片數量 -
fontsize=[40,50,60]
指定驗證碼字體大小(單個字符的字體大小從中隨機選取)
參數默認值都寫在代碼里了。
好像我是唯一一個使用這種方式輸入captcha
參數的。
安裝相關庫
首先安裝兩個庫:
pip install captcha pip install pillow
完整源碼
#!/usr/bin/env python # -*- coding:utf-8 -*- # Use `pip install captcha` and `pip install pillow` to install dependencies ### from tkinter import font from captcha.image import ImageCaptcha from PIL import Image import random import time import os,sys,ast NUMBER = ['0','1','2','3','4','5','6','7','8','9'] alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'] ALPHABET = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'] ALL_CHAR_SET = NUMBER + alphabet + ALPHABET #最終生成的驗證碼數量 count = 10 #參數: 寬度、高度、字符長度、字體大小 img_width,img_height,labelno,fontsize = 200,100,4,[42, 50, 56] def mylist(x): if isinstance(x,(list,tuple)): return x else: return [x] def get_argv(): global img_width,img_height,labelno,fontsize,count if len(sys.argv) > 1: for i in range(1,len(sys.argv)): arg = sys.argv[i].split('=') if arg[0]=='w': img_width = int(arg[1]) if arg[0]=='h': img_height = int(arg[1]) if arg[0]=='n': labelno = int(arg[1]) if arg[0]=='c': count = int(arg[1]) if arg[0] == 'fontsize': # fontsize = mylist(int(arg[1])) fontsize = ast.literal_eval(arg[1]) print(f'img_width:{img_width}, img_height:{img_height}, labelno:{labelno},fonsize:{fontsize},filecount={count}') def random_captcha_text(): global labelno captcha_text = [] for i in range(labelno): c = random.choice(ALL_CHAR_SET) captcha_text.append(c) return ''.join(captcha_text) # 生成字符對應的驗證碼 def gen_captcha_text_and_image(): global img_width,img_height,fontsize image = ImageCaptcha(width=img_width,height=img_height,font_sizes=fontsize) captcha_text = random_captcha_text() # print('captcha_text:',captcha_text) captcha_image = Image.open(image.generate(captcha_text)) return captcha_text, captcha_image def main(): global img_width, img_height, count, labelno get_argv() # 保存路徑 path = f'{img_width}x{img_height}_{labelno}chars' print('save path:',path) if not os.path.exists(path): os.makedirs(path) for i in range(count): now = str(int(time.time())) text, image = gen_captcha_text_and_image() filename = text+'_'+now+'.png' image.save(path + os.path.sep + filename) print('saved %d : %s' % (i+1,filename)) if __name__ == '__main__': main()
參考資料:
Python使用captcha制作驗證碼修改captcha包中image.py文件生成不改變字符形狀的有背景和無背景文本驗證碼
captcha · PyPI
原文鏈接:https://blog.csdn.net/toopoo/article/details/124742619
相關推薦
- 2022-10-24 Golang?errgroup?設計及實現原理解析_Golang
- 2022-09-18 Go語言包管理工具Godep的用法_Golang
- 2023-04-07 Python實現SVM支持向量機的示例代碼_python
- 2022-11-16 Python中Pygame模塊的詳細安裝過程_python
- 2022-07-02 C++中關于互斥量的全面認知_C 語言
- 2022-05-27 C++回溯算法廣度優先搜索舉例分析_C 語言
- 2022-08-10 C#對文件名智能排序的算法_C#教程
- 2022-01-18 在使用npm install時遇到的問題 npm ERR! code ERESOLVE
- 最近更新
-
- 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同步修改后的遠程分支