-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_pi.py
More file actions
38 lines (34 loc) · 1.26 KB
/
Copy pathutils_pi.py
File metadata and controls
38 lines (34 loc) · 1.26 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
import numpy as np
from PIL import Image, ImageGrab
import os
import subprocess
def preprocess_canvas(canvas, temp_ps="temp.ps"):
"""
Preprocesses the Tkinter canvas content for CNN inference.
Laptop-compatible version.
"""
try:
if os.name == 'nt':
# On Windows Laptop: Use ImageGrab for a clean screenshot of the canvas
x = canvas.winfo_rootx()
y = canvas.winfo_rooty()
x1 = x + canvas.winfo_width()
y1 = y + canvas.winfo_height()
img = ImageGrab.grab().crop((x, y, x1, y1)).convert("L")
else:
# On Raspberry Pi: Use Postscript + Ghostscript (more reliable on Linux)
canvas.postscript(file=temp_ps, colormode="color")
img = Image.open(temp_ps).convert("L")
# Standard Resize to 28x28
img = img.resize((28, 28))
img_array = np.array(img) / 255.0
# Invert (Model expects 1.0 for ink)
img_array = 1.0 - img_array
return img_array.reshape(1, 28, 28, 1)
except Exception as e:
# print(f"⚠️ Canvas preprocessing error: {e}")
return None
finally:
if os.path.exists(temp_ps):
try: os.remove(temp_ps)
except: pass