🔍 Code Extractor

function create_instruction_diagram

Maturity: 42

Generates a sample instructional diagram image showing step-by-step coffee making instructions with text, lines, shapes, and an arrow pointing to a coffee maker illustration.

File:
/tf/active/vicechatdev/e-ink-llm/demo.py
Lines:
48 - 83
Complexity:
simple

Purpose

Creates a demonstration PNG image file containing formatted instructional content. This function is useful for testing image processing pipelines, generating sample data for documentation, or demonstrating PIL/Pillow image creation capabilities. It produces a 800x600 pixel white background image with a title, numbered steps, decorative elements, and a simple diagram component.

Source Code

def create_instruction_diagram():
    """Create a sample instruction diagram"""
    img = Image.new('RGB', (800, 600), color='white')
    draw = ImageDraw.Draw(img)
    
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 24)
        title_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", 28)
    except:
        font = ImageFont.load_default()
        title_font = ImageFont.load_default()
    
    # Draw instruction content
    draw.text((50, 40), "How to Make Coffee", fill='black', font=title_font)
    draw.line([(45, 80), (300, 80)], fill='black', width=2)
    
    draw.text((50, 120), "Steps:", fill='black', font=font)
    draw.text((70, 160), "1. Heat water to 200°F", fill='black', font=font)
    draw.text((70, 200), "2. Grind coffee beans (medium)", fill='black', font=font)
    draw.text((70, 240), "3. Add 2 tbsp coffee to filter", fill='black', font=font)
    draw.text((70, 280), "4. Pour hot water slowly", fill='black', font=font)
    draw.text((70, 320), "5. Wait 4 minutes", fill='black', font=font)
    draw.text((70, 360), "6. Enjoy!", fill='black', font=font)
    
    # Draw a simple diagram
    draw.rectangle([(450, 120), (650, 220)], outline='black', width=2)
    draw.text((480, 160), "Coffee Maker", fill='black', font=font)
    
    # Arrow
    draw.line([(350, 200), (440, 200)], fill='black', width=3)
    draw.polygon([(440, 195), (440, 205), (450, 200)], fill='black')
    
    filename = "demo_instructions.png"
    img.save(filename)
    print(f"✅ Created demo instructions: {filename}")
    return filename

Return Value

Returns a string containing the filename of the created image ('demo_instructions.png'). The function also saves the generated image to disk in the current working directory and prints a success message to stdout.

Dependencies

  • PIL
  • Pillow

Required Imports

from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont

Conditional/Optional Imports

These imports are only needed under specific conditions:

ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf', 24)

Condition: only if Liberation Sans font is available on the system at the specified path (Linux systems)

Optional
ImageFont.truetype('/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf', 28)

Condition: only if Liberation Sans Bold font is available on the system at the specified path (Linux systems)

Optional
ImageFont.load_default()

Condition: fallback when TrueType fonts are not available or font loading fails

Required (conditional)

Usage Example

from PIL import Image, ImageDraw, ImageFont

# Create the instruction diagram
filename = create_instruction_diagram()

# The function returns the filename and saves the image
print(f"Image saved as: {filename}")

# You can now open or process the created image
img = Image.open(filename)
img.show()  # Display the image

Best Practices

  • Ensure write permissions exist in the current directory before calling this function
  • The function overwrites any existing 'demo_instructions.png' file without warning
  • Font loading uses a try-except block to gracefully fall back to default fonts on systems without Liberation Sans
  • The hardcoded font path is Linux-specific; may not work on Windows or macOS without modification
  • Consider parameterizing the output filename to avoid conflicts in multi-threaded environments
  • The function has side effects (file I/O and print statements); consider separating image creation from file saving for better testability

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_handwritten_question 60.3% similar

    Generates a synthetic handwritten-style question image about photosynthesis with formatted text and decorative elements, saved as a PNG file.

    From: /tf/active/vicechatdev/e-ink-llm/demo.py
  • function create_math_problem 55.7% similar

    Generates a PNG image file containing a sample algebra math problem with workspace lines for solving.

    From: /tf/active/vicechatdev/e-ink-llm/demo.py
  • function create_test_image 55.5% similar

    Creates a synthetic test image with text rendered in a handwritten-style font on a white background and saves it to disk.

    From: /tf/active/vicechatdev/e-ink-llm/test.py
  • class IllustrationGenerator 54.6% similar

    A class that generates educational illustrations and technical drawings for mathematical and scientific concepts using matplotlib and programmatic rendering.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
  • class DiagramGenerator 54.3% similar

    A class that generates various types of diagrams including flowcharts, process flows, and network diagrams using matplotlib and networkx, returning base64-encoded PNG images.

    From: /tf/active/vicechatdev/e-ink-llm/graphics_generator.py
← Back to Browse