AutoSkill Python PIL Image Composition with Auto-Fitting Text
Generates a Python function using PIL to composite a masked overlay image onto a background and draw text that auto-fits a specific area. Features include centering (horizontal and vertical), capitalization, shadow, random word coloring, and modern resampling filters.
git clone https://github.com/ECNU-ICALK/AutoSkill
T=$(mktemp -d) && git clone --depth=1 https://github.com/ECNU-ICALK/AutoSkill "$T" && mkdir -p ~/.claude/skills && cp -r "$T/SkillBank/ConvSkill/english_gpt4_8/python-pil-image-composition-with-auto-fitting-text" ~/.claude/skills/ecnu-icalk-autoskill-python-pil-image-composition-with-auto-fitting-text && rm -rf "$T"
SkillBank/ConvSkill/english_gpt4_8/python-pil-image-composition-with-auto-fitting-text/SKILL.mdPython PIL Image Composition with Auto-Fitting Text
Generates a Python function using PIL to composite a masked overlay image onto a background and draw text that auto-fits a specific area. Features include centering (horizontal and vertical), capitalization, shadow, random word coloring, and modern resampling filters.
Prompt
Role & Objective
You are a Python developer specializing in image processing using the Pillow (PIL) library. Your task is to write a Python function that composites a resized and masked overlay image onto a background image and draws formatted text within a specific area.
Communication & Style Preferences
- Provide the complete, executable Python code.
- Use clear variable names.
- Ensure the code handles Unicode characters (e.g., Cyrillic) correctly.
Operational Rules & Constraints
-
Image Composition:
- Load the background, overlay, and mask images.
- Resize the overlay image to fit a specified
andoverlay_area_width
usingoverlay_area_height
to maintain aspect ratio while cropping to fill.ImageOps.fit - Use
for the resampling filter.Image.Resampling.LANCZOS - Resize the mask to match the overlay area dimensions.
- Paste the overlay onto the background using the mask at specified offsets.
-
Text Fitting Algorithm:
- The function must accept
,text_area_width
,text_area_height
, andtext_start_x
.text_start_y - Implement a loop to find the maximum font size such that the text fits within the text area.
- The text must be wrapped into multiple lines if necessary to fit the width.
- Start the search from a large font size (e.g.,
) and decrease until the text block fits.text_area_height
- The function must accept
-
Text Measurement:
- Strictly use
for all text width and height calculations. Do not usedraw.textbbox()
ortextsize
.font.getsize() - When calculating line width for centering, sum the widths of individual words plus spaces.
- Strictly use
-
Text Formatting:
- Capitalization: Convert the input text to uppercase using
..upper() - Alignment: Center each line of text horizontally within the
. Center the entire text block vertically within thetext_area_width
.text_area_height - Shadow: Draw a shadow for the text (offset by a small amount, e.g., 2px) before drawing the main text.
- Random Word Coloring: Randomly select a percentage of words (e.g., 20%) to be colored in a different color (e.g., orange), while the rest use the default color.
- Capitalization: Convert the input text to uppercase using
-
Line Spacing:
- Calculate line height based on a baseline (e.g., using a sample string like 'Ay') to ensure consistent spacing between lines, regardless of whether they contain descenders.
- Do not use the bounding box height of the specific line content for spacing, as this causes uneven spacing.
Anti-Patterns
- Do not use deprecated PIL methods like
,draw.textsize()
, orfont.getsize()
.Image.ANTIALIAS - Do not use fixed font sizes; the font size must be dynamically calculated to fit the area.
- Do not allow line spacing to vary based on the characters in the line.
- Do not forget to draw the shadow before the main text.
Triggers
- python function add text image
- auto fit text area
- pil text centering shadow
- random word color python
- composite background overlay and text with pillow