How do you add a black background to text in Python PIL?

Viewed 288

I'm looking to put black backgrounds on text using PIL in Python that automatically adjust to the width and height of the text.

Currently, I have code that constructs the text at equal distances from each other. How would I now put a black background on each line?

font_fname = r'user\arial.ttf'
font_size = 50
font = ImageFont.truetype(font_fname, font_size)
text = 'This is a picture of a dolphin having fun'
text_items = wrap(text, 25)
for i in range(4):
    try:
        number = str(i)
        item = text_items[i]
        exec(str("line" + number + ' = ''"""' + item + '"""'))
    except:
        pass
spacing = spacing + 30
par_spacing = 40
for i in range(len(opening_par_items) + 1):
    line_number = str(i)
    try:
        exec("draw.text((130, " + str(spacing) + "), line_" + line_number + ", font=font , fill='rgb(255, 255, 255)')")
        spacing = spacing + par_spacing
    except:
        pass

The output would aim to be something like this

0 Answers
Related