python how do I change the text in the image?

You need to create a bunch of letters using a template (psd tiff is not essential) with a changing full name and number and save each one in a separate jpg

How best to implement it and with what tools? I can't find any normal libraries for editing images

1 answers

Using PIL, for example. Minimally, you will need to import Image, ImageFont (to connect a truetype font), and ImageDraw from there. Save the letter form without the text, and then draw the text on top. Something like this:

from PIL import ImageFont
from PIL import Image
from PIL import ImageDraw

# определяете шрифт
font = ImageFont.trueType('/путь/до/файла/со/шрифтом.ttf')

# определяете положение текста на картинке
text_position = (150, 150)

# цвет текста, RGB
text_color = (255,0,0)

# собственно, сам текст
text = 'Фамилия Имя Отчество'

# загружаете фоновое изображение
img = Image.open('blank.jpg')

# определяете объект для рисования
draw = ImageDraw.Draw(img)

# добавляем текст
draw.text(text_position, text, text_color, font)

# сохраняем новое изображение
img.save('blank_with_text.jpg')

In short, something like that... Probably, the position and size of the text will have to be selected experimentally.

 3
Author: stxdtm, 2017-04-08 10:26:17