handschecker / sample_images.py
Nur Arifin Akbar
Add handwriting assessment app with auto resizer and real API demo
a11d064
"""
Sample image generator for the Handwriting Assessment App
Creates realistic sample images for demonstration purposes.
Optimized for gemini3n series model requirements (768x768 resolution).
"""
from PIL import Image, ImageDraw, ImageFont
def create_sample_answer_key_image():
"""Create a sample answer key image optimized for gemini3n series (768x768)."""
img_width, img_height = 768, 768 # Optimal resolution for gemini3n series
img = Image.new('RGB', (img_width, img_height), color='white')
draw = ImageDraw.Draw(img)
# Try to use a font that looks more like handwriting
try:
# Try different handwriting-like fonts
font_large = ImageFont.truetype("Brush Script MT.ttf", 50)
font_medium = ImageFont.truetype("Brush Script MT.ttf", 45)
except:
try:
font_large = ImageFont.truetype("Comic Sans MS.ttf", 45)
font_medium = ImageFont.truetype("Comic Sans MS.ttf", 40)
except:
font_large = ImageFont.load_default()
font_medium = ImageFont.load_default()
# Sample answer key content
lines = [
("Question 1: What is the capital of France?", font_medium),
("Answer: Paris", font_large),
("", font_medium),
("Question 2: Calculate 15 + 27", font_medium),
("Answer: 42", font_large),
("", font_medium),
("Question 3: Name three primary colors", font_medium),
("Answer: Red, Blue, Yellow", font_large)
]
y_position = 80 # Start lower to center content better
for line_text, font in lines:
if line_text: # Skip empty lines
# Add slight randomness to make it look more natural
x_offset = 60 + (hash(line_text) % 15)
draw.text((x_offset, y_position), line_text, fill='black', font=font)
y_position += 80 # Increase spacing for larger canvas
return img
def create_sample_student_response_image():
"""Create a sample student response image optimized for gemini3n series (768x768)."""
img_width, img_height = 768, 768 # Optimal resolution for gemini3n series
img = Image.new('RGB', (img_width, img_height), color='white')
draw = ImageDraw.Draw(img)
try:
font_large = ImageFont.truetype("Brush Script MT.ttf", 50)
font_medium = ImageFont.truetype("Brush Script MT.ttf", 45)
except:
try:
font_large = ImageFont.truetype("Comic Sans MS.ttf", 45)
font_medium = ImageFont.truetype("Comic Sans MS.ttf", 40)
except:
font_large = ImageFont.load_default()
font_medium = ImageFont.load_default()
# Student response with slight variations
lines = [
("Question 1: What is the capital of France?", font_medium),
("Answer: Paris", font_large),
("", font_medium),
("Question 2: Calculate 15 + 27", font_medium),
("Answer: 42", font_large),
("", font_medium),
("Question 3: Name three primary colors", font_medium),
("Answer: Red, Blue, Yellow", font_large) # Same as answer key for high score
]
y_position = 80 # Start lower to center content better
for line_text, font in lines:
if line_text:
# Add different randomness for student response
x_offset = 65 + (hash(line_text + "student") % 20)
draw.text((x_offset, y_position), line_text, fill='black', font=font)
y_position += 80 # Increase spacing for larger canvas
return img