用python生成隨機4位驗證碼


b博客園怎么markdown啊

 

 

from PIL import Image, ImageDraw, ImageFont, ImageFilter

import random

def randNums():
#隨機字符
return "a"

def randColor():
#隨機顏色
return (random.randint(64,100), random.randint(64,100), random.randint(64,100))

def randColor2():
#隨機顏色2
return (random.randint(32,127), random.randint(32,127), random.randint(32,127))

if __name__ == "__main__":
#240 X 60
width = 60*4
height = 60
image = Image.new('RGB', (width, height), (255, 255, 255))
# with open("pic", "wb") as f:
# image.save(f, format="png")
#create draw obj
draw = ImageDraw.Draw(image)
#define char color
font = ImageFont.truetype('arial.ttf',size=36)
for x in range(width):
for y in range(height):
draw.point((x, y), fill=randColor())

for t in range(4):
draw.text((60*t+10, 10), randNums(), fill=randColor2(),
font=font
)
image.show()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM