function create_handwritten_question
Generates a synthetic handwritten-style question image about photosynthesis with formatted text and decorative elements, saved as a PNG file.
/tf/active/vicechatdev/e-ink-llm/demo.py
17 - 46
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
PILPillow
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)
OptionalImageFont.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)
OptionalUsage 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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_test_image 72.7% similar
-
function create_math_problem 61.0% similar
-
function create_instruction_diagram 60.3% similar
-
function run_demo 55.0% similar
-
class SignatureGenerator 53.2% similar