im: Image to work
mode : Optional , mode to use colour values.
Creating bulk certificates using data from MySQL by Pillow in Python
Adding text to an image
Opening an image
from PIL import Image, ImageDraw, ImageFont
path = "E:\\testing\\images\\cat.png" # existing image to open
img = Image.open(path)
Font family and size.
fnt = ImageFont.truetype("times.ttf", 140) # family with size
#fnt = ImageFont.truetype("arial.ttf", 140)
Full code to read image, add text and save in given path is here.
from PIL import Image, ImageDraw, ImageFont
path = "E:\\testing\\images\\cat.png" # existing image to open
path2 = "E:\\testing\\images\\cat2.png" # save the new image path
img = Image.open(path)
fnt = ImageFont.truetype("times.ttf", 140)
d1 = ImageDraw.Draw(img)
d1.text((80, 160), "plus2net", fill=(194, 61, 43),font=fnt)
img.show()
img.save(path2) # Save the image in new location or path
We can specify the anchor option for the alignment of the text with xy coordinates. The default value is la (left-ascender) or top – left corner of the image. More precise alignment can be achieved by using anchor option with different values.
Create certificate with details of the student
from PIL import Image, ImageDraw, ImageFont
path = "F:\\testing\\images\\certificate.png" # blank certificate
path2 = "F:\\testing\\images\\certificate_01.png" # final certificate
img = Image.open(path, mode="r")
# img.show()
img1 = ImageDraw.Draw(img) # create object to Draw
fnt1 = ImageFont.truetype("arial.ttf", 70) # font for Label text
fnt2 = ImageFont.truetype("arial.ttf", 50) # font for data text
fill1 = (39, 77, 113) # colour of the Label text
fill2 = (19, 119, 169) # colour of the data text
img1.text((280, 700), "Name :", fill=fill1, font=fnt1)
img1.text((550, 720), "plus2net", fill=fill2, font=fnt2)
img1.text((280, 800), "Grade :", fill=fill1, font=fnt1)
img1.text((550, 820), "B+", fill=fill2, font=fnt2)
img1.text((280, 900), "Class :", fill=fill1, font=fnt1)
img1.text((550, 920), "Five", fill=fill2, font=fnt2)
img.show()
img.save(path2) # Save the image in new location or path
List data over an Image
There are four elements in one List. We will use the data from the list to add to an Image.
from PIL import Image, ImageDraw, ImageFont
path = "E:\\testing\\images\\certificate_b.png" # blank certificate
path2 = "E:\\testing\\images\\certificate_12.png" # final certificate
img = Image.open(path, mode="r")
# img.show()
img1 = ImageDraw.Draw(img) # create object to Draw
fnt1 = ImageFont.truetype("arial.ttf", 70) # Font
fnt2 = ImageFont.truetype("arial.ttf", 50)
fill2 = (19, 119, 169) # colour used for data
l1=[12,'plus2net','Five',80] # List data to use for certificate
img1.text((570, 615), str(l1[0]), fill=fill2, font=fnt2)
img1.text((570, 720), l1[1], fill=fill2, font=fnt1)
img1.text((570, 870), l1[2], fill=fill2, font=fnt1)
img1.text((570, 1000), str(l1[3]), fill=fill2, font=fnt1)
img.show()
img.save(path2) # Save the image in new location or path
Bulk Data from student table
We can collect records from our student table. Using the records we will create certificate for all. The file name should have the student id.
The path to store the certificates are created using above format and inside the for loop each time a new file name is created. str() is used to convert integer to string.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://root:pw@localhost/my_db")
execute the query and get all the records. Here we are using LIMIT query to get 5 records only. This can be increased to get more records.
l1=my_conn.execute("SELECT * FROM student limit 0,5")
Full code is here
from PIL import Image, ImageDraw, ImageFont
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/my_db")
path = "E:\\testing\\images\\certificate_b.png" # blank certificate
fnt1 = ImageFont.truetype("arial.ttf", 70) # Font
fnt2 = ImageFont.truetype("arial.ttf", 50)
fill2 = (19, 119, 169) # colour used for data
l1=my_conn.execute("SELECT * FROM student limit 0,5")
for row in l1:
print(row )
img = Image.open(path, mode="r")
img1 = ImageDraw.Draw(img) # create object to Draw
path2 = "E:\\testing\\images\\certificate_"+str(row[0])+".png"
img1.text((570, 615), str(row[0]), fill=fill2, font=fnt2)
img1.text((570, 720), row[1], fill=fill2, font=fnt1)
img1.text((570, 870), row[2], fill=fill2, font=fnt1)
img1.text((570, 1000), str(row[3]), fill=fill2, font=fnt1)
img.show()
img.save(path2) # Save the image in new location or path
Generating PDF files
While creating a new image we have to use mode as RGB ( not RGBA). We can't keep Alpha Channel which specifies the opacity for a color and then convert to PDF.
While creating the image, use the option to create RGB mode only. If we don't have the choice or reading one existing image then we can convert the mode to RGB by using convert() method.
Here is the code to convert the image mode to RGB.