您可以使用PIL
from PIL import Image, ImageDraw, ImageFont #Import PIL functions
class myTemplate(): #Your template
def __init__(self, name, description, image):
self.name=name #Saves Name input as a self object
self.description=description #Saves Description input as a self object
self.image=image #Saves Image input as a self object
def draw(self):
"""
Draw Function
------------------
Draws the template
"""
img = Image.open(r'C:\foo\...\template.png', 'r').convert('RGB') #Opens Template Image
if self.image != '':
pasted = Image.open(self.image).convert("RGBA") #Opens Selected Image
pasted=pasted.resize((278, int(pasted.size[1]*(278/pasted.size[0])))) #Resize image to width fit black area's width
pasted=pasted.crop((0, 0, 278, 322)) #Crop height
img.paste(pasted, (31, 141)) #Pastes image into template
imgdraw=ImageDraw.Draw(img) #Create a canvas
font=ImageFont.truetype("C:/Windows/Fonts/Calibril.ttf", 48) #Loads font
imgdraw.text((515,152), self.name, (0,0,0), font=font) #Draws name
imgdraw.text((654,231), self.description, (0,0,0), font=font) #Draws description
img.save(r'C:\foo\...\out.png') #Saves output
amaztemp=myTemplate('Hello, world!', 'Hi there', r'C:\foo\...\images.jfif')
amaztemp.draw()
说明
PIL 是一个图像处理库,它可以像 GIMP 一样使用 Python 编辑图像(但它更有限)。
在这段代码中,我们声明了一个名为myTemplate的类,这将是我们的模板,在这个类中我们有两个函数,一个将初始化该类,并请求name、description和image,以及另一个会画。
好吧,从13 到15 行,程序导入并验证是否有选定的图像,如果有,它会裁剪并调整选定图像的大小(16 和17),然后粘贴选定的图像模板中的图像。
之后,绘制名称和描述,然后程序保存文件。
您可以根据需要自定义课程和26 行
图片参考
模板.png
images.jfif
out.png