-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresize_images_2.0.py
More file actions
183 lines (135 loc) · 5.23 KB
/
resize_images_2.0.py
File metadata and controls
183 lines (135 loc) · 5.23 KB
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
from tkinter import *
from tkinter import filedialog
from tkinter import ttk
from PIL import Image
import glob
import os
# is called from resize button
def Resize(path):
print("Got to resize")
FILTER = Filter.get()
DIM_X = Dim_X.get()
DIM_Y = Dim_Y.get()
SIZE = [int(DIM_X), int(DIM_Y)]
DESTINATION_DIR = Destination_Path.get()
Resize_Type = Mode.get()
print('FILTER = ', FILTER, 'SIZE = ', SIZE, 'DESTINATION = ', DESTINATION_DIR)
# Uses resize, which stretches the picture to fit the new dimensions
if Resize_Type == "Stretch":
Image_Orig = Image.open(path)
Image_Mod = Image_Orig.resize(SIZE, getattr(Image, FILTER))
Filename = os.path.basename(path)
Save_Path = os.path.join(DESTINATION_DIR, Filename)
Image_Mod.save(Save_Path)
# Uses fit, which fits the image and maintains its aspect ratio, cropping the middle
# This can be set to crop from other locations, the middle works for what I need it for
if Resize_Type == "Crop":
Image_Orig = Image.open(path)
Image_Mod = ImageOps.fit(ImageOrig, SIZE, getattr(Image, FILTER))
Filename = os.path.basename(path)
Save_Path = os.path.join(DESTINATION_DIR, Filename)
Image_Mod.save(Save_Path)
# Opens a file browser to pick the source dir
def SourceButtonDef():
global Source_Path
Filename = filedialog.askdirectory()
Source_Path.set(Filename)
print(Source_Path.get())
# Opens a file browser to pick the destination dir
def DestinationPathDef():
global Destination_Path
Filename = filedialog.askdirectory()
Destination_Path.set(Filename)
print(Destination_Path.get())
# Closes the window and quits the program
def CloseWindow():
Window.destroy()
exit()
count = 0
def Popup():
global Progress_Bar
SOURCE_DIR = Source_Path.get()
Progress_Window = Toplevel()
Progress_Window.title("Resizing...")
Progress_Bar = ttk.Progressbar(Progress_Window, orient="horizontal", length=300, mode="determinate")
Progress_Bar.grid(row=0, column=0)
TOTAL_FILES = len(glob.glob(SOURCE_DIR + '/*.*'))
Progress_Bar['maximum'] = TOTAL_FILES
Button(Progress_Window, text="+1", width=14, command=test).grid(row=1, column=0)
# Function to update the progress bar
def Progress_Update(Current_Value):
global Progress_Bar
Progress_Bar['value'] = Current_Value
def test():
global count
count = count + 1
Progress_Update(count)
# Initializes the resizing part
def Initialize():
global count
Popup()
SOURCE_DIR = Source_Path.get()
TOTAL_FILES = len(glob.glob(SOURCE_DIR + '/*.*'))
print(TOTAL_FILES, ' ', glob.iglob(SOURCE_DIR + '/*.*'), ' ', SOURCE_DIR)
for filepath in glob.iglob(SOURCE_DIR + '/*.*'):
print(filepath)
count = count + 1
Progress_Update(count)
Resize(filepath)
# Initializes the window and gives it a title
Window = Tk()
Window.title("Image Resizer 2.0")
#Variable setup for TKinter
Source_Path = StringVar()
Destination_Path = StringVar()
Dim_X = IntVar()
Dim_Y = IntVar()
Mode = StringVar()
Filter = StringVar()
# Source Row -- Row 0
Label(Window, text="Source:").grid(row=0, column=0)
Source = Label(Window,textvariable=Source_Path, width=30, bg="White")
Source.grid(row=0, column=1)
Button(Window, text="Browse", width=6, command=SourceButtonDef).grid(row=0, column=2)
# Destination Row -- Row 1
Label(Window, text="Destination").grid(row=1, column=0)
Destination = Label(Window, textvariable=Destination_Path, width=30, bg="White")
Destination.grid(row=1, column=1)
Button(Window, text="Browse", width=6, command=DestinationPathDef).grid(row=1, column=2)
# Frame_Frame -- Encapsulates XYFrame, Mode_Frame, Resampling Frame -- Row 2
Frame_Frame = Frame(Window)
Frame_Frame.grid(row=2, column=0, columnspan=3, sticky=W)
# XYFrame
XY_Frame = Frame(Frame_Frame)
XY_Frame.grid(row=0, column=0, columnspan=3)
# Mode_Frame
Mode_Frame = Frame(Frame_Frame)
Mode_Frame.grid(row=1, column=1, columnspan=3)
# Resampling_Frame
Resampling_Frame = Frame(Frame_Frame)
Resampling_Frame.grid(row=2, column=2, columnspan=3)
# Close_Frame Row -- Row 4
Close_Frame = Frame(Window)
Close_Frame.grid(row=4, column=0, columnspan=3)
# X Entry
Label(XY_Frame, text="X").grid(row=0, column=0)
Dim_X = Entry(XY_Frame, width=4)
Dim_X.grid(row=0, column=1)
# Y Entry
Label(XY_Frame, text="Y").grid(row=0, column=2)
Dim_Y = Entry(XY_Frame, width=4)
Dim_Y.grid(row=0, column=3)
# Mode Selection Radiobutton
Crop = Radiobutton(Mode_Frame, text="Crop", variable=Mode, value="Crop")
Crop.grid(row=0, column=0)
Stretch = Radiobutton(Mode_Frame, text="Stretch", variable=Mode, value="Stretch")
Stretch.grid(row=0, column=1)
# Resampling Filter Dropdown
Dropdown = ttk.Combobox(Resampling_Frame, textvariable=Filter)
Dropdown.grid(row=0, column=0)
Dropdown['values'] = ('NEAREST', 'BILINEAR', 'BICUBIC', 'LANCZOS')
# Quit and Resize Buttons
Button(Close_Frame, text="Quit", width=14, command=CloseWindow).grid(row=0, column=0)
Button(Close_Frame, text="Resize", width=14, command=Initialize).grid(row=0, column=1)
# Runs the Window
Window.mainloop()