import random
from PIL import Image,ImageDraw,ImageFont
#产生随机颜色函数
def random_color():
c1 = random.randint(0,255)
c2 = random.randint(0,255)
c3 = random.randint(0,255)
return (c1,c3,c3)
#随机产生字符串和数字的函数
def random_text():
num = str(random.randint(0,9))
s_str = chr(random.randint(97,122))
c_str = chr(random.randint(65,90))
return (random.choice([num,s_str,c_str]))
font = ImageFont.truetype(’/System/Library/Fonts/PingFang.ttc’,size = 30)
#开始画图
def draw_picture():
width = 150
height = 40
#画布
image = Image.new(‘RGB’,(width,height),random_color())
draw = ImageDraw.Draw(image)
#在图片上画字符串和数字
for i in range(4):
draw.text((20+i*20,5),random_text(),random_color(),font=font)
with open(‘test.png’,‘wb’) as f:
image.save(f,‘png’)
f.close()
if name == ‘main’:
draw_picture()
参考文章:
https://blog.csdn.net/figo8875/article/details/88755122