I'm working on a tool to add text to specific regions of an image, like inside a speech bubble. I'm generating a bounding box and would like to restrict the text to only be inside of this bounding box, but so far looking into the cv2 and PIL libraries they only seem to take a starting point for the text, not a bounding box.
cv2:
import cv2
myimg = "471.jpg"
img = cv2.imread(myimg)
f_face = cv2.FONT_HERSHEY_PLAIN
f_size = 1
f_color = (255,255,255)
f_thickness = 2
text = 'Hello World'
x,y = 400,800
img = cv2.putText(img, text, (x,y), f_face, f_size, f_color, f_thickness)
cv2.imshow("Test", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
PIL:
from PIL import Image
from PIL import ImageDraw
myimg = "471.jpg"
img = Image.open(myimg)
id = ImageDraw.Draw(img)
id.text((400, 800), "Hello World", fill=(255, 255, 255))
img.show()
Ideally, I'd be looking for a library that could give me the same amount of control over the text as you'd have when using Photoshop, meaning: font size, font face, center (to bounding box/text box) vertically and center (to bounding box/text box) horizontally as well as auto-resizing either the font size or bounding box to fit the text to it is not cut off (or at the very least give some kind of warning that the text has overflowed the textbox so that I can make a process that reduces the font size and tries again).
Are there other libraries out there that have this kind of control? Or are there ways of doing this inside of cv2 or PIL?
Thank you for your help