-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimageViewer.py
143 lines (106 loc) · 4.88 KB
/
imageViewer.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
from tkinter import Frame,Canvas,CENTER,ROUND
from tkinter.constants import ANCHOR
from PIL import Image,ImageTk
import cv2
class ImageViewer(Frame):
def __init__(self,master=None):
Frame.__init__(self,master=master,bg='gray',width=600,height=400)
self.shown_image = None
self.x = 0
self.y = 0
self.crop_start_x = 0
self.crop_start_y = 0
self.crop_end_x = 0
self.crop_end_y = 0
self.draw_ids = list()
self.rectangle_id = 0
self.ratio = 0
self.canvas = Canvas(self,bg="gray",width=600,height=400)
self.canvas.place(relx=0.5,rely=0.5,anchor = CENTER)
def show_image(self,img = None):
self.clear_canvas()
if img is None:
image = self.master.processed_image.copy()
else:
image = img
image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
height,width,channel = image.shape
ratio = height/width
new_width = width
new_height = height
if height>self.winfo_height() or width>self.winfo_width():
if ratio<1:
new_width = self.winfo_width()
new_height= int(new_width*ratio)
else:
new_height = self.winfo_height()
new_width= int(new_height*(width/height))
self.shown_image = cv2.resize(image,(new_width,new_height))
self.shown_image = ImageTk.PhotoImage(Image.fromarray(self.shown_image))
self.ratio = height/new_height
self.canvas.config(width=new_width,height=new_height)
self.canvas.create_image(new_width/2,new_height/2,anchor=CENTER,image = self.shown_image)
def activate_draw(self):
self.canvas.bind("<ButtonPress>",self.start_draw)
self.canvas.bind("<B1-Motion>",self.draw)
self.master.is_draw_state = True
def activate_crop(self):
self.canvas.bind("<ButtonPress>", self.start_crop)
self.canvas.bind("<B1-Motion>", self.crop)
self.canvas.bind("<ButtonRelease>", self.end_crop)
self.master.is_crop_state = True
def deactivate_draw(self):
self.canvas.unbind("<ButtonPress>")
self.canvas.unbind("<B1-Motion>")
self.master.is_draw_state = False
def deactivate_crop(self):
self.canvas.unbind("<ButtonPress>")
self.canvas.unbind("<B1-Motion>")
self.canvas.unbind("<ButtonRelease>")
self.master.is_crop_state = False
def start_draw(self,event):
self.x = event.x
self.y = event.y
def draw(self,event):
self.draw_ids.append(self.canvas.create_line(self.x,self.y,event.x,event.y,width=2,fill="red",capstyle=ROUND,smooth=True))
cv2.line(self.master.processed_image, (int(self.x * self.ratio), int(self.y * self.ratio)),(int(event.x * self.ratio), int(event.y * self.ratio)),(0, 0, 255), thickness=int(self.ratio * 2),lineType=8)
self.x = event.x
self.y = event.y
def start_crop(self,event):
self.crop_start_x = event.x
self.crop_start_y = event.y
def crop(self,event):
if self.rectangle_id:
self.canvas.delete(self.rectangle_id)
self.crop_end_x = event.x
self.crop_end_y = event.y
self.rectangle_id = self.canvas.create_rectangle(self.crop_start_x,self.crop_start_y,self.crop_end_x,self.crop_end_y,width=1)
def end_crop(self,event):
if self.crop_start_x <= self.crop_end_x and self.crop_start_y <= self.crop_end_y:
start_x = int(self.crop_start_x * self.ratio)
start_y = int(self.crop_start_y * self.ratio)
end_x = int(self.crop_end_x * self.ratio)
end_y = int(self.crop_end_y * self.ratio)
elif self.crop_start_x > self.crop_end_x and self.crop_start_y<=self.crop_end_y:
start_x = int(self.crop_end_x * self.ratio)
start_y = int(self.crop_start_y * self.ratio)
end_x = int(self.crop_start_x * self.ratio)
end_y = int(self.crop_end_y * self.ratio)
elif self.crop_start_x <= self.crop_end_x and self.crop_start_y > self.crop_end_y:
start_x = int(self.crop_start_x * self.ratio)
start_y = int(self.crop_end_y * self.ratio)
end_x = int(self.crop_end_x * self.ratio)
end_y = int(self.crop_start_y * self.ratio)
else:
start_x = int(self.crop_end_x * self.ratio)
start_y = int(self.crop_end_y * self.ratio)
end_x = int(self.crop_start_x * self.ratio)
end_y = int(self.crop_start_y * self.ratio)
x = slice(start_x,end_x,1)
y = slice(start_y,end_y,1)
self.master.processed_image = self.master.processed_image[y,x]
self.show_image()
def clear_canvas(self):
self.canvas.delete("all")
def clear_draw(self):
self.canvas.delete(self.draw_ids)