-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdebug_partial.py
More file actions
146 lines (126 loc) · 6.04 KB
/
debug_partial.py
File metadata and controls
146 lines (126 loc) · 6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
import os
from PIL import Image, ImageFont, ImageDraw
import random
def load_icons(icon_folder):
icons = {}
for root, dirs, files in os.walk(icon_folder):
icon_name = os.path.basename(root)
if icon_name not in icons:
icons[icon_name] = []
for filename in files:
if filename.endswith(".png"):
icons[icon_name].append(Image.open(os.path.join(root, filename)))
break
return icons
def create_visual_equation(equation, icons, font, icon_map):
elements = equation.split()
images = []
coefficient = 1
metadata = {}
# List of available icon types
available_icon_types = [k for k, v in icons.items() if v]
if len(available_icon_types) < 2:
raise ValueError("Not enough icon types available")
# Larger font for symbols and coefficients
try:
large_font = ImageFont.load_default().font_variant(size=30)
except Exception:
large_font = ImageFont.load_default()
print(f"Processing equation: {equation}")
print(f"Elements: {elements}")
for idx, element in enumerate(elements):
print(f"Processing element {idx}: '{element}'")
if element.isdigit() and idx != len(elements) - 1:
# This is a coefficient for the next variable
coefficient = int(element)
print(f" -> Set coefficient to {coefficient}")
elif element.isalpha():
print(f" -> Processing variable '{element}' with coefficient {coefficient}")
# This is a variable - create coefficient + icon combination
if element not in icon_map:
used_types = set(icon_map.values())
available_types = [t for t in available_icon_types if t not in used_types]
if not available_types:
raise ValueError("Not enough unique icon types available")
icon_type = random.choice(available_types)
icon_map[element] = icon_type
print(f" -> Assigned icon type '{icon_type}' to variable '{element}'")
# Get the icon
icon_variations = icons.get(icon_map[element], [])
if not icon_variations:
raise ValueError(f"No icons available for type {icon_map[element]}")
selected_icon = random.choice(icon_variations)
icon_img = selected_icon.convert("RGBA").resize((50, 50), Image.Resampling.LANCZOS)
# Create coefficient text image
coeff_img = Image.new('RGBA', (30, 50), (255, 255, 255, 255))
draw = ImageDraw.Draw(coeff_img)
draw.text((5, 10), str(coefficient), font=large_font, fill=(0, 0, 0))
# Combine coefficient + icon horizontally
combined_width = coeff_img.width + icon_img.width
combined_height = max(coeff_img.height, icon_img.height)
combined_img = Image.new('RGB', (combined_width, combined_height), (255, 255, 255))
# Paste coefficient first, then icon
if coeff_img.mode == 'RGBA':
combined_img.paste(coeff_img, (0, 0), coeff_img)
else:
combined_img.paste(coeff_img, (0, 0))
if icon_img.mode == 'RGBA':
combined_img.paste(icon_img, (coeff_img.width, 0), icon_img)
else:
combined_img.paste(icon_img, (coeff_img.width, 0))
images.append(combined_img)
metadata[element] = (icon_map[element], coefficient)
coefficient = 1 # Reset coefficient
print(f" -> Created combined image size: {combined_img.size}")
else:
# Handle symbols (+, -, =) and constants
if element in ['+', '-', '=', 'x']:
symbol_img = Image.new('RGB', (30, 50), (255, 255, 255))
draw = ImageDraw.Draw(symbol_img)
draw.text((10, 10), element, font=large_font, fill=(0, 0, 0))
images.append(symbol_img)
print(f" -> Created symbol image for '{element}', size: {symbol_img.size}")
elif idx == len(elements) - 1: # Last element is usually the constant
constant_img = Image.new('RGB', (50, 50), (255, 255, 255))
draw = ImageDraw.Draw(constant_img)
draw.text((10, 10), element, font=large_font, fill=(0, 0, 0))
images.append(constant_img)
print(f" -> Created constant image for '{element}', size: {constant_img.size}")
print(f"Total images created: {len(images)}")
for i, img in enumerate(images):
print(f" Image {i}: size {img.size}")
# Combine images side by side
if images:
widths, heights = zip(*(i.size for i in images))
total_width = sum(widths)
max_height = max(heights)
combined_image = Image.new('RGB', (total_width, max_height), color=(255, 255, 255))
x_offset = 0
for img in images:
if img.mode == 'RGBA':
# Create a white background and paste the RGBA image on it
temp_img = Image.new('RGB', img.size, (255, 255, 255))
temp_img.paste(img, (0, 0), img)
combined_image.paste(temp_img, (x_offset, 0))
else:
combined_image.paste(img, (x_offset, 0))
x_offset += img.width
print(f"Final combined image size: {combined_image.size}")
return combined_image, metadata
else:
print("No images created!")
return None, metadata
if __name__ == "__main__":
icons = load_icons("colored_icons_final")
font = ImageFont.load_default()
# Test with one equation
equation = "6 a + 5 b + 5 c = 100"
icon_map = {}
img, metadata = create_visual_equation(equation, icons, font, icon_map)
if img:
img.save("debug_single_equation.png")
print(f"Saved image: debug_single_equation.png, size: {img.size}")
print(f"Metadata: {metadata}")
else:
print("Failed to create image")