pillow 隨機生成驗證碼

pillow 隨機隨機生成驗證碼

from PIL import Image, ImageDraw,ImageFilter,Imagefont
import random
#隨機字母 運用 ascii編碼表
def random_char():
return chr(random.randint(65,90))
#隨機數字
def random_num():
return random.randint(0,9)
#隨機字體顏色 稍深
def random_color():
return(random.randint(150,255),random.randint(150,255),random.randint(150,255))
#隨機背景顏色
def random_color2():
return(random.randint(30,120),random.randint(30,120),random.randint(30,120))
#生成空白背景圖層
image=Image.new(‘RGB’,size(240,60),color=(255,255,255)
#生成繪製對象
draw=ImageDraw.Draw(image)
#字體對象,字體,字號
font=ImageFont.truetype(‘arial.ttf’,36)
#循環像素點並填充顏色
for x in range(0,240):
for y in range(0,60):
draw.point(xy=(x,y)),fill=random_color2())
#生成文字
for t in range(0,4):
draw.text((60*t+20,10),random_char(),font=font,fill=random_color())
#加模糊濾鏡
image=image.fillter(ImageFilter.BLUR)
#保存
image.save('demo4.jpg,‘jpeg’)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章