I would like to create a python script that dynamically adds text to fit within the center of the image, regardless of the amount of text. I have a working script that does this which I've pasted below. Now I would like to surround the text with a gray box.
What I've tried to do was create a gray image then add the text to it and then add that to the original image. But for whatever reason I can't get that image to resize to the text.
Here is the working script:
from PIL import Image, ImageDraw, ImageFont
import textwrap
from string import ascii_letters
img = Image.open(fp='background.jpg', mode='r')
font = ImageFont.truetype(font='arial', size=50)
draw = ImageDraw.Draw(im=img)
text = """Simplicity--the art of maximizing the amount of work not done--is essential."""
avg_char_width = sum(font.getsize(char)[0] for char in ascii_letters) / len(ascii_letters)
max_char_count = int(img.size[0] * .83 / avg_char_width)
text = textwrap.fill(text=text, width=max_char_count)
draw.text(xy=(img.size[0]/2, img.size[1] / 2), text=text, font=font, fill='#000000', anchor='mm')
img.show()


