I’m trying to ensure that when text exceeds the width of the image (variable images sizes) that the text gets scaled down to fit aesthetically.
I’m trying to do that by using max text width, but I cannot seem to get it to function properly.
<code>
with Image.open(filename).convert("RGBA") as img:
font_size = int(img.width * 0.2) # % of image width
x = img.width * 0.05 # 5% from left edge
y = img.height * 0.1 # 10% from top edge
watermark = Image.new("RGBA", img.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("impact.ttf", font_size)
max_text_width = int(img.width * 0.9)
wrapped_text = wrap_text(text, font, max_text_width)
draw.multiline_text((x, y), wrapped_text, font=font, stroke_width=1, stroke_fill=(0, 0, 0, 255), fill=(255, 255, 255, 256))
watermark_image = Image.open("watermark.png").convert("RGBA")
watermark_size = tuple([int(0.15 * x) for x in watermark_image.size])
watermark_image = watermark_image.resize(watermark_size)
watermark_position = (img.width - watermark_image.width, img.height - watermark_image.height)
img.paste(watermark_image, watermark_position, mask=watermark_image)
</code>
<code>
with Image.open(filename).convert("RGBA") as img:
font_size = int(img.width * 0.2) # % of image width
x = img.width * 0.05 # 5% from left edge
y = img.height * 0.1 # 10% from top edge
watermark = Image.new("RGBA", img.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("impact.ttf", font_size)
max_text_width = int(img.width * 0.9)
wrapped_text = wrap_text(text, font, max_text_width)
draw.multiline_text((x, y), wrapped_text, font=font, stroke_width=1, stroke_fill=(0, 0, 0, 255), fill=(255, 255, 255, 256))
watermark_image = Image.open("watermark.png").convert("RGBA")
watermark_size = tuple([int(0.15 * x) for x in watermark_image.size])
watermark_image = watermark_image.resize(watermark_size)
watermark_position = (img.width - watermark_image.width, img.height - watermark_image.height)
img.paste(watermark_image, watermark_position, mask=watermark_image)
</code>
with Image.open(filename).convert("RGBA") as img:
font_size = int(img.width * 0.2) # % of image width
x = img.width * 0.05 # 5% from left edge
y = img.height * 0.1 # 10% from top edge
watermark = Image.new("RGBA", img.size, (255, 255, 255, 0))
draw = ImageDraw.Draw(watermark)
font = ImageFont.truetype("impact.ttf", font_size)
max_text_width = int(img.width * 0.9)
wrapped_text = wrap_text(text, font, max_text_width)
draw.multiline_text((x, y), wrapped_text, font=font, stroke_width=1, stroke_fill=(0, 0, 0, 255), fill=(255, 255, 255, 256))
watermark_image = Image.open("watermark.png").convert("RGBA")
watermark_size = tuple([int(0.15 * x) for x in watermark_image.size])
watermark_image = watermark_image.resize(watermark_size)
watermark_position = (img.width - watermark_image.width, img.height - watermark_image.height)
img.paste(watermark_image, watermark_position, mask=watermark_image)
Am I missing something crucial?