Printable images with the Pillow library

Asked

Viewed 27 times

0

I am developing an automatic certificate issuance project where I use the pandas to pull the names of a spreadsheet and Pillow to write the names in the certificate.

The problem occurs when printing the names in the image. I used the for to go through the array of names and write each one in an image, but what happens is that all the names are written in one image only, with only the first name of the list in a separate image.

I’ve tried using the while and the def but only got more bugs. My code below:

from PIL import Image, ImageDraw, ImageFont
import pandas as pd


pessoas = pd.read_excel(r"pessoas.xlsx")
nome = (pessoas['NOME'])

# Definir o modelo do certificado e as fontes que serão utilizadas
im = Image.open('certificado.png')
fnt = ImageFont.truetype("font/Twenty One.ttf", 50)
fnt2 = ImageFont.truetype("font/Cinzel-Regular.otf", 20)

texto = ImageDraw.Draw(im)
date = ['10/10/1000',(155, 300)]  # cordenadas da data da emissão
inst = ['Instituição', (485, 300)] # cordenadas do nome da INSTITUIÇÃO

# Cordenadas - NOME // DATA // INSTITUIÇÃO
cord = [(230, 210),(155, 300),(485, 300)]

for x in nome:
    texto.text(cord[0], x, font=fnt, fill=('black'))
    texto.text(cord[1], date[0], font=fnt2, fill=('black'))
    texto.text(cord[2], inst[0], font=fnt2, fill=('black'))
    im.show()

1 answer

0


You only have one instance of the image that is created the moment you call Image.open - If you simply move this line, and the line that creates the object texto into the for will work.

But it’s a good code for you to better understand how functions work, instead of just trying to stack one line under the other - You can have a function that receives the parameters open the image, write the text, and return the new image - and its for may have a single line.

  • impressed how simple it was to solve haha thank you very much had not me attack to that had already tried to create a function with these parameters and gave the same problem but following your tip worked very well

Browser other questions tagged

You are not signed in. Login or sign up in order to post.