function create_math_problem
Maturity: 44
Generates a PNG image file containing a sample algebra math problem with workspace lines for solving.
File:
/tf/active/vicechatdev/e-ink-llm/demo.py
Lines:
85 - 116
85 - 116
Complexity:
simple
simple
Purpose
Creates a demonstration image file (demo_math.png) showing a formatted algebra problem '2x + 7 = 23' with title, instructions, and workspace lines. Useful for testing image processing pipelines, OCR systems, educational content generation, or demonstrating document analysis capabilities.
Source Code
def create_math_problem():
"""Create a sample math problem"""
img = Image.new('RGB', (800, 600), color='white')
draw = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 26)
title_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", 30)
math_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf", 28)
except:
font = ImageFont.load_default()
title_font = ImageFont.load_default()
math_font = ImageFont.load_default()
# Draw math problem
draw.text((50, 50), "Algebra Problem", fill='black', font=title_font)
draw.line([(45, 90), (250, 90)], fill='black', width=2)
draw.text((50, 140), "Solve for x:", fill='black', font=font)
draw.text((100, 200), "2x + 7 = 23", fill='black', font=math_font)
draw.text((50, 280), "Show all steps and check your answer.", fill='black', font=font)
# Add some workspace lines
for i in range(5):
y = 350 + (i * 30)
draw.line([(50, y), (700, y)], fill='lightgray', width=1)
filename = "demo_math.png"
img.save(filename)
print(f"✅ Created demo math problem: {filename}")
return filename
Return Value
Returns a string containing the filename of the created image ('demo_math.png'). The function also saves the image file to 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
Usage Example
from PIL import Image, ImageDraw, ImageFont
def create_math_problem():
img = Image.new('RGB', (800, 600), color='white')
draw = ImageDraw.Draw(img)
try:
font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Regular.ttf", 26)
title_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationSans-Bold.ttf", 30)
math_font = ImageFont.truetype("/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf", 28)
except:
font = ImageFont.load_default()
title_font = ImageFont.load_default()
math_font = ImageFont.load_default()
draw.text((50, 50), "Algebra Problem", fill='black', font=title_font)
draw.line([(45, 90), (250, 90)], fill='black', width=2)
draw.text((50, 140), "Solve for x:", fill='black', font=font)
draw.text((100, 200), "2x + 7 = 23", fill='black', font=math_font)
draw.text((50, 280), "Show all steps and check your answer.", fill='black', font=font)
for i in range(5):
y = 350 + (i * 30)
draw.line([(50, y), (700, y)], fill='lightgray', width=1)
filename = "demo_math.png"
img.save(filename)
print(f"✅ Created demo math problem: {filename}")
return filename
# Usage
output_file = create_math_problem()
print(f"Math problem saved to: {output_file}")
Best Practices
- The function has no parameters and always creates the same math problem - consider parameterizing if you need variety
- The output filename is hardcoded as 'demo_math.png' and will overwrite existing files with the same name
- Font loading uses a try-except block to gracefully fall back to default fonts if Liberation fonts are not available
- The function prints to stdout - consider removing or making this optional for library usage
- Ensure write permissions exist in the current directory before calling
- The function returns the filename string, which can be used for further processing or verification
- Image dimensions (800x600) and all positioning values are hardcoded - consider making these configurable for reusability
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_handwritten_question 61.0% similar
-
function create_instruction_diagram 55.7% similar
-
class IllustrationGenerator 52.9% similar
-
function create_test_image 49.8% similar
-
function demo_graphics_generation 49.4% similar