from PIL import Image, ImageDraw, ImageFont, ImageFilter import random # 生成随机大写字母,ascii为 [65, 90], def rndChar(): return chr(random.randint(65, 90)) # 生成填充颜色,返回 RGB 值,每个颜色值在(128, 255)之间 def rndColor(): return (random.randint(128, 255), random.randint(128, 255), random.randint(128, 255)) # rndColor() # 生成字体颜色,返回 RGB 值,每个颜色值在(0, 127)之间,在此我只是为了不和背景颜色重叠,大家可以自行设置。 def rndColor2(): return (random.randint(0, 127), random.randint(0, 127), random.randint(0, 127)) def yanzhengma(): # 图片大小(存放 5 个随机字母),宽度为:400,高度为:80 width = 400 height = 80 # 黑色背景,RGB值为:(0, 0, 0) image = Image.new('RGB', (width, height), (0, 0, 0)) # 创建 Font 对象,设定字体大小为 36 font = ImageFont.truetype('C:/Windows/Fonts/verdana.ttf', 36) # 创建Draw对象 draw = ImageDraw.Draw(image) # 填充每个像素 for x in range(width): for y in range(height): draw.point((x, y), fill=rndColor()) # 输出文字 for t in range(5): draw.text((80 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) # 让验证码图片模糊 image = image.filter(ImageFilter.BLUR) image.save('123123.jpg', 'jpeg') yanzhengma()
07-12
1709
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-26
2526
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
05-18