-
Notifications
You must be signed in to change notification settings - Fork 0
/
DIP.py
239 lines (207 loc) · 8.87 KB
/
DIP.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from tkinter.filedialog import askopenfilename,asksaveasfilename
from PIL import Image, ImageTk, ImageFilter, ImageEnhance, ImageOps
import os
import function
from matplotlib.figure import Figure
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg ,NavigationToolbar2Tk
def GrayImage():
global done,last_label
done = function.GrayImage(image_path)
last_label ="Gray Image"
var2.set(last_label)
SecondImage()
def add():
global done,last_label
Input = int(entry2.get())
done = function.add(image_path,Input)
last_label ="Add {}".format(Input)
var2.set(last_label)
SecondImage()
def contrast():
global done,last_label
done = function.contrast(image_path)
last_label ="Contrast Stretching"
var2.set(last_label)
SecondImage()
def GSApproch1():
global done,last_label
GS_from = int(entry4.get())
GS_to = int(entry5.get())
done = function.GSApproch1(image_path,GS_from,GS_to)
last_label ="GrayScale Approch1"
var2.set(last_label)
SecondImage()
def GSApproch2():
global done,last_label
GS_from = int(entry4.get())
GS_to = int(entry5.get())
done = function.GSApproch2(image_path,GS_from,GS_to)
last_label ="GrayScale Approch2"
var2.set(last_label)
SecondImage()
def GrayScaling():
global done,last_label
scale = int(scale_n.get())
done = function.GrayScaling(image_path,scale)
last_label ="Gray Scale {}".format(scale)
var2.set(last_label)
SecondImage()
def Negative():
global done,last_label
done = function.Negative(image_path)
last_label ="Negative Image"
var2.set(last_label)
SecondImage()
def log():
global done,last_label
done = function.log(image_path)
last_label ="Log Image"
var2.set(last_label)
SecondImage()
def medianFilter():
global done,last_label
done = function.medianFilter(image_path)
last_label ="Median Filter"
var2.set(last_label)
SecondImage()
def power():
global done,last_label
gamma = float(entry3.get())
done = function.power(image_path,gamma)
last_label ="Power {} Image".format(gamma)
var2.set(last_label)
SecondImage()
def Avarage():
global done,last_label
done = function.Avarage(image_path)
last_label ="Avarage"
var2.set(last_label)
SecondImage()
def sharp():
global done,last_label
done = function.sharp(image_path)
last_label ="Sharping"
var2.set(last_label)
SecondImage()
def clearImage1():
canvas2.delete("all")
var1.set("Origional Image")
def clearImage2():
canvas3.delete("all")
var2.set("Second Image")
def selected():
global image_path, image, orignal_size, entry,origional_length
clearImage1()
image_path = filedialog.askopenfilename(initialdir=os.getcwd())
image = Image.open(image_path)
origional_length = len(image_path)
image.thumbnail((380, 380))
image1 = ImageTk.PhotoImage(image)
orignal_size = (image1.width(), image1.height())
canvas2.create_image(200, 200, image=image1)
canvas2.image=image1
Directory.set(image_path)
var1.set("Origional Image")
clearImage2()
def SecondImage():
global image_path, done,resized,orignal_size,img
resized = cv.resize(done, orignal_size)
img = ImageTk.PhotoImage(Image.fromarray(resized))
canvas3.create_image(200, 200, image=img)
canvas3.image=img
def reuse():
global image_path, done,resized,orignal_size,img,last_label
resized = cv.resize(done, orignal_size)
img = ImageTk.PhotoImage(Image.fromarray(resized))
canvas2.create_image(200, 200, image=img)
canvas2.image=img
var1.set(last_label)
imgg = Image.fromarray(done)
file_name, file_ext = os.path.splitext(image_path)
file_name = file_name + "({})".format(last_label)
new_image_path = r'{}.{}'.format(file_name,file_ext)
imgg.save(new_image_path)
currant_length = len(image_path)
new_length = len(new_image_path)
if (origional_length < currant_length and currant_length<new_length):
os.remove(image_path)
image_path = new_image_path
clearImage2()
def save():
imgg = Image.fromarray(done)
ext = image_path.split(".")[-1]
file=asksaveasfilename(defaultextension =f".{ext}",filetypes=[("All Files","*.*"),("PNG file","*.png"),("jpg file","*.jpg")])
if file:
imgg.save(file)
ws = Tk()
ws.title("Image processing")
ws.geometry("840x760")
ws.configure(bg='#85929E')
# ws.iconbitmap('python.png')
button1 = Button(ws, text="Select Image", bg='#52BE80', fg='black', font=('ariel 15 bold'),cursor="hand2" ,relief=GROOVE, command=selected)
button1.place(x=60, y=705)
button2 = Button(ws, text="Save", width=12, bg='#D6EAF8', fg='black', font=('ariel 15 bold'),cursor="hand2", relief=GROOVE, command=save)
button2.place(x=440, y=705)
button3 = Button(ws, text="Exit", width=12, bg='#CD6155', fg='black', font=('ariel 15 bold'),cursor="hand2", relief=GROOVE, command=ws.destroy)
button3.place(x=660, y=705)
button4 = Button(ws, text="Reuse", width=12, bg='#afafaf', fg='black', font=('ariel 15 bold'),cursor="hand2", relief=GROOVE, command=reuse)
button4.place(x=250, y=705)
Button( ws,text='Gray Scaling',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= GrayScaling).place(x=30, y=7)
Button( ws,text='ContrastStretch',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= contrast).place(x=30, y=60)
Button( ws,text='Negative Image',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= Negative).place(x=30, y=113)
Button( ws,text='Log Image',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= log).place(x=225, y=7)
Button( ws,text='power low',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= power).place(x=225, y=60)
Button( ws,text='Median Filter',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= medianFilter).place(x=225, y=113)
Button( ws,text='GS Approch1',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= GSApproch1).place(x=420, y=7)
Button( ws,text='GS Approch2',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= GSApproch2).place(x=420, y=60)
Button( ws,text='Avarage',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= Avarage).place(x=420, y=113)
Button( ws,text='Adding',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= add).place(x=615, y=7)
Button( ws,text='Gray Image',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= GrayImage).place(x=615, y=60)
Button( ws,text='Sharping',width=12,font=("ariel 17 bold"),bg='#3498DB',cursor="hand2",command= sharp).place(x=615, y=113)
canvas2 = Canvas(ws, width="400", height="400", relief=RIDGE, bd=2)
canvas2.place(x=10, y=210)
canvas3 = Canvas(ws, width="400", height="400", relief=RIDGE, bd=2)
canvas3.place(x=420, y=210)
var1 = StringVar()
label1 = Label( ws, textvariable=var1, relief=RAISED,font=("ariel 17 bold") ,bg="#17202A",fg="#F0F3F4")
var1.set("Origional Image")
label1.place(x=100, y=170)
var2 = StringVar()
label2 = Label( ws, textvariable=var2, relief=RAISED,font=("ariel 17 bold"),bg="#17202A",fg="#F0F3F4" )
var2.set("Second Image")
label2.place(x=540, y=170)
Directory = StringVar()
Label(ws,text='Image path:',font="ariel 15 bold",bg="#17202A",fg="#F0F3F4").place(x=58,y=664)
entry = Entry(ws, font="ariel 15",textvariable=Directory,width=50).place(x=180,y=664)
values = [1, 2, 3, 4, 5, 6, 7, 8]
scale_n = ttk.Combobox(ws, values=values, font=('ariel 15 bold'),width=2)
scale_n.insert('0', values[0])
scale_n.place(x=50, y=627)
Label(ws, text="K:", font=("ariel 15 bold"),bg="#17202A",fg="#F0F3F4").place(x=20, y=627)
values2 = [10, 30, 50, 80 , 100]
Label(ws,text='Add:',font="ariel 15 bold",bg="#17202A",fg="#F0F3F4").place(x=125,y=627)
entry2 = ttk.Combobox(ws, font="ariel 15",values=values2,width=4)
entry2.place(x=180,y=627)
entry2.insert('0', values2[0])
values3 = [0.1, 0.3, 0.5, 0.8, 1.2, 1.5, 2]
Label(ws,text='Gamma:',font="ariel 15 bold",bg="#17202A",fg="#F0F3F4").place(x=275,y=627)
entry3 = ttk.Combobox(ws, font="ariel 15",values=values3,width=3)
entry3.place(x=365,y=627)
entry3.insert('0', values3[0])
values4 = [10, 30, 50, 80 , 100]
Label(ws,text='GS from:',font="ariel 15 bold",bg="#17202A",fg="#F0F3F4").place(x=460,y=627)
entry4 = ttk.Combobox(ws, font="ariel 15",values=values4,width=3)
entry4.place(x=555,y=627)
entry4.insert('0', values4[4])
values5 = [150, 180, 200, 220, 255]
Label(ws,text='GS To:',font="ariel 15 bold",bg="#17202A",fg="#F0F3F4").place(x=653,y=627)
entry5 = ttk.Combobox(ws, font="ariel 15",values=values5,width=3)
entry5.place(x=730,y=627)
entry5.insert('0', values5[2])
ws.mainloop()