Spaces:
Runtime error
Runtime error
Commit
·
771ee9f
1
Parent(s):
8c5f698
Upload bounding.py
Browse files- bounding.py +56 -0
bounding.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import tkinter as tk # this is in python 3.4. For python 2.x import Tkinter
|
2 |
+
from PIL import Image, ImageTk
|
3 |
+
|
4 |
+
|
5 |
+
|
6 |
+
class ExampleApp(tk.Tk):
|
7 |
+
def __init__(self):
|
8 |
+
tk.Tk.__init__(self)
|
9 |
+
self.x = self.y = 0
|
10 |
+
self.canvas = tk.Canvas(self, width=512, height=512, cursor="cross")
|
11 |
+
self.canvas.pack(side="top", fill="both", expand=True)
|
12 |
+
self.canvas.bind("<ButtonPress-1>", self.on_button_press)
|
13 |
+
self.canvas.bind("<B1-Motion>", self.on_move_press)
|
14 |
+
self.canvas.bind("<ButtonRelease-1>", self.on_button_release)
|
15 |
+
|
16 |
+
self.rect = None
|
17 |
+
|
18 |
+
self.start_x = None
|
19 |
+
self.start_y = None
|
20 |
+
|
21 |
+
|
22 |
+
self._draw_image()
|
23 |
+
|
24 |
+
|
25 |
+
def _draw_image(self):
|
26 |
+
self.im = Image.open('./resource/lena.jpg')
|
27 |
+
self.tk_im = ImageTk.PhotoImage(self.im)
|
28 |
+
self.canvas.create_image(0,0,anchor="nw",image=self.tk_im)
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
def on_button_press(self, event):
|
33 |
+
# save mouse drag start position
|
34 |
+
self.start_x = event.x
|
35 |
+
self.start_y = event.y
|
36 |
+
|
37 |
+
# create rectangle if not yet exist
|
38 |
+
#if not self.rect:
|
39 |
+
self.rect = self.canvas.create_rectangle(self.x, self.y, 1, 1, fill="black")
|
40 |
+
|
41 |
+
def on_move_press(self, event):
|
42 |
+
curX, curY = (event.x, event.y)
|
43 |
+
|
44 |
+
# expand rectangle as you drag the mouse
|
45 |
+
self.canvas.coords(self.rect, self.start_x, self.start_y, curX, curY)
|
46 |
+
|
47 |
+
|
48 |
+
|
49 |
+
def on_button_release(self, event):
|
50 |
+
pass
|
51 |
+
|
52 |
+
|
53 |
+
if __name__ == "__main__":
|
54 |
+
app = ExampleApp()
|
55 |
+
app.mainloop()
|
56 |
+
app.mainloop()
|