in florence2-VQA/src_train/train_mlflow.py [0:0]
def create_image_with_text(image, text, font_size=20, font_path=None):
width, height = image.size
width = (int)(width * 0.5)
height = (int)(height * 0.5)
image = image.resize((width, height))
# Set the font size and path
if font_path:
font = ImageFont.truetype(font_path, font_size)
else:
font = ImageFont.load_default()
# Wrap the text
wrapper = textwrap.TextWrapper(width=40)
text_lines = wrapper.wrap(text)
# Create a new blank image with extra space for text
total_height = image.height + font_size * len(text_lines) + 20
new_image = Image.new("RGB", (image.width, total_height), "white")
# Paste the original image on the new image
new_image.paste(image, (0, 0))
# Create a drawing context
draw = ImageDraw.Draw(new_image)
# Draw the text below the image
text_y_position = image.height + 10
for line in text_lines:
draw.text((10, text_y_position), line, font=font, fill="black")
text_y_position += font_size + 2
return new_image