Create Certificates using Python-PIL
Last Updated :
26 May, 2020
Prerequisites: Python: Pillow (a fork of PIL)
Well if you have ever done something like creating certificates for participants of any event, then you know how tedious process it is. Let's automate that using Python. We will be using the Pillow module of Python. To install that simply type the following in your terminal
pip install Pillow
You also need the design of the certificate as an image format (preferably png). You can use something like Microsoft Office Publisher to create the certificates and export them as png. Keep some extra space for entering the name. Below is the template certificate we will be using.
Template Certificate:

So, our certificate template is ready. Now, we need to find a suitable font for writing the name on it. You need the path to the font file (TTF file). If you are using Windows 10, then simply search for
Fonts in windows search, it will show you results of Fonts settings. Head over there and you should see something similar to the following screen.

Now, choose the font you like from here and click on it. You will see a path to that font. Note down the path somewhere. You will need it in your code.
Below is the implementation.
Python3 1==
# imports
from PIL import Image, ImageDraw, ImageFont
def coupons(names: list, certificate: str, font_path: str):
for name in names:
# adjust the position according to
# your sample
text_y_position = 900
# opens the image
img = Image.open(certificate, mode ='r')
# gets the image width
image_width = img.width
# gets the image height
image_height = img.height
# creates a drawing canvas overlay
# on top of the image
draw = ImageDraw.Draw(img)
# gets the font object from the
# font file (TTF)
font = ImageFont.truetype(
font_path,
200 # change this according to your needs
)
# fetches the text width for
# calculations later on
text_width, _ = draw.textsize(name, font = font)
draw.text(
(
# this calculation is done
# to centre the image
(image_width - text_width) / 2,
text_y_position
),
name,
font = font )
# saves the image in png format
img.save("{}.png".format(name))
# Driver Code
if __name__ == "__main__":
# some example of names
NAMES = ['Frank Muller',
'Mathew Frankfurt',
'Cristopher Greman',
'Natelie Wemberg',
'John Ken']
# path to font
FONT = "/path / to / font / ITCEDSCR.ttf"
# path to sample certificate
CERTIFICATE = "path / to / Certificate.png"
coupons(NAMES, CERTIFICATE, FONT)
Output:

Add the names to the
NAMES list. Then change the font path and path to the certificate template according to your system. Then run the above code and all your certificates should be ready. This is a pretty effective solution for automating the process of creating certificates for a large number of participants. This can be very effective for event organizers.
Similar Reads
Python Pillow - Using Image Module In this article, we will see how to work with Image Module of PIL in Python. First, let's see how to install the PIL. Installation: Linux: On the Linux terminal type the following: pip install Pillow Installing pip via terminal: sudo apt-get update sudo apt-get install python-pipWorking with Image
5 min read
Python PIL | Image.convert() Method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
2 min read
Image Captioning using Python Image captioning is a very classical and challenging problem coming to Deep Learning domain, in which we generate the textual description of image using its property, but we will not use Deep learning here. In this article, we will simply learn how can we simply caption the images using PIL. Preproc
3 min read
Python PIL | Image.resize() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
4 min read
Python PIL | Image.getdata() PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files,
2 min read