-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
130 lines (110 loc) · 5.51 KB
/
main.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
from tkinter import Tk, Label, Entry, Button, StringVar, W, E, filedialog, messagebox, ttk
import imageManipulation
from PIL import Image
import imghdr
class Gui:
def __init__(self, master):
self.master = master
master.title("Image Resizer")
self.filename = "..."
self.xOrig = "X"
self.yOrig = "Y"
self.filter = "Nearest Neighbour"
self.filename_label_text = StringVar()
self.filename_label_text.set(self.filename)
self.filename_label = Label(master, textvariable=self.filename_label_text)
self.title_filename_label = Label(master, text="Filename:")
self.title_original = Label(master, text="Original:")
self.title_resize = Label(master, text="Resize:")
self.label_x = Label(master, text="Width:")
self.label_y = Label(master, text="Height:")
self.label_x_orig_text = StringVar()
self.label_x_orig_text.set(self.xOrig)
self.label_x_orig = Label(master, textvariable=self.label_x_orig_text)
self.label_y_orig_text = StringVar()
self.label_y_orig_text.set(self.yOrig)
self.label_y_orig = Label(master, textvariable=self.label_y_orig_text)
vc = (master.register(self.validate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.contentX = StringVar()
self.entryX = Entry(master, text="Width: ", validate='key', validatecommand=vc, textvariable=self.contentX)
self.contentY = StringVar()
self.entryY = Entry(master, text="Height: ", validate='key', validatecommand=vc, textvariable=self.contentY)
self.combobox_value = StringVar()
self.combobox = ttk.Combobox(master, textvariable=self.combobox_value)
self.combobox['values'] = ('Nearest Neighbour', 'Bilinear', 'Bicubic', 'Anti-Alias')
self.combobox.current(0)
self.browse_button = Button(master, text="Browse...", command=lambda: self.update("browse"))
self.generate_button = Button(master, text="Generate", command=lambda: self.update("generate"))
self.reset_button = Button(master, text="Reset", command=lambda: self.update("reset"))
# Layout
self.title_filename_label.grid(row=0, column=0, sticky=W)
self.browse_button.grid(row=0, column=4, sticky=W+E)
self.filename_label.grid(row=0, column=1, columnspan=3, sticky=W)
self.title_original.grid(row=1, column=1, sticky=W+E)
self.title_resize.grid(row=1, column=4, sticky=W+E)
self.label_x.grid(row=2, column=0, sticky=W)
self.label_y.grid(row=3, column=0, sticky=W)
self.label_x_orig.grid(row=2, column=1, sticky=W+E)
self.label_y_orig.grid(row=3, column=1, sticky=W+E)
self.entryX.grid(row=2, column=4, sticky=W+E)
self.entryY.grid(row=3, column=4, sticky=W+E)
self.combobox.grid(row=4, column=0, columnspan=5, sticky=W+E)
self.generate_button.grid(row=5, column=0, columnspan=4, sticky=W+E)
self.reset_button.grid(row=5, column=4, sticky=W+E)
self.im = Image
def update(self, method):
if method == "browse":
self.clear()
root = Tk()
myFormats = [('Portable Network Graphics (.png)', '*.png'),
('JPEG / JFIF (.jpg)', '*.jpg'),
('Windows Bitmap (.bmp)', '*.bmp'),
('CompuServer GIF (.gif)', '*.gif')]
file = filedialog.askopenfile(parent=root, mode='rb', defaultextension=".png", filetypes=myFormats, title='Choose a file')
if file is not None:
if imghdr.what(file) is None:
messagebox.showinfo("Error", "Non-image file selected.")
else:
self.filename = file.name
self.filename_label_text.set(self.filename)
# Populate original width and height fields
self.im = Image.open(self.filename)
origWidth, origHeight = self.im.size
self.xOrig = origWidth
self.label_x_orig_text.set(self.xOrig)
self.yOrig = origHeight
self.label_y_orig_text.set(self.yOrig)
self.contentX.set(origWidth)
self.contentY.set(origHeight)
root.withdraw()
elif method == "generate":
if not (self.contentX.get() == "") or (self.contentY.get() == ""):
self.filter = self.combobox.get()
imageManipulation.resizeImage(self.im, self.filter, int(self.contentX.get()), int(self.contentY.get()))
else: # reset
self.clear()
def clear(self):
self.filename = "..."
self.filename_label_text.set(self.filename)
self.xOrig = "X"
self.label_x_orig_text.set(self.xOrig)
self.yOrig = "Y"
self.label_y_orig_text.set(self.yOrig)
self.contentX.set("")
self.contentY.set("")
self.combobox.current(0)
def validate(self, action, index, value_if_allowed,
prior_value, text, validation_type, trigger_type, widget_name):
if text in '0123456789':
if len(value_if_allowed) != 0:
try:
int(value_if_allowed)
return True
except ValueError:
return False
else:
return False
root = Tk()
my_gui = Gui(root)
root.mainloop()