PY0271验证码的创建

生成的 随机背景色的 效果

 

 

from PIL import Image,ImageDraw,ImageFont,ImageFilter
import random


# 设置随机的背景颜色。
def rndColor():
r = random.randint(64,255)
g = random.randint(64,255)
b = random.randint(64,255)
return r,g,b

# 字体颜色
def rndColor2():
r = random.randint(32,127)
g = random.randint(32,127)
b = random.randint(32,127)
return r,g,b

# 产生随机字母
def rndChar():
return chr(random.randint(65,90))

width = 100*4
height = 100
# 获取一个Image对象,参数分别是 RGB模式。宽,高颜色
image = Image.new('RGB',(width,height),(255,255,255))
image.save('code.jpg','jpeg')
# 创建一个draw对象
draw = ImageDraw.Draw(image)
for x in range(width):
for y in range(height):
draw.point((x,y),fill=rndColor())

image.save('code2.jpg','jpeg')

font = ImageFont.truetype('ARIALNI',36)
for t in range(0,4):
draw.text((100*t+30,20),rndChar(),font=font,fill=rndColor2())

image.save('code3.jpg','jpeg')




 

posted @ 2022-12-31 13:54  猫有九命  阅读(19)  评论(1编辑  收藏  举报