-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdraw.py
More file actions
149 lines (127 loc) · 5.17 KB
/
draw.py
File metadata and controls
149 lines (127 loc) · 5.17 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
147
148
149
# 128x32 Pixel Canvas
import time
import datetime
import sys
import os
import pygame
from PIL import Image, ImageDraw, ImageFont
from drive import SSD1305
# Initialize SSD1305 display
disp = SSD1305.SSD1305()
disp.Init()
disp.clear()
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
draw = ImageDraw.Draw(image)
# Define the folder name
folder_name = "Saved"
# Check if the folder exists
if not os.path.exists(folder_name):
# If not, create the folder
os.makedirs(folder_name)
print(f'Folder "{folder_name}" created.')
else:
print(f'Folder "{folder_name}" already exists.')
# Initialize pygame
pygame.init()
# Set up the drawing window with a larger size for easier drawing
scale_factor = 8
pygame.display.set_caption("Canvas")
button_height = 40
window_width, window_height = width * scale_factor, height * scale_factor + button_height
window = pygame.display.set_mode((window_width, window_height))
# Set up canvas and colors
canvas = pygame.Surface((window_width, window_height - button_height))
canvas.fill((0, 0, 0)) # Black canvas
draw_color = (255, 255, 255) # White pen
erase_color = (0, 0, 0) # Black to erase
brush_size = 15
# Flag to track changes in canvas
canvas_changed = False
# Font for drawing on SSD1305 display
font = ImageFont.truetype('04B_08__.TTF', 8)
# Function to show image on SSD1305 display
def show_image_on_display(image_path):
picture = Image.open(image_path).resize((width, height)).convert('1')
disp.clear()
disp.getbuffer(picture)
disp.ShowImage()
picture.close()
# Function to draw text on SSD1305 display
def draw_text_on_display(text):
draw.rectangle((0, 0, width, height), outline=0, fill=0) # Clear previous text
draw.text((0, 0), text, font=font, fill=255)
disp.getbuffer(image)
disp.ShowImage()
# Function to save canvas as PNG
def temp_canvas():
small_canvas = pygame.transform.smoothscale(canvas, (width, height))
pygame.image.save(small_canvas, "Saved/temp.png")
show_image_on_display("Saved/temp.png")
# Button properties
button_font = pygame.font.Font(None, 30)
button_texts = ["Clear", "Save", "Increase Brush", "Decrease Brush"]
buttons = []
button_width = window_width // 4
for i, text in enumerate(button_texts):
rect = pygame.Rect(i * button_width, window_height - button_height, button_width, button_height)
buttons.append((rect, text, False)) # Add False to track if the button is pressed
# Main loop
running = True
last_save_time = time.time()
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEMOTION:
if pygame.mouse.get_pressed()[0]: # Left mouse button draws
mouseX, mouseY = event.pos
if mouseY < window_height - button_height:
pygame.draw.circle(canvas, draw_color, (mouseX, mouseY), brush_size)
canvas_changed = True
elif pygame.mouse.get_pressed()[2]: # Right mouse button erases
mouseX, mouseY = event.pos
if mouseY < window_height - button_height:
pygame.draw.circle(canvas, erase_color, (mouseX, mouseY), brush_size)
canvas_changed = True
elif event.type == pygame.MOUSEBUTTONDOWN:
mouseX, mouseY = event.pos
for i, (rect, text, _) in enumerate(buttons):
if rect.collidepoint(mouseX, mouseY):
buttons[i] = (rect, text, True) # Mark the button as pressed
if text == "Clear":
canvas.fill((0, 0, 0)) # Clear the canvas
canvas_changed = True
elif text == "Save":
current_time = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")
small_canvas = pygame.transform.smoothscale(canvas, (width, height))
filename = f"Saved/{current_time}.png"
pygame.image.save(small_canvas, filename)
elif text == "Increase Brush":
brush_size += 1
elif text == "Decrease Brush":
brush_size -= 1
elif event.type == pygame.MOUSEBUTTONUP:
for i, (rect, text, pressed) in enumerate(buttons):
if pressed:
buttons[i] = (rect, text, False) # Mark the button as not pressed
# Autosave every 1 second if canvas has changed
current_time = time.time()
if current_time - last_save_time >= 0.01 and canvas_changed:
temp_canvas()
last_save_time = current_time
canvas_changed = False
# Draw everything on the pygame window
window.fill((0, 0, 0))
window.blit(canvas, (0, 0))
# Draw buttons
for rect, text, pressed in buttons:
color = (75, 75, 75) if pressed else (100, 100, 100) # change button color when pressed
pygame.draw.rect(window, color, rect)
text_surface = button_font.render(text, True, (255, 255, 255))
window.blit(text_surface, text_surface.get_rect(center=rect.center))
pygame.display.flip()
# Quit pygame
pygame.quit()
sys.exit()