安装依赖库
pip install --only-binary Pillow Pillow
生成画布
# PIL
# pip install pillow
img = Image.new('RGB', (200, 40), color=(255, 255, 255))
# 新建一个和图片大小的画布
draw = ImageDraw = ImageDraw.Draw(img)
img.save('new_img.png', 'PNG')
完整代码
# PIL
# pip install pillow
import random
from PIL import Image, ImageDraw, ImageFont
import string
# 数字+大小写字符
str_all = string.digits + string.ascii_letters
img = Image.new('RGB', (200, 40), color=(255, 255, 255))
# 新建一个和图片大小的画布
draw = ImageDraw = ImageDraw.Draw(img)
# 生成字体对象
# linhailishu.ttf是字体,可以选择自己喜欢的
font = ImageFont.truetype(font='./font/linhailishu.ttf', size=32)
# 书写文字
str_string = 'lele'
for i in range(4):
draw.text((45*i+20, 10), random.choice(str_all), (0, 0, 0), font=font)
img.save('new_img.png', 'PNG')