Now I write you few functions on python which add image and text on background. Change…
- User
Now I write you few functions on python which add image and text on background. Change these functions so that text be centralized (aligment), but save all the features like different colors for random words etc. Code: from PIL import Image, ImageDraw, ImageFont, ImageOps import textwrap import random
def draw_text(draw, text, font, default_text_color, text_area_width, text_area_height, start_x, start_y, shadow_offset, color_ratio=0.2, alternate_color='orange'): text = text.upper() words = text.split()
Determine the number of words to color differently, based on color_ratio
num_words_to_color = max(1, int(len(words) * color_ratio))
Randomly pick unique words to color differently
words_to_color = set(random.sample(words, num_words_to_color))
lines = textwrap.wrap(text, width=text_area_width // (font.size // 2))
y_offset = start_y for line in lines: # Break the line into words and calculate line height for consistent spacing line_words = line.split() line_height = draw.textbbox((0, 0), line, font=font)[3]
x_offset = start_x for word in line_words: # Set word color depending on whether it's selected to be colored differently word_color = alternate_color if word in words_to_color else default_text_color # Draw shadow first draw.text((x_offset + shadow_offset, y_offset + shadow_offset), word, font=font, fill='black') # Draw the word itself draw.text((x_offset, y_offset), word, font=font, fill=word_color) bbox = draw.textbbox((x_offset, y_offset), word + ' ', font=font) # Space included for naturally spacing words x_offset += bbox[2] - bbox[0] y_offset += line_height + shadow_offsetdef add_image_and_text_to_background_with_mask6( background_path, overlay_image_path, mask_path, output_path, overlay_area_width, overlay_area_height, text_area_width, text_area_height, text_start_x, text_start_y, text, font_path, initial_font_size, text_color, shadow_color, alternate_color='orange', color_ratio=0.5):
Load background and images
background = Image.open(background_path) overlay_image = Image.open(overlay_image_path).resize((overlay_area_width, overlay_area_height), resample=Image.Resampling.LANCZOS) mask_image = Image.open(mask_path).resize((overlay_area_width, overlay_area_height), resample=Image.Resampling.LANCZOS)
Ensure the background is the correct size (1080x1350)
assert background.size == (1080, 1350)
Paste the overlay image onto the background using the mask
background.paste(overlay_image, (50, 50), mask_image)
Initialize the drawing
font = ImageFont.truetype(font_path, initial_font_size) draw = ImageDraw.Draw(background)
Set the seed for reproducibility, if needed
random.seed(1)
draw_text(draw, text, font, text_color, text_area_width, text_area_height, text_start_x, text_start_y, 2, color_ratio=color_ratio, alternate_color=alternate_color)
- Assistant
- User
Use textbbox instead of textsize
- Assistant
- User
Perfect, add possibility to specify additional line spacing between lines of text.
- Assistant
- User
Modify the function so that the indentation between lines is counted with respect to the baseline of the line. Because in your proposed variant, the indentation is counted from the lowest character (descender), so in cases when a line contains a comma or a letter with a descender, the indentation between this line and the next line is larger than between other lines (where there are no commas or descenders). It is necessary to have the same indentation between all lines (regardless of whether they contain commas or descendents).
- Assistant
- User
In the program you suggested, only the shadows of the text are spelled correctly, and the text itself is overlapped at the top of the picture.
- Assistant
- User
I got error AttributeError: 'FreeTypeFont' object has no attribute 'getoffset' in line (x_offset, y_offset + font.getoffset(word)[1]),
- Assistant
- User
Modify the function so that if the input text is too long, the function can automatically decrement font size that would allow the text to fit within the specified area.
- Assistant
- User
Use textbbox instead of textsize
- Assistant
- User
I got error AttributeError: 'FreeTypeFont' object has no attribute 'read'
- Assistant