github博客传送门
csdn博客传送门python
字体包和源码文件连接
https://download.csdn.net/download/zhanghao3389/10663672git
生成一个随机的大写字母验证码图片github
from PIL import Image, ImageDraw, ImageFont # 图片 图片生成,画图 字体包 import random # 导入random包 def ascii(): # 随机生成一个字母 return chr(random.randint(65, 90)) # 将生成的整数 65-90 转换ascii码 为字母 def randomcolor(): # 随机生成颜色 三个通道 # 生成三个随机的像素值 在64-255之间的值 return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) width = 250 # 图片的边界 宽 height = 60 # 图片的边界 高 # 新建一个 RGB 图片 长宽(width,height)的 (255,255,255)白底 image = Image.new("RGB", (width, height), (255, 255, 255)) # 导入一个字体包 建立font对象 字体对象 # 字体包本身在本身 C 盘搜索 .ttf 也可搜索出来本身须要的 font = ImageFont.truetype("Merriweather-Black.ttf", 36) draw = ImageDraw.Draw(image) # 建立一个能够在给定图像上绘图的对象。 # 在白底的图片上 填充像素 for x in range(width): # 遍历图片的像素 宽 的次数 for y in range(height): # 遍历图片的像素 高 的次数 draw.point((x, y), fill=randomcolor()) # 填充遍历的 x,y 坐标 填入一个 随机的 rgb值 # 在填充像素后的图片上 填充文字 for i in range(4): # 设置间距 随机字母 字体包 随机颜色 draw.text((60 * i + 10, 10), ascii(), font=font, fill=randomcolor()) image.show() # 展现这张图片