-
Notifications
You must be signed in to change notification settings - Fork 0
/
gui.py
148 lines (118 loc) · 4.78 KB
/
gui.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
from tkinter import *
from tkinter.ttk import *
from tkinter import filedialog
from PIL import Image
from PIL import ImageTk
import cv2
from app import App
import numpy as np
class Gui:
def __init__(self):
self.windowWidth = 1280
self.windowHeight = 820
self.root = Tk()
self.root.title("DIP app")
# windowSize = str(self.windowWidth) + "x" + str(self.windowHeight)
windowSize = str("200x300")
self.root.geometry(windowSize)
self.root.eval('tk::PlaceWindow . center')
# App storing and data processing
self.data = App()
# Buttons
self.btn1 = None # Select Image
self.btn2 = None # Apply Filter
self.btn3 = None # Add Noise
self.btn4 = None # Show First Image
self.otherButtonFrame = None
# Canvas for image display
self.canvas = None
# Run
self.run()
def selectImage(self): # Image --> openCV style
path = filedialog.askopenfilename()
if len(path) > 0:
image = cv2.imread(path, 0)
self.data.addImage(image)
self.btn1.destroy()
self.otherButtons()
self.displayImage(self.data.initialImage)
def selectImageButton(self):
self.btn1 = Button(self.root, text='Select Image', command=self.selectImage)
self.btn1.place(relx=0.5, rely=0.5, anchor=CENTER)
def addNoise(self):
top = Toplevel(self.root)
top.geometry("300x65")
top.title("Noise")
# Position pop up
root_x = self.root.winfo_rootx()
root_y = self.root.winfo_rooty()
win_x = root_x + 300
win_y = root_y + 100
top.geometry(f'+{win_x}+{win_y}')
# padOrSame = IntVar() # 0 --> pad 1 --> same
# padOrSame.set(0)
# pad1 = Radiobutton(top, text="Original size", variable=padOrSame, value=1, command=None)
# pad0 = Radiobutton(top, text="Padded size", variable=padOrSame, value=0, command=None)
# pad0.grid(row=0, column=1, sticky=W, pady=4)
# pad1.grid(row=1, column=1, sticky=W, pady=4)
def gauss():
self.data.myImNoise(self.data.processedImage, "gaussian")
self.displayImage(self.data.processedImage)
def salt():
self.data.myImNoise(self.data.processedImage, "salt")
self.displayImage(self.data.processedImage)
gauss = Button(top, text='Gaussian', command=gauss)
salt = Button(top, text='Salt', command=salt)
gauss.grid(row=0, column=0, sticky=W, pady=4)
salt.grid(row=1, column=0, sticky=W, pady=4)
def applyFilter(self):
top = Toplevel(self.root)
top.geometry("300x65")
top.title("Filters")
# Position pop up
root_x = self.root.winfo_rootx()
root_y = self.root.winfo_rooty()
win_x = root_x + 300
win_y = root_y + 100
top.geometry(f'+{win_x}+{win_y}')
def mean():
pass
def median():
self.data.myImFilter(self.data.processedImage, "median")
self.displayImage(self.data.processedImage)
mean = Button(top, text='Mean Filter', command=mean)
median = Button(top, text='Median Filter', command=median)
mean.grid(row=0, column=0, sticky=W, pady=4)
median.grid(row=1, column=0, sticky=W, pady=4)
def otherButtons(self):
self.otherButtonFrame = Frame(self.root)
self.otherButtonFrame.pack(side=BOTTOM)
self.btn2 = Button(self.otherButtonFrame, text='Apply Filter', command=self.applyFilter)
self.btn3 = Button(self.otherButtonFrame, text='Add Noise', command=self.addNoise)
self.btn4 = Button(self.otherButtonFrame, text='Initial', command=self.displayInitial)
self.btn2.grid(row=0, column=0, sticky=W, pady=2)
self.btn3.grid(row=0, column=1, sticky=W, pady=2)
self.btn4.grid(row=0, column=2, sticky=W, pady=2)
def displayInitial(self):
self.displayImage(self.data.initialImage)
def displayImage(self, img):
image = img
height = len(image)
width = len(image[0])
a = height / 700
width = int(width / a)
height = 700
# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = Image.fromarray((image % 256).astype(np.uint8))
image = image.resize((width, height), Image.ANTIALIAS)
image = ImageTk.PhotoImage(image)
sz = str(width) + "x" + str(self.windowHeight)
self.root.geometry(sz)
self.canvas = Canvas(self.root, width=width, height=height)
self.canvas.create_image(10, 10, anchor=NW, image=image)
self.canvas.place(relx=0.5, rely=0.5, anchor=CENTER)
self.canvas.mainloop()
def run(self):
self.selectImageButton()
self.root.mainloop()
gui = Gui()