Create a certificate using Python PIL

Today, we’ll create a certificate using Python PIL.

Generally, creation of certificate requires manual work, but Python’s pillow module can save all effort! Feed a sample certificate, list the names, and we’re good to go.

What is PIL?

PIL is an acronym for Python’s Pillow module. Basically, PIL module provides with methods to work on images in Python code.

Following are the classes we’ll be importing from PIL:

  • ImageDraw class
  • ImageFont class
  • Image class

These classes bring in methods to help us adjust the image properties, and include the names on the certificates. For further reference, kindly refer:

Get the Basic image information with Pillow | Python.

Create a certificate using python:

Implementation has the following steps:

code segment 1:

  1. Inception of the code, takes place with importing the above classes from PIL.
  2. Defining a function(‘Name_chit’ in the code), with parameters: list of names, sample certificate, path to the font file).
  3.  A for loop iterates through the given list of names, for which:
    • We give the position of the text
    • Opening the image and specifying its height, width properties.
    • A canvas opens on the image which helps print the given name on the image.
    • Text properties and positions are set.
    • Finally, saving the edited certificate image is ‘.png’ format.
# imports 
from PIL import ImageDraw as ID
from PIL import ImageFont as IF
from PIL import Image as I 


def Name_chit(names: list, certificate: str, font_path: str): 

  for N in names: 
    
    text_y_position = 900
# this opens the image
    Open_img = I.open(certificate, mode ='r') 
    
# width of image is set here 
    widthIMG = Open_img.width 
    
# height of image is set here
    image_height = Open_img.height 

# here the 'canv_over' canvas is made to overlay on the image 
    canv_over = ID.Draw(Open_img) 

    # gets the font object from the 
    # font file (TTF) 
    Obj_font = IF.truetype(font_path, 200 ) 

# this will set the text width for further calculations 
    widthTXT, _ = canv_over.textsize(N, font = Obj_font) 

    canv_over.text( 
      ( 
# this is to centre given image 
        (widthIMG - widthTXT) / 2, 
        text_y_position 
      ), 
      N, 
      font = Obj_font	 ) 

# saving the image with '.png' format 
    Open_img.save("{}.png".format(N)) 

code section 2:

Coming out of the Name_chit function, we have our main section.

Main section comprises of the following:

  1. Defining list of names for the certificate.
  2. Specifying the exact path to the font file required.
  3. Finally, giving the sample certificate image path.
  4. These details are passed as parameters to the Name_chit function.

 

# Main section 
if __name__ == "__main__": 

# sample list of names on certificate 
  sample_names = ['Priya Bansal', 'Bisleen Kaur', 'Jasmine'] 
  
# sample font file path 
  font_file= "/Library/Fonts/Microsoft/Lucida Calligraphy.ttf"
  
# sample certificate's path set here 
  certi_file = "/Users/mohitgarg/Downloads/Certificate.png"
#calling the function
  Name_chit(sample_names,certi_file, font_file)

Output:

One by one, png files of the certificates will show up in the folder wherever the program is. Following is the output of the above code:

certificate output image

This is how, one can create certificates using Python’s PIL module, and can save the manual effort of typing each one. For further reference regarding PIL classes, methods, syntaxes, kindly refer: PIL reference link.

Leave a Reply

Your email address will not be published. Required fields are marked *