🔍 Code Extractor

function create_handwritten_question

Maturity: 42

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

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

Purpose

This function creates a demo/sample image that simulates a handwritten question for testing or demonstration purposes. It's designed to generate a realistic educational question image that can be used to test OCR, image processing, or question-answering systems. The function creates an 800x600 pixel white background image with a title, main question text, and bulleted sub-questions about photosynthesis, complete with decorative underlines to simulate handwritten appearance.

Source Code

def create_handwritten_question():
    """Create a sample handwritten question image"""
    img = Image.new('RGB', (800, 600), color='white')
    draw = ImageDraw.Draw(img)
    
    # Try to load a system font
    try:
        font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 28)
        title_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", 32)
    except:
        font = ImageFont.load_default()
        title_font = ImageFont.load_default()
    
    # Draw handwritten-style question
    draw.text((50, 50), "Question about Photosynthesis", fill='black', font=title_font)
    draw.text((50, 120), "What is photosynthesis and how does it work?", fill='black', font=font)
    draw.text((50, 180), "", fill='black', font=font)
    draw.text((50, 220), "Please explain:", fill='black', font=font)
    draw.text((70, 270), "• The chemical equation", fill='black', font=font)
    draw.text((70, 320), "• What inputs are needed", fill='black', font=font)
    draw.text((70, 370), "• What outputs are produced", fill='black', font=font)
    draw.text((70, 420), "• Why it's important for life on Earth", fill='black', font=font)
    
    # Add some decorative elements to make it look more handwritten
    draw.line([(45, 110), (750, 110)], fill='black', width=2)  # Underline
    
    filename = "demo_question.png"
    img.save(filename)
    print(f"✅ Created demo question: {filename}")
    return filename

Return Value

Returns a string containing the filename ('demo_question.png') of the created image. The function also saves the 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', 28)

Condition: only if Liberation Sans font is available on the system (Linux systems with liberation fonts package installed)

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

Condition: only if Liberation Sans Bold font is available on the system (Linux systems with liberation fonts package installed)

Optional

Usage Example

from PIL import Image, ImageDraw, ImageFont

# Create the demo question image
filename = create_handwritten_question()

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

# You can now use the image for testing
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 will overwrite any existing 'demo_question.png' file without warning
  • Font loading uses a try-except block to gracefully fall back to default fonts if system fonts are unavailable
  • The function is platform-dependent for font loading (hardcoded Linux font paths), but will work on any platform with fallback fonts
  • Consider wrapping the function call in error handling if disk space or permissions might be an issue
  • The returned filename is relative to the current working directory

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_test_image 72.7% 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
  • function create_math_problem 61.0% 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_instruction_diagram 60.3% similar

    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.

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

    Orchestrates a complete demonstration of an E-Ink LLM Assistant by creating three sample handwritten content files (question, instruction diagram, math problem) and processing each through an AI pipeline.

    From: /tf/active/vicechatdev/e-ink-llm/demo.py
  • class SignatureGenerator 53.2% similar

    A class that generates signature-like images from text names using italic fonts and decorative flourishes.

    From: /tf/active/vicechatdev/document_auditor/src/utils/signature_generator.py
← Back to Browse