用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()