[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
import random
import os
from PIL import Image, ImageFont, ImageDraw
def drawatcenter(data,text,y,color,fon):
w,h=data.textsize(text,font=fon)
data.multiline_text(((900-w)/2.0,y-h/2.0),text,fill=color,font=fon,
spacing=50)
#bcolor=random.randint(0,127),random.randint(0,127),random.randint(0,127))
bcolor=(51,102,204)
fontname="DFPHeiBold-B5.ttc" #要替换成你电脑上有的字体名
fcolor=(255,255,255)
for num in range(101,1000):
if not (num%100)//10==num%10:
text = u"找到"
print(num)
nmu=(num//100)*100+10*(num%10)+(num%100)//10
print(nmu)
a=[num for i in range(35)]
a[random.randint(0,34)]=nmu
print(a)
text=text+str(nmu)
txt=""
for i in range(35):
txt=txt+str(a[i])
if i%5==4:
txt=txt+'\n'
else:
txt=txt+' '
im = Image.new("RGB", (900, 1600), bcolor)
dr = ImageDraw.Draw(im)
font = ImageFont.truetype(os.path.join("fonts", fontname), 144)
drawatcenter(dr,text,200,"orange",font)
font = ImageFont.truetype(os.path.join("fonts", fontname), 90)
drawatcenter(dr,txt,700,fcolor,font)
#im.show()
im.save(str(num)+"-0.png")
font = ImageFont.truetype(os.path.join("fonts", fontname), 160)
im = Image.new("RGB", (900,200), "orange")
dr = ImageDraw.Draw(im)
w,h=dr.textsize(text,font=font)
drawatcenter(dr,text,100,fcolor,font)
im.save(str(num)+"-1.png") #这个保存的是视频封面,也就是视频第一帧的字幕