Skip to content

Commit 1d46320

Browse files
author
dn0z
committed
Made changes to the progress (we still need to add multithreading)
1 parent 7fe5700 commit 1d46320

File tree

2 files changed

+107
-43
lines changed

2 files changed

+107
-43
lines changed

bir.py

Lines changed: 69 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,49 @@ def confirm_settings(self):
6262

6363
return messagebox.askyesno("Export confirmation", confirm_msg)
6464

65+
def increase_progress_bar_value(self):
66+
"""
67+
Increase the progress bar value by one
68+
"""
69+
70+
self.progress_bar["value"] += 1
71+
print("Increased progress bar value by one")
72+
73+
def clear_progress_window(self):
74+
"""
75+
Clear the progress window
76+
"""
77+
78+
if self.progress_window is not None:
79+
self.progress_window.destroy()
80+
81+
def display_progress_window(self, progress_maximum=100):
82+
"""
83+
Display the progress window
84+
85+
:param progress_maximum: The maximum value of the progress bar (default is 100)
86+
"""
87+
88+
self.clear_progress_window()
89+
90+
self.progress_window = Toplevel(self)
91+
self.progress_window.title("Exporting")
92+
self.progress_window.geometry("300x100")
93+
94+
Label(self.progress_window,
95+
text="Exporting images",
96+
font=("Segoe UI", 16)).pack(fill="x", side="top")
97+
98+
self.progress_bar = ttk.Progressbar(self.progress_window,
99+
orient="horizontal",
100+
length=280,
101+
mode="determinate")
102+
self.progress_bar.pack(expand=True, fill="both", side="bottom")
103+
self.progress_bar["value"] = 0
104+
self.progress_bar["maximum"] = progress_maximum
105+
106+
self.progress_window.update()
107+
65108
def get_settings_status(self):
66109
"""
67110
Check if our settings are valid (directory exists, width and height are digits etc)
@@ -114,39 +157,31 @@ def export_button_handler(self):
114157
else:
115158
# Valid settings, confirm settings with the user and export
116159
if self.confirm_settings():
117-
# result = imgedit.export_all_in_dir(
118-
# self.selected_directory.get(),
119-
# int(self.export_properties["width"].get()),
120-
# int(self.export_properties["height"].get()),
121-
# self.export_properties["type"].get(),
122-
# self.overwrite_original.get()
123-
# )
124-
125-
# Display progress
126-
if self.progress_window is not None:
127-
self.progress_window.destroy()
128-
129-
self.progress_window = Toplevel(self)
130-
self.progress_window.title("Exporting")
131-
self.progress_window.geometry("300x100")
132-
#self.progress_window.iconbitmap("icon.ico")
133-
134-
Label(self.progress_window,
135-
text="Exporting images",
136-
font=("Segoe UI", 16)).pack(fill="x", side="top")
137-
138-
progress_bar = ttk.Progressbar(self.progress_window,
139-
orient="horizontal",
140-
length=280,
141-
mode="determinate")
142-
progress_bar.pack(expand=True, fill="both", side="bottom")
143-
progress_bar["value"] = 60
144-
progress_bar["maximum"] = 100
145-
146-
# if result:
147-
# # at this point, we are done with our exports, display a success message
148-
# messagebox.showinfo("Exports completed",
149-
# "All images were exported successfully")
160+
num_of_images = imgedit.image_files_in_dir(self.selected_directory.get())
161+
self.display_progress_window(num_of_images)
162+
print("Progress window is on screen")
163+
164+
print("Exporting images")
165+
result = imgedit.export_all_in_dir(
166+
self.selected_directory.get(),
167+
int(self.export_properties["width"].get()),
168+
int(self.export_properties["height"].get()),
169+
self.export_properties["type"].get(),
170+
self.overwrite_original.get(),
171+
self.increase_progress_bar_value
172+
)
173+
174+
self.clear_progress_window()
175+
print("Progress window is cleared")
176+
177+
if result:
178+
# at this point, we are done with our exports, display a success message
179+
messagebox.showinfo("Exports completed",
180+
"All images were exported successfully")
181+
else:
182+
# one or more images failed to export, display a warning
183+
messagebox.showwarning("Exports failed",
184+
"One or more images failed to export")
150185

151186
def browse_for_directory(self):
152187
"""
@@ -253,6 +288,7 @@ def __init__(self, parent=None):
253288
# properties
254289
self.save_as_dropdown = None
255290
self.progress_window = None
291+
self.progress_bar = None
256292

257293
self.selected_directory = StringVar(self)
258294
self.overwrite_original = BooleanVar(self)

imgedit.py

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,40 @@ def export_file(path, name, width, height, export_type, overwrite):
4848

4949
img_path = os.path.join(path, name)
5050

51-
# open the given image and resize it
52-
img = PIL.Image.open(img_path)
53-
img = img.resize((width, height), PIL.Image.ANTIALIAS)
54-
5551
# set the destination image file we want to save
5652
dest_img_name = get_filename_with_type(name, export_type, "_resize")
5753
if overwrite:
5854
dest_img_name = name
5955

60-
# save the resized/converted image
61-
img.save(os.path.join(path, dest_img_name))
56+
try:
57+
# open the given image, resize and save it
58+
img = PIL.Image.open(img_path)
59+
img = img.resize((width, height), PIL.Image.ANTIALIAS)
60+
img.save(os.path.join(path, dest_img_name))
61+
except IOError:
62+
return False
63+
64+
return True
65+
66+
67+
def image_files_in_dir(selected_dir):
68+
"""
69+
Returns the number of image files in the given directory
70+
71+
:param selected_dir: The directory containing the images
72+
:return: the number of image files the directory contains
73+
"""
74+
images = 0
75+
76+
for path, subdirs, files in os.walk(selected_dir):
77+
for name in files:
78+
if is_image(name):
79+
images += 1
80+
81+
return images
6282

6383

64-
def export_all_in_dir(selected_dir, width, height, export_type, overwrite):
84+
def export_all_in_dir(selected_dir, width, height, export_type, overwrite, progress_bar_callback=None):
6585
"""
6686
Export all the images in the selected directory
6787
@@ -77,10 +97,18 @@ def export_all_in_dir(selected_dir, width, height, export_type, overwrite):
7797
:return: `True` if all images were exported successfully or `False` if there was an error
7898
"""
7999

100+
all_exported_successfully = True
101+
80102
for path, subdirs, files in os.walk(selected_dir):
81103
for name in files:
82104
if is_image(name):
83-
# TODO: Add a new window with a progress bar while exporting images
84-
export_file(path, name, width, height, export_type, overwrite)
105+
# export and check if everything went okay
106+
exported_successfully = export_file(path, name, width, height, export_type, overwrite)
107+
if not exported_successfully:
108+
all_exported_successfully = False
85109

86-
return True
110+
# update the progress bar
111+
if progress_bar_callback is not None:
112+
progress_bar_callback()
113+
114+
return all_exported_successfully

0 commit comments

Comments
 (0)