Automating Certificate Generation for Events Using GIMP

0
9804

Gimp with certificate

This article should interest DIY enthusiasts, providing them with the knowhow to generate multiple customised certificates using GIMP. With a little bit of Python and GIMP, one can add value to events by issuing computer generated certificates to participants – all in quick time and without much effort.

Let’s assume that your organisation is conducting a mega event and is expecting participants in the hundreds. Since no one is supposed to go back empty handed, all the participants are assured a participation certificate. As luck would have it, the work of generating those certificates falls upon you. What would you do?
Like those in many large (and smart) organisations, you too can automate the task of generating certificates. A quick search for certificate generators shows up a number of online tools or Windows-specific tools. On the GNU/Linux side we have the trusty Latex for certificate generation. But Latex is mainly for typesetting. So the GIMP can be given a try, and why not? It’s free, and it’s open source, with a great community backing it.

Okay, So where do I start?
The prerequisites for doing this are that you must have GIMP and Python 2.7 installed on your system – you could use Python 3 but you might need to modify the code a little. It helps if you have a little knowledge of Python and GIMP. Even if you do not, you could just follow the steps here to get what you need – hundreds of customised certificates!

Fig 1
Figure 1: The template

Here’s how we do it
1) Create or download a certificate template (the background image for your certificate). Open your certificate template using GIMP (right-click on the file and open with GNU Image Manipulation Program), or you could alternatively open GIMP and open your file from there using File -> Open. For this demo, we are using a template with a red background and some text.
2) Now type some dummy text where you expect the name of the participant, contest, etc, to appear. To keep it simple, let us have only one text <name>. To do this, click on the A button in the GIMP toolbox (it is usually on the left side), and then click and drag your mouse over the image so that a rectangular box appears right above the line. You can type the text here – placement is crucial because the participants’ names will be substituted in this location. You can change the font size in the menu above the box and drag the box at its sides, for proper alignment.
3) If you look at the layer toolbox (it is normally on the right side, and if not, it can be opened using Windows -> Dockable Dialogs -> Layers) you should now see two layers, one labelled ‘Background’ and the other with <name>. If it is so, you are on the right track.
Now save the file with Ctrl + S, and exit GIMP.
We now need to get the file with the participants’ names ready. Any format can be used, but in this case, we will use a normal text file. Enter all names, one per line and save the file.
Here comes the coding part. The code given below comes with explanations given as comments (beginning with #):

#!/usr/bin/env python
from gimpfu import *
# this is the important module for coding in python GIMP
import os # for file/folder paths
def run(*args):
template_file, list_file, output_folder = args
# the data is sent is unpacked into 3 variables
# this is where we will store our auto-generated certificates
output_folder = os.path.expanduser(‘~/’ + output_folder)
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# create the output directory, if its not there already
# this number is used for avoiding collisions when
# 2 people have the same name
# if there are 2 people named X,
# one file will have a name containing X1, the other X2 etc.
i = 0
with open(list_file, ‘r’) as list: # open the file for reading
for name in list:
# for each line do something i.e for each name in the list
i += 1
bg_image = pdb.gimp_file_load(template_file, template_file)
# load the certificate
# create a reference to the name layer in the certificate
name_layer = filter(lambda x: x.name == ‘<name>’, bg_image.layers)[0]
# change below 2 values in brackets if needed
pdb.gimp_text_layer_set_font(name_layer, ‘Comic Sans MS’)
pdb.gimp_text_layer_set_font_size(name_layer, 20, 0)
# set the required name in the certificate
pdb.gimp_text_layer_set_text(name_layer, name)
# We can save only one layer at time, so merge them
merged = pdb.gimp_image_merge_visible_layers(bg_image, 0)

# save file as <name><some number>
output_filename = name + str(i) + “.png”
#relative path
output_filename = os.path.join(output_folder, output_filename)
#absolute path

# save the file as a PNG image
pdb.file_png_save_defaults(bg_image, merged, output_filename, output_filename)
print “Finished”

# this is an important part, without this,
# the plug-in will not appear in GIMP
# first line is the name that appears in the title bar
# second line is the menu hierarchy
# the ‘[‘ starting from the third line provides the selection # tools for selecting the files/directories we # need
# the last line calls the run() method defined above
register(
“certificate_generator”, “”, “”, “”, “”, “”,
“<Toolbox>/Xtns/Languages/Python-Fu/_Certificate-Generator”, “”,
[
(PF_FILE, “arg0”, “Certificate Template file”, “”),
(PF_FILE, “arg1”, “Data File”, “”),
(PF_STRING, “arg2”, “Output directory (created relative to user home)”, “”),
],
[],
run
)

main()

Save the script as <your_script_name>.py. There are a number of places to save it but the one that worked out for me was /usr/lib/gimp/2.X/plug-ins. Here, 2.X is the GIMP version installed in your system.
Type the following command to give executable permissions to your script:

//switch to the directory
cd /usr/lib/gimp/2.X/plug-ins
//give permissions
sudo chmod +x <your_script_name>.py

Now, to run it:
1. Open GIMP
2. Navigate to Filters->Python-fu->Certificate Generator
There it is! A window appears asking for the path to the template, list file and output directory.
These can be typed or selected using the file browser.
Click ‘OK’ to run the script and the certificates are generated in the output directory specified.

Fig 2
Figure 2: Template with placeholder text added
Fig 3
Figure 3: One of the certificates generated

Additional notes
If the plugin does not appear in GIMP, run GIMP from the /usr/lib/gimp/2.X/plug-ins directory; if there are any errors, they are shown in the terminal. Also try running the file as follows:

$ python <your_script_name>.py.

It should only show an error related to gimpfu; if not, try to fix the error shown.
The text is black in colour here. For additional information on how to change the background/foreground and how to view the plugins in GIMP, head over to References.
While creating the text layer in GIMP (the <name> one), create the rectangle large enough for the longest possible name.

References
[1] https://choboprogrammer.wordpress.com/2010/12/17/writing-python-script-for-gimp/#more-7
[2] http://coderazzi.net/python/gimp/pythonfu.html

LEAVE A REPLY

Please enter your comment!
Please enter your name here