-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhiteboard.py
183 lines (151 loc) · 7.3 KB
/
whiteboard.py
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import tkinter as tk
from tkinter import filedialog, messagebox, colorchooser
from PIL import Image, ImageDraw, ImageTk
class PaintApp:
def __init__(self, root):
self.root = root
self.root.title("Paint with Tkinter")
self.root.geometry("1000x600")
self.root.resizable(width = True,height = True)
self.canvas = tk.Canvas(root, bg="white", width=800, height=600)
self.canvas.pack(side=tk.LEFT, fill="both", expand=True)
self.image = Image.new("RGB", (800, 600), "white")
self.draw = ImageDraw.Draw(self.image)
self.drawing = False
self.last_x, self.last_y = None, None
self.brush_size = 2
self.brush_color = "black"
self.last_brush_color = "black"
self.mode = "brush"
self.history = []
self.redo_history = []
self.current_stroke = []
self.canvas.bind("<ButtonPress-1>", self.start_drawing)
self.canvas.bind("<B1-Motion>", self.draw_line)
self.canvas.bind("<ButtonRelease-1>", self.stop_drawing)
self.create_side_menu()
self.create_menu()
def create_side_menu(self):
side_menu = tk.Frame(self.root, width=200, bg="#1E1E1E")
side_menu.pack(side=tk.RIGHT, fill="y")
color_picker_button = tk.Button(side_menu, text="Choose Color", command=self.choose_color)
color_picker_button.pack(pady=0)
brush_size_label = tk.Label(side_menu, text="Brush Size")
brush_size_label.pack(pady=5)
self.brush_size_slider = tk.Scale(side_menu, from_=1, to=100, orient=tk.HORIZONTAL, command = self.update_brush_size)
self.brush_size_slider.set(self.brush_size)
self.brush_size_slider.pack(pady=10)
self.mode_label = tk.Label(side_menu, text="Mode: Brush")
self.mode_label.pack(pady=5)
brush_button = tk.Button(side_menu, text="Brush", command=lambda: self.set_mode("brush"))
brush_button.pack(pady=5)
erase_button = tk.Button(side_menu, text="Erase", command=lambda: self.set_mode("erase"))
erase_button.pack(pady=5)
save_button = tk.Button(side_menu, text="Save", command = self.save_image)
save_button.pack(pady=5)
clear_button = tk.Button(side_menu, text="Clear", command = self.clear_canvas)
clear_button.pack(pady=5)
undo_button = tk.Button(side_menu, text="Undo", command = self.undo)
undo_button.pack(pady=5)
redo_button = tk.Button(side_menu, text="Redo", command = self.redo)
redo_button.pack(pady=5)
Size = tk.Entry(side_menu, width = 10)
Size.insert(tk.END,'Insert Size')
Size.pack(pady = 5)
Size.bind("<FocusIn>", lambda event: Size.delete(0,tk.END))
Import_img = tk.Button(side_menu, text = 'Import IMG', command = lambda: self.Popup(Size.get()))
Import_img.pack(pady = 5)
def create_menu(self):
menubar = tk.Menu(self.root)
self.root.config(menu=menubar)
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="Save", command=self.save_image, accelerator="Ctrl+S")
file_menu.add_command(label="Clear", command=self.clear_canvas, accelerator="Ctrl+C")
file_menu.add_command(label="Undo", command=self.undo, accelerator="Ctrl+Z")
file_menu.add_command(label="Redo", command=self.redo, accelerator="Ctrl+Y")
self.root.bind("<Control-s>", lambda event: self.save_image())
self.root.bind("<Control-c>", lambda event: self.clear_canvas())
self.root.bind("<Control-z>", lambda event: self.undo())
self.root.bind("<Control-y>", lambda event: self.redo())
self.root.bind("<b>", lambda event: self.set_mode("brush"))
self.root.bind("<e>", lambda event: self.set_mode("erase"))
def set_mode(self, mode):
self.mode = mode
if mode == "erase":
self.last_brush_color = self.brush_color
self.brush_color = "white"
self.mode_label.config(text="Mode: Erase")
else:
self.brush_color = self.last_brush_color
self.mode_label.config(text="Mode: Brush")
def start_drawing(self, event):
self.drawing = True
self.last_x, self.last_y = event.x, event.y
self.current_stroke = [(self.last_x, self.last_y, self.brush_color, self.brush_size)]
def draw_line(self, event):
if self.drawing:
x, y = event.x, event.y
self.canvas.create_line(self.last_x, self.last_y, x, y, fill=self.brush_color, width=self.brush_size, capstyle=tk.ROUND, smooth=tk.TRUE, splinesteps=36)
self.draw.line([self.last_x, self.last_y, x, y], fill=self.brush_color, width=self.brush_size)
self.last_x, self.last_y = x, y
self.current_stroke.append((x, y, self.brush_color, self.brush_size))
def stop_drawing(self, event):
self.drawing = False
self.history.append(self.current_stroke)
self.current_stroke = []
self.redo_history = []
def save_image(self):
file_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")])
if file_path:
self.image.save(file_path)
def clear_canvas(self):
self.canvas.delete("all")
self.image = Image.new("RGB", (800, 600), "white")
self.draw = ImageDraw.Draw(self.image)
self.history = []
self.redo_history = []
def update_brush_size(self, value):
self.brush_size = int(value)
def choose_color(self):
color = colorchooser.askcolor(title="Choose color")[1]
if color:
self.brush_color = color
self.last_brush_color = color
if self.mode == "erase":
self.set_mode("brush")
def undo(self):
if self.history:
last_stroke = self.history.pop()
self.redo_history.append(last_stroke)
self.redraw_canvas()
def redo(self):
if self.redo_history:
next_stroke = self.redo_history.pop()
self.history.append(next_stroke)
self.redraw_canvas()
def redraw_canvas(self):
self.canvas.delete("all")
self.image = Image.new("RGB", (800, 600), "white")
self.draw = ImageDraw.Draw(self.image)
for stroke in self.history:
for i in range(1, len(stroke)):
x1, y1, color, size = stroke[i-1]
x2, y2, _, _ = stroke[i]
self.canvas.create_line(x1, y1, x2, y2, fill=color, width=size, capstyle=tk.ROUND, smooth=tk.TRUE, splinesteps=36)
self.draw.line([x1, y1, x2, y2], fill=color, width=size)
def Popup(self,size):
file_path = filedialog.askopenfilename(defaultextension=".png", filetypes=[("PNG files", "*.png"), ("JPEG files", "*.jpg"), ("All files", "*.*")])
size = size.split('x')
x_size = int(size[0])
y_size = int(size[1])
if file_path:
img = Image.open(file_path)
img = img.resize((x_size, y_size), Image.ANTIALIAS)
tk_img = ImageTk.PhotoImage(img)
self.canvas.create_image(0, 0, anchor=tk.NW, image=tk_img)
self.canvas.image = tk_img
if __name__ == "__main__":
root = tk.Tk()
app = PaintApp(root)
root.mainloop()