
作者:Kevin
來源:麥叔編程
最近感覺被大數據定義成機器人了,隨便看個網頁都跳驗證碼。
怎么用python繞驗證碼是個令人頭禿的事情,
我投降!那么今天手把手教大家如何寫驗證碼,去為難別人,讓他們頭禿。
說錯了,其實就是教大家如何通過python代碼去生成驗證碼~~
pip install pillow
# 導入相關的繪畫模塊 from PIL import Image, ImageDraw, ImageFont # 設置背景色 bg_color = (100, 100, 255) #設置畫布長寬(像素) width = 400 height = 100 # 通過設置生成新的畫布 im = Image.new('RGB',(width,height),bg_color) # 展示畫布 im.show()
在這bg_color背景色的設置是用RGB顏色標準去設置的,如果你不喜歡這個背景色可以自己調一下。
“
常見的RGB顏色
運行代碼后:
先上代碼
from PIL import Image, ImageDraw, ImageFont # 省略第一步的代碼 # 創建畫筆對象 draw = ImageDraw.Draw(im) # 驗證碼文本 string = 'MSBC' # 構造字體對象 font = ImageFont.truetype('./ziti.ttf', 90) # font = ImageFont.load_default().font # 構造字體顏色 fontcolor = (255, 100, 100) # 繪制4個字 draw.text((10, 2), string[0], font=font, fill=fontcolor)
draw.text((110, 2), string[1], font=font, fill=fontcolor)
draw.text((210, 2), string[2], font=font, fill=fontcolor)
draw.text((310, 2), string[3], font=font, fill=fontcolor) #釋放畫筆 del draw #展示圖片 im.show()
代碼分析:
draw = ImageDraw.Draw(im)
在im畫布上實例化一只筆。
font = ImageFont.truetype('./ziti.ttf', 90) # font = ImageFont.load_default().font
第一個參數是設置字體,我這有下載一個ttf的字體文件所以可以用它,如果沒有指定的字體文件可以使用默認的# font = ImageFont.load_default().font;
第二個參數是繪制字體的大小,因為我們畫布是400x100的 所以我們為了美觀就把字體設成90x90的尺寸。
# 構造字體顏色 fontcolor = (255, 100, 100)
字體文本的顏色,參照第一步畫布的RGB設置。
# 繪制4個字 draw.text((10, 2), string[0], font=font, fill=fontcolor)
draw.text((110, 2), string[1], font=font, fill=fontcolor)
draw.text((210, 2), string[2], font=font, fill=fontcolor)
draw.text((310, 2), string[3], font=font, fill=fontcolor)
這里draw.text函數,顧名思義就是開始拿畫起畫筆開始寫字,
第一個參數 寫字的坐標;
第二個參數 要寫的字;
第三個參數 字的顏色(上面構造過了,你也可以設成一字一色)。
代碼跑一下看成果:
效果還行,就是總覺得少了點什么?
既然是驗證碼,肯定要稍微難識別,上面那個那么傻白甜的驗證碼是怎么回事??
這一步我們需要導入random模塊,因為干擾是不規則隨機生成的。
import random from PIL import Image, ImageDraw, ImageFont # 省略第一步代碼 # 省略第二步代碼 #使用point函數繪制噪點 for i in range(0, 100): xy = (random.randrange(0, width), random.randrange(0, height)) fill = (random.randrange(0, 255), 255, random.randrange(0, 255)) draw.point(xy, fill=fill) #釋放畫筆 del draw im.show()
代碼分析:
import random
別忘了導入random模塊
for i in range(0, 100): xy = (random.randrange(0, width), random.randrange(0, height)) fill = (255, 255, 255) draw.point(xy, fill=fill)
一個循環100次的for循環,
xy變量是畫干擾點的坐標值
fill變量是噪點的顏色,還是RGB標準的
draw.point 畫點的動作
“
這個for循環的次數越多,畫布上噪點也會相應增多。
跑一下代碼看看噪點的效果如何:
感覺還是有點傻白甜,我們來循環1000次的試試:
10000次!
夠了。
這一步我將各個參數結合random模塊,使我們的驗證碼更難辨別!
import random from PIL import Image, ImageDraw, ImageFont
bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255))
width = 400 height = 100 im = Image.new('RGB',(width,height),bg_color)
# 創建畫筆對象
draw = ImageDraw.Draw(im)
# 構造字體對象
font = ImageFont.truetype('./ziti.ttf', 100)
# font = ImageFont.load_default().font
# 構造字體顏色
fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
# 繪制4個字 string = 'MSBC' draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor)
draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor)
draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor)
draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor)
#調用畫筆的point()函數繪制噪點 for i in range(0, 10000):
xy = (random.randrange(0, width), random.randrange(0, height))
fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
draw.point(xy, fill=fill)
#釋放畫筆
del draw
im.show()
我把字體顏色,噪點顏色,文本位置都結合了random模塊,效果圖如下:
這一步,我們需要把上面的代碼封裝到函數中,大致把上面代碼重構成:
# 使用for循環生成文本字符 # 生成驗證碼圖片的函數,參數就是上面生成的文本 # 調用生成驗證碼圖片函數
重構后:
import random from PIL import Image, ImageDraw, ImageFont string = '' #隨機選取4個值作為驗證碼
rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' for i in range(0, 4): string += rand_str[random.randrange(0, len(rand_str))]
def gen_verify_img(text):
bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255))
width = 400 height = 100 im = Image.new('RGB',(width,height),bg_color)
# 創建畫筆對象
draw = ImageDraw.Draw(im)
# 構造字體對象
font = ImageFont.truetype('./ziti.ttf', 100)
# font = ImageFont.load_default().font
# 構造字體顏色
fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
# 繪制4個字
draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor)
draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor)
draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor)
draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor)
#調用畫筆的point()函數繪制噪點 for i in range(0, 10000):
xy = (random.randrange(0, width), random.randrange(0, height))
fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
draw.point(xy, fill=fill)
#釋放畫筆
del draw
im.show()
# 調用函數
gen_verify_img(string)
把原先代碼中的string變量提到了函數外,把它變成函數需要傳入的參數,
再用for循環,隨機選出4個字符。
string = '' #隨機選取4個值作為驗證碼 rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' for i in range(0, 4): string += rand_str[random.randrange(0, len(rand_str))]
代碼再跑一下:
上面的驗證碼是 DZNO還是DZN0?
在web開發的登錄操作,和訓練驗證碼識別的神經運算中,都需要大量的驗證碼圖片。
所以需要把大量的驗證碼圖片文件,我們將批量驗證碼保存到本地。
完整代碼:
import random from PIL import Image, ImageDraw, ImageFont
def gen_verify_img(text):
bg_color = (random.randrange(20, 120), random.randrange(20, 120), random.randrange(150, 255))
width = 400 height = 100 im = Image.new('RGB',(width,height),bg_color)
# 創建畫筆對象
draw = ImageDraw.Draw(im)
# 構造字體對象
font = ImageFont.truetype('./ziti.ttf', 100)
# font = ImageFont.load_default().font
# 構造字體顏色
fontcolor = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
# 繪制4個字
draw.text((random.randint(10, 30), (random.randint(0, 10))), string[0], font=font, fill=fontcolor)
draw.text((random.randint(90, 130), (random.randint(0, 10))), string[1], font=font, fill=fontcolor)
draw.text((random.randint(180, 230), (random.randint(0, 10))), string[2], font=font, fill=fontcolor)
draw.text((random.randint(270, 330), (random.randint(0, 10))), string[3], font=font, fill=fontcolor)
#調用畫筆的point()函數繪制噪點 for i in range(0, 10000):
xy = (random.randrange(0, width), random.randrange(0, height))
fill = (random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
draw.point(xy, fill=fill)
#釋放畫筆
del draw
# im.show()
im.save(f'./{text}.png','png') for i in range(100): string = '' #隨機選取4個值作為驗證碼
rand_str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' for i in range(0, 4): string += rand_str[random.randrange(0, len(rand_str))]
gen_verify_img(string) print(f'{string} 驗證碼生成成功??!')
最后再跑一下:
部分驗證碼展示:
“
作者注:
如果再將本文中的代碼進行變形或改寫,可能會得到更五花八門的驗證碼,怎么發揮就看屏幕錢你的了。
數據分析咨詢請掃描二維碼
若不方便掃碼,搜微信號:CDAshujufenxi
CDA數據分析師證書考試體系(更新于2025年05月22日)
2025-05-26解碼數據基因:從數字敏感度到邏輯思維 每當看到超市貨架上商品的排列變化,你是否會聯想到背后的銷售數據波動?三年前在零售行 ...
2025-05-23在本文中,我們將探討 AI 為何能夠加速數據分析、如何在每個步驟中實現數據分析自動化以及使用哪些工具。 數據分析中的AI是什么 ...
2025-05-20當數據遇見人生:我的第一個分析項目 記得三年前接手第一個數據分析項目時,我面對Excel里密密麻麻的銷售數據手足無措。那些跳動 ...
2025-05-20在數字化運營的時代,企業每天都在產生海量數據:用戶點擊行為、商品銷售記錄、廣告投放反饋…… 這些數據就像散落的拼圖,而相 ...
2025-05-19在當今數字化營銷時代,小紅書作為國內領先的社交電商平臺,其銷售數據蘊含著巨大的商業價值。通過對小紅書銷售數據的深入分析, ...
2025-05-16Excel作為最常用的數據分析工具,有沒有什么工具可以幫助我們快速地使用excel表格,只要輕松幾步甚至輸入幾項指令就能搞定呢? ...
2025-05-15數據,如同無形的燃料,驅動著現代社會的運轉。從全球互聯網用戶每天產生的2.5億TB數據,到制造業的傳感器、金融交易 ...
2025-05-15大數據是什么_數據分析師培訓 其實,現在的大數據指的并不僅僅是海量數據,更準確而言是對大數據分析的方法。傳統的數 ...
2025-05-14CDA持證人簡介: 萬木,CDA L1持證人,某電商中廠BI工程師 ,5年數據經驗1年BI內訓師,高級數據分析師,擁有豐富的行業經驗。 ...
2025-05-13CDA持證人簡介: 王明月 ,CDA 數據分析師二級持證人,2年數據產品工作經驗,管理學博士在讀。 學習入口:https://edu.cda.cn/g ...
2025-05-12CDA持證人簡介: 楊貞璽 ,CDA一級持證人,鄭州大學情報學碩士研究生,某上市公司數據分析師。 學習入口:https://edu.cda.cn/g ...
2025-05-09CDA持證人簡介 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度、美團、阿里等 ...
2025-05-07相信很多做數據分析的小伙伴,都接到過一些高階的數據分析需求,實現的過程需要用到一些數據獲取,數據清洗轉換,建模方法等,這 ...
2025-05-06以下的文章內容來源于劉靜老師的專欄,如果您想閱讀專欄《10大業務分析模型突破業務瓶頸》,點擊下方鏈接 https://edu.cda.cn/g ...
2025-04-30CDA持證人簡介: 邱立峰 CDA 數據分析師二級持證人,數字化轉型專家,數據治理專家,高級數據分析師,擁有豐富的行業經驗。 ...
2025-04-29CDA持證人簡介: 程靖 CDA會員大咖,暢銷書《小白學產品》作者,13年頂級互聯網公司產品經理相關經驗,曾在百度,美團,阿里等 ...
2025-04-28CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-27數據分析在當今信息時代發揮著重要作用。單因素方差分析(One-Way ANOVA)是一種關鍵的統計方法,用于比較三個或更多獨立樣本組 ...
2025-04-25CDA持證人簡介: 居瑜 ,CDA一級持證人國企財務經理,13年財務管理運營經驗,在數據分析就業和實踐經驗方面有著豐富的積累和經 ...
2025-04-25