'How to insert a sentence with multiple lines on PIL

I have this code the insert text in de midle o a picture, like a slide. It insert up to three lines. But the problem that I can't change this to break the lines when it reachs the margins. I mean, break the line when its be the sentence is larger than the picture.

For who need, I explain the instructions beside the code.

 #!/usr/bin/python

import codecs
import sys
import os
from PIL import Image, ImageFont, ImageDraw, ImageColor

inputfilename = sys.argv[1]

file1 = codecs.open(inputfilename, "r", "utf-8-sig")
lines = file1.readlines()
file1.close()

font_size_field_1=0
font_color_field_1="#000000"
font_size_field_2=0
font_color_field_2="#000000"
font_size_field_3=0
font_color_field_3="#000000"
background_image = ""
output = ""
unicode = ""

for line in lines:
    if line.startswith("font_size_field_1="):
        font_size_field_1 = int(line[18:].strip())
    if line.startswith("font_color_field_1="):
        font_color_field_1 = line[19:].strip()
    if line.startswith("font_size_field_2="):
        font_size_field_2 = int(line[18:].strip())
    if line.startswith("font_color_field_2="):
        font_color_field_2 = line[19:].strip()
    if line.startswith("font_size_field_3="):
        font_size_field_3 = int(line[18:].strip())
    if line.startswith("font_color_field_3="):
        font_color_field_3 = line[19:].strip()
    if line.startswith("background_image:"):
        background_image = line[17:].strip()
    if line.startswith("output:"):
        output = line[7:].strip()
    
    if "|" in line:
       cells = line.rstrip().split("|")
       outputfilename = os.path.join(output, cells[3])
       image = Image.open(background_image)
       width, height = image.size
       #print(rect1)
       draw = ImageDraw.Draw(image)
       font = ImageFont.truetype("arial-unicode-ms.ttf", font_size_field_1)
       rect1 = (width/16, 2*height/9, 15*width/16, 4*height/9)
       text_width, text_height = draw.textsize(cells[0], font=font)
       draw.text((rect1[0]+(rect1[2]-rect1[0]-text_width)/2,rect1[1]+(rect1[3]-rect1[1]-text_height)/2), cells[0], ImageColor.getcolor(font_color_field_1, "RGB"), font=font)

       font = ImageFont.truetype("arial-unicode-ms.ttf", font_size_field_2)
       rect2 = (width/16, 4*height/9, 15*width/16, 6*height/9)
       text_width, text_height = draw.textsize(cells[1], font=font)
       draw.text((rect2[0]+(rect2[2]-rect2[0]-text_width)/2,rect2[1]+(rect2[3]-rect2[1]-text_height)/2), cells[1], ImageColor.getcolor(font_color_field_2, "RGB"), font=font)
       #draw.rectangle([(rect2[0],rect2[1]), (rect2[2],rect2[3])], outline ="green")

       font = ImageFont.truetype("arial-unicode-ms.ttf", font_size_field_3)
       rect3 = (width/16, 6*height/9, 15*width/16, 8*height/9)
       text_width, text_height = draw.textsize(cells[2], font=font)
       draw.text((rect3[0]+(rect3[2]-rect3[0]-text_width)/2,rect3[1]+(rect3[3]-rect3[1]-text_height)/2), cells[2], ImageColor.getcolor(font_color_field_3, "RGB"), font=font)
       #draw.rectangle([(rect3[0],rect3[1]), (rect3[2],rect3[3])], outline ="green")
        
       image.save(outputfilename)

       print(outputfilename)

To execute just type: python thiscode.py text_file.txt

This is the txt file:

font_size_field_1=50
font_color_field_1=#000000
font_size_field_2=50
font_color_field_2=#000000
font_size_field_3=50
font_color_field_3=#5c5b5b
background_image:C:\Users\conta\Documents\slide_eg.png
output:C:\Users\conta\AppData\Python\Python310\audios\images
line_1|line_2|line_3|21b.png
Hello|world!||22b.png
|So|on|name.png


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source