You can use 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...emplate.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()
Explanation
PIL is an Image Manipulation library, it can edit images like GIMP with Python (but its more limited).
In this code we declare a class called myTemplate
, that will be our template, inside this class we have two functions, one will initialize the class, and request name
, description
and image
, and the other will draw.
Well, from line 13
to 15
, the program imports and verifies if there is a selected image, if yes it crops and resizes the selected image (16
and 17
), then pastes the selected image in the template.
After this, name and description are drawn, then the program saves the file.
You can customize teh class and the line 26
for your needs
Image references
template.png
images.jfif
out.png