資源簡介
利用Python生成驗證碼圖片
代碼和字體文件壓縮包

代碼片段和文件信息
#?導入庫文件
from?PIL?import?ImageImageFontImageDrawImageFilter
import?stringrandomos
#?方法:獲得四個隨機字母
def?getRandomChar():
return?[random.choice(string.ascii_letters)for?_?in?range(4)]
#?方法:獲取隨機顏色
def?getRandomColor():
return?(random.randint(30100)random.randint(30100)random.randint(30100))
#?主方法:獲取驗證碼圖片
def?getCodePicture(fontPathoutPath):
#?圖片寬高
width?=?240
height?=?60
#?創建空白畫布
image?=?Image.new(‘RGB‘(widthheight)(180180180))
#?驗證碼字體
font?=?ImageFont.truetype(fontPath40)
#?畫出畫布,獲取畫布對象
draw?=?ImageDraw.Draw(image)
#?創建驗證碼對象
code?=?getRandomChar()
#?把驗證碼畫到畫布上
for?x?in?range(4):
draw.text((60*x+100)code[x]font=fontfill=getRandomColor())
#?填充噪點
for?_?in?range(random.randint(15003000)):
draw.point((random.randint(0width)random.randint(0height))fill?=?getRandomColor())
#?模糊處理
image?=?image.filter(ImageFilter.BLUR)
#?保存
image.save(outPath+“\\“+““.join(code)+“.jpg“‘jpeg‘)
if?__name__?==?‘__main__‘:
getCodePicture(“simsun.ttf“os.getcwd())
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1217??2017-07-14?17:21??codepicture.py
?????文件????10499104??2012-05-14?11:30??simsun.ttf
- 上一篇:python實現淘寶爬蟲
- 下一篇:計算機二級學習資料python
評論
共有 條評論