agentsclimarketplace

Python pil image composition with auto fitting text

Skill ECNU-ICALK/AutoSkill/SkillBank/ConvSkill/english_gpt4_8/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.From its SKILL.md

Install
npx -y skills add ECNU-ICALK/AutoSkill --skill python-pil-image-composition-with-auto-fitting-text

Assembled from the repository path, not quoted from the project. Check it against their README if it does not work.

One thing to look at

  • no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.

SKILL.md

3.9 KB, 744 tokens by cl100k_base, as published. Nobody here has run it

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.

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

  1. Image Composition:

    • Load the background, overlay, and mask images.
    • Resize the overlay image to fit a specified overlay_area_width and overlay_area_height using ImageOps.fit to maintain aspect ratio while cropping to fill.
    • Use Image.Resampling.LANCZOS for the resampling filter.
    • Resize the mask to match the overlay area dimensions.
    • Paste the overlay onto the background using the mask at specified offsets.
  2. Text Fitting Algorithm:

    • The function must accept text_area_width, text_area_height, text_start_x, and 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., text_area_height) and decrease until the text block fits.
  3. Text Measurement:

    • Strictly use draw.textbbox() for all text width and height calculations. Do not use textsize or font.getsize().
    • When calculating line width for centering, sum the widths of individual words plus spaces.
  4. Text Formatting:

    • Capitalization: Convert the input text to uppercase using .upper().
    • Alignment: Center each line of text horizontally within the text_area_width. Center the entire text block vertically within the 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.
  5. 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(), font.getsize(), or 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

What ships with it

Read from the repository

Just SKILL.md. No reference files, no scripts.

Keep looking

Skills are one crate of 325,949. Ordering is by how many stacks a row turns up in, so the top of any crate is what has actually been picked rather than what has the most stars.