Draw text on a background image #149291
Replies: 1 comment 1 reply
-
Here's how you can implement overlaying three pieces of content consecutively and centering them on a background image, using your wrap_text and draw_text functions. I’ll include the logic for overlaying the content and ensure everything is aligned properly. from PIL import Image, ImageDraw, ImageFont
def wrap_text(text, font, max_width, draw):
words = text.split()
lines = []
current_line = words[0]
for word in words[1:]:
test_line = current_line + ' ' + word
bbox = draw.textbbox((0, 0), test_line, font=font)
text_width = bbox[2] - bbox[0]
if text_width > max_width:
lines.append(current_line)
current_line = word
else:
current_line = test_line
lines.append(current_line)
return lines
def draw_text(draw, text, position, font, color, max_width, line_spacing, alignment):
lines = wrap_text(text, font, max_width, draw)
x, y = position
line_height = font.getbbox('A')[3]
for line in lines:
# Calculate the bounding box for the current line
bbox = draw.textbbox((0, 0), line, font=font)
text_width = bbox[2] - bbox[0]
# Adjust x based on alignment
if alignment == 'right':
draw.text((x - text_width, y), line, font=font, fill=color)
elif alignment == 'center':
draw.text(((x - text_width) / 2, y), line, font=font, fill=color)
else:
draw.text((x, y), line, font=font, fill=color)
# Move y down to the next line
y += line_height + line_spacing
return y
# Main function to overlay text on a background image
def overlay_text_on_image(background_path, output_path, content_list):
# Open the background image
image = Image.open(background_path)
draw = ImageDraw.Draw(image)
image_width, image_height = image.size
# Iterate through the content pieces
y_position = image_height // 4 # Start at 1/4th of the image height
for content in content_list:
text = content['text']
font = ImageFont.truetype(content['font'], content['font_size'])
color = content['color']
max_width = image_width - 40 # Allow some padding
line_spacing = content['line_spacing']
alignment = content['alignment']
# Draw the text and update y_position
y_position = draw_text(draw, text, (image_width // 2, y_position), font, color, max_width, line_spacing, alignment)
# Add extra space between blocks
y_position += 40
# Save the resulting image
image.save(output_path)
print(f"Image saved at {output_path}")
# Example usage
content_list = [
{
'text': "First piece of content with a bold font.",
'font': 'arialbd.ttf', # Replace with a bold font file path
'font_size': 40,
'color': 'red',
'line_spacing': 10,
'alignment': 'center',
},
{
'text': "Second piece of content with a script font.",
'font': 'ariali.ttf', # Replace with an italic font file path
'font_size': 30,
'color': 'blue',
'line_spacing': 15,
'alignment': 'center',
},
{
'text': "Third piece of content with a modern font.",
'font': 'arial.ttf', # Replace with a regular font file path
'font_size': 25,
'color': 'green',
'line_spacing': 8,
'alignment': 'center',
},
]
# Call the function
overlay_text_on_image('background.jpg', 'output.jpg', content_list) Each piece of content is defined with its own text, font, size, color, spacing, and alignment. Dynamic Text Placement: The y_position starts at 1/4th of the image height and updates after each piece of content, ensuring spacing between blocks. Background Image: The resulting image is saved to output.jpg. |
Beta Was this translation helpful? Give feedback.
-
Body
I want to overlay 3 pieces of content consecutively and center them on a background image. Each piece of content has a different style and font. I’m not sure how to implement this. Could someone help me, please? Thank you!
def wrap_text(text, font, max_width, draw):
words = text.split()
lines = []
current_line = words[0]
for word in words[1:]:
test_line = current_line + ' ' + word
bbox = draw.textbbox((0, 0), test_line, font=font)
text_width = bbox[2] - bbox[0]
if text_width > max_width:
lines.append(current_line)
current_line = word
else:
current_line = test_line
lines.append(current_line)
return lines
def draw_text(draw, text, position, font, color, max_width, line_spacing, alignment):
lines = wrap_text(text, font, max_width, draw)
x, y = position
line_height = font.getbbox('A')[3]
Guidelines
Beta Was this translation helpful? Give feedback.
All reactions