I have created a set of images in python utilizing PIL. In addition to this, I've implemented textwrap in order to put text onto the images I've created, however, they're not quite perfect. First, below are three examples of images I've created.
These three images have different widths, but I'd like them all to have the same width, whereas height isn't of concern and can be taller or smaller than each other; the width is the only thing that must remain consistent. In addition to this, I've used utf-8 encoding in order to get this text on the images, but I would like the font to look something more like the following
Also shown in the above image is how those boxes are stacked--That is how I'd like to have my final product. Rather than three separate images of bordered text, I'd like to have one single image containing those bordered boxes of text. Here is my current code for what I've output
for match in find_matches(text=fullText):
ct += 1
match_words = match.split(" ")
match = " ".join(match_words[:-1])
print(match)
W, H = 300, 300
base = Image.new("RGB", (W, H), (255, 255, 255))
draw = ImageDraw.Draw(base)
font = ImageFont.load_default()
current_h, pad = 50, 5
for key in textwrap.wrap(match, width=50):
line = key.encode("ascii")
w, h = draw.textsize(line, font=font)
draw.text(((W - w) / 2, current_h), line, (0, 0, 0), font=font)
current_h += h + pad
draw.text((W / 2, current_h), str(ct).encode("utf-8"), (0, 0, 0), font=font)
for count, matches in enumerate(match):
base.save(f"{ct}C.png")
bbox = ImageOps.invert(base).getbbox()
trim = base.crop(bbox)
patent = ImageOps.expand(trim, border=5, fill=(255, 255, 255))
patent = ImageOps.expand(patent, border=3, fill=(0, 0, 0))
patent.save(f"{ct}C.png")
p_w, p_h = patent.size
Image.open(result_fpath, "r")
result.paste(patent)
result.save(result_fpath)
Finally, this has to be an automated process. What I was thinking that could be done for the stacked boxes into a single image would be a for-loop that takes in the created images and then pastes them into an image of the same size as the first pasted image which resizes appropriately for each subsequent bordered box of text. I'd appreciate any help on this greatly.





