參考資料:
1、腳本之家
https://www.jb51.net/article/173211.htm
一、驗(yàn)證碼圖片的生成
1、創(chuàng)建一張圖片(畫布),用于繪圖
- Image.new():
Image模塊中的new()函數(shù)
2、實(shí)例化對象draw,執(zhí)行繪圖
- ImageDraw.Draw()
3、選取字體
- ImageFont.truetype()
4、繪制字符
- draw.textsize()
5、隨機(jī)顏色
- random模塊中的randint()
用于隨機(jī)生成顏色值、隨機(jī)生成字符在畫布上的x和y坐標(biāo)。
6、隨機(jī)生成字符
- random模塊中的choice()
choice()用于從26個(gè)大寫字母(chr字符集中65~90為26個(gè)大寫字母)和數(shù)字中隨機(jī)選擇一個(gè)字符。
二、代碼
# -*- coding: utf-8 -*-
# __author__: Pad0y
from PIL import Image, ImageDraw, ImageFont
from random import choice, randint, randrange
import string
# 候選字符集,大小寫字母+數(shù)字
chrs = string.ascii_letters + string.digits
def selected_chrs(length):
"""
返回length個(gè)隨機(jī)字符串
:param length:
:return:
"""
result = ''.join(choice(chrs) for _ in range(length))
return result
def get_color():
"""
設(shè)置隨機(jī)顏色
:return:
"""
r = randint(0, 255)
g = randint(0, 255)
b = randint(0, 255)
return (r, g, b)
def main(size=(200, 100), chrNumber=6, bgcolor=(255, 255, 255)):
"""
定義圖片大小,驗(yàn)證碼長度,背景顏色
:param size:
:param chrNumber:
:param bgcolor:
:return:
"""
# 創(chuàng)建空白圖像和繪圖對象
image_tmp = Image.new('RGB', size, bgcolor)
draw = ImageDraw.Draw(image_tmp)
# 生成并計(jì)算隨機(jī)字符的寬度和高度
text = selected_chrs(chrNumber)
font = ImageFont.truetype('c:\\windows\\fonts\\simhei.ttf', 48) # 選定一款系統(tǒng)字體
width, height = draw.textsize(text, font)
if width + 2*chrNumber > size[0] or height > size[1]:
print('Size Error!')
return
# 繪制字符串
startX = 0
width_eachchr = width // chrNumber # 計(jì)算每個(gè)字符寬度
for i in range(chrNumber):
startX += width_eachchr + 1
position = (startX, (size[1]-height)//2+randint(-10, 10)) # 字符坐標(biāo), Y坐標(biāo)上下浮動
draw.text(xy=position, text=text[i], font=font, fill=get_color()) # 繪制函數(shù)
# 對像素位置進(jìn)行微調(diào),實(shí)現(xiàn)驗(yàn)證碼扭曲效果
img_final = Image.new('RGB', size, bgcolor)
pixels_final = img_final.load()
pixels_tmp = image_tmp.load()
for y in range(size[1]):
offset = randint(-1, 0) # randint()相當(dāng)于閉區(qū)間[x,y]
for x in range(size[0]):
newx = x + offset # 像素微調(diào)
if newx >= size[0]:
newx = size[0] - 1
elif newx < 0:
newx = 0
pixels_final[newx, y] = pixels_tmp[x, y]
# 繪制隨機(jī)顏色隨機(jī)位置的干擾像素
draw = ImageDraw.Draw(img_final)
for i in range(int(size[0]*size[1]*0.07)): # 7%密度的干擾像素
draw.point((randrange(size[0]), randrange(size[1])), fill=get_color()) # randrange取值范圍是左開右閉
# 繪制隨機(jī)干擾線,這里設(shè)置為8條
for i in range(8):
start = (0, randrange(size[1]))
end = (size[0], randrange(size[1]))
draw.line([start, end], fill=get_color(), width=1)
# 繪制隨機(jī)弧線
for i in range(8):
start = (-50, -50) # 起始位置在外邊看起來才會像弧線
end = (size[0]+10, randint(0, size[1]+10))
draw.arc(start+end, 0, 360, fill=get_color())
# 保存圖片
img_final.save('Veri_code.jpg')
img_final.show()
if __name__ == '__main__':
main((200, 100), 6, (255, 255, 255))
運(yùn)行結(jié)果
