-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
300 lines (239 loc) · 10.8 KB
/
Copy pathapp.py
File metadata and controls
300 lines (239 loc) · 10.8 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import customtkinter as ctk
from tkinter import filedialog, messagebox
from PIL import Image
import os
import io
from compressor import compress_image
ctk.set_appearance_mode("System")
ctk.set_default_color_theme("blue")
class ImageCompressionApp(ctk.CTk):
def __init__(self):
super().__init__()
self.title("DCT Image Compression")
self.geometry("900x600")
self.image_path = None
self.compressed_image_data = None
# --- Sidebar (Controls) ---
self.sidebar_frame = ctk.CTkFrame(self, width=200, corner_radius=0)
self.sidebar_frame.pack(side="left", fill="y", padx=0, pady=0)
self.logo_label = ctk.CTkLabel(
self.sidebar_frame,
text="DCT Compressor",
font=ctk.CTkFont(size=20, weight="bold"),
)
self.logo_label.pack(pady=20, padx=20)
self.select_btn = ctk.CTkButton(
self.sidebar_frame, text="Select Image (.bmp)", command=self.select_image
)
self.select_btn.pack(pady=10, padx=20)
self.path_label = ctk.CTkLabel(
self.sidebar_frame, text="No image selected", font=ctk.CTkFont(size=10)
)
self.path_label.pack(pady=(0, 10), padx=20)
# F Entry
self.f_label = ctk.CTkLabel(self.sidebar_frame, text="Macro-block size (F):")
self.f_label.pack(pady=(10, 0), padx=20, anchor="w")
self.f_entry = ctk.CTkEntry(self.sidebar_frame, placeholder_text="e.g. 8")
self.f_entry.pack(pady=5, padx=20)
# d Entry
self.d_label = ctk.CTkLabel(
self.sidebar_frame, text="Cutoff threshold (d <= 2F - 2):"
)
self.d_label.pack(pady=(10, 0), padx=20, anchor="w")
self.d_entry = ctk.CTkEntry(self.sidebar_frame, placeholder_text="e.g. 4")
self.d_entry.pack(pady=5, padx=20)
self.process_btn = ctk.CTkButton(
self.sidebar_frame, text="Compress", command=self.process_image
)
self.process_btn.pack(pady=(30, 10), padx=20)
self.save_btn = ctk.CTkButton(
self.sidebar_frame,
text="Save Compressed",
command=self.save_image,
state="disabled",
)
self.save_btn.pack(pady=10, padx=20)
# Zoom Slider
self.zoom_label = ctk.CTkLabel(self.sidebar_frame, text="Zoom scale: 100%")
self.zoom_label.pack(pady=(20, 0), padx=20, anchor="w")
self.zoom_slider = ctk.CTkSlider(
self.sidebar_frame, from_=0.1, to=5.0, command=self.update_zoom
)
self.zoom_slider.set(1.0)
self.zoom_slider.pack(pady=5, padx=20)
# Internal references for image resizing
self.display_orig_img = None
self.display_comp_img = None
# --- Main Area (Images) ---
self.main_frame = ctk.CTkFrame(self)
self.main_frame.pack(side="right", fill="both", expand=True, padx=10, pady=10)
self.main_frame.grid_columnconfigure(0, weight=1)
self.main_frame.grid_columnconfigure(1, weight=1)
self.main_frame.grid_rowconfigure(0, weight=1)
self.main_frame.grid_rowconfigure(1, weight=10)
self.main_frame.grid_rowconfigure(2, weight=1)
# Labels for Images
self.lbl_orig_title = ctk.CTkLabel(
self.main_frame, text="Original Image", font=ctk.CTkFont(weight="bold")
)
self.lbl_orig_title.grid(row=0, column=0, pady=10)
self.lbl_comp_title = ctk.CTkLabel(
self.main_frame, text="Compressed Image", font=ctk.CTkFont(weight="bold")
)
self.lbl_comp_title.grid(row=0, column=1, pady=10)
self.frame_orig_img = ctk.CTkFrame(
self.main_frame, width=350, height=350, fg_color="gray15"
)
self.frame_orig_img.grid(row=1, column=0, padx=10, pady=10)
self.frame_orig_img.pack_propagate(False)
self.frame_orig_img.grid_propagate(False)
self.frame_comp_img = ctk.CTkFrame(
self.main_frame, width=350, height=350, fg_color="gray15"
)
self.frame_comp_img.grid(row=1, column=1, padx=10, pady=10)
self.frame_comp_img.pack_propagate(False)
self.frame_comp_img.grid_propagate(False)
self.lbl_orig_img = ctk.CTkLabel(self.frame_orig_img, text="")
self.lbl_orig_img.place(x=175, y=175, anchor="center")
self.lbl_comp_img = ctk.CTkLabel(self.frame_comp_img, text="")
self.lbl_comp_img.place(x=175, y=175, anchor="center")
# Setup panning variables and bindings
self.pan_x = 175
self.pan_y = 175
self.drag_start_x = 0
self.drag_start_y = 0
self.lbl_orig_img.bind("<ButtonPress-1>", self.on_drag_start)
self.lbl_orig_img.bind("<B1-Motion>", self.on_drag_motion)
self.lbl_comp_img.bind("<ButtonPress-1>", self.on_drag_start)
self.lbl_comp_img.bind("<B1-Motion>", self.on_drag_motion)
self.lbl_orig_size = ctk.CTkLabel(self.main_frame, text="")
self.lbl_orig_size.grid(row=2, column=0, pady=(0, 10))
self.lbl_comp_size = ctk.CTkLabel(self.main_frame, text="")
self.lbl_comp_size.grid(row=2, column=1, pady=(0, 10))
def on_drag_start(self, event):
self.drag_start_x = event.x_root
self.drag_start_y = event.y_root
def on_drag_motion(self, event):
dx = event.x_root - self.drag_start_x
dy = event.y_root - self.drag_start_y
self.drag_start_x = event.x_root
self.drag_start_y = event.y_root
self.pan_x += dx
self.pan_y += dy
self.lbl_orig_img.place(x=self.pan_x, y=self.pan_y, anchor="center")
self.lbl_comp_img.place(x=self.pan_x, y=self.pan_y, anchor="center")
def select_image(self):
filename = filedialog.askopenfilename(
title="Select an image",
filetypes=[("BMP Files", "*.bmp"), ("All Files", "*.*")],
)
if filename:
self.image_path = filename
self.path_label.configure(text=os.path.basename(filename))
img = Image.open(self.image_path)
self.display_orig_img = img.copy()
self.display_comp_img = None
orig_kb = os.path.getsize(self.image_path) / 1024
w, h = img.size
self.lbl_orig_size.configure(
text=f"Resolution: {w}x{h}\nFile Size (su disco): {orig_kb:.2f} KB"
)
self.lbl_comp_size.configure(text="")
self.pan_x = 175
self.pan_y = 175
self.lbl_orig_img.place(x=self.pan_x, y=self.pan_y, anchor="center")
self.lbl_comp_img.place(x=self.pan_x, y=self.pan_y, anchor="center")
self.zoom_slider.set(1.0)
self.update_zoom(1.0)
self.lbl_comp_img.configure(image="", text="Run compression to see result")
self.save_btn.configure(state="disabled")
def process_image(self):
if not self.image_path:
messagebox.showerror("Error", "Please select an image first.")
return
try:
macroblock_size = int(self.f_entry.get())
cutoff_threshold = int(self.d_entry.get())
except ValueError:
messagebox.showerror("Error", "F and d must be integers.")
return
if macroblock_size <= 0:
messagebox.showerror("Error", "F must be greater than 0.")
return
if not (0 <= cutoff_threshold <= 2 * macroblock_size - 2):
messagebox.showerror(
"Error", f"d must be between 0 and {2 * macroblock_size - 2}"
)
return
try:
orig_img, comp_img = compress_image(
self.image_path, macroblock_size, cutoff_threshold
)
# Save raw unmodified image data for potential saving
self.compressed_image_data = comp_img.copy()
self.save_btn.configure(state="normal")
orig_w, orig_h = orig_img.size
comp_w, comp_h = comp_img.size
BMP_HEADER_8BIT = 1078
orig_kb = (orig_w * orig_h + BMP_HEADER_8BIT) / 1024
comp_buffer = io.BytesIO()
self.compressed_image_data.save(comp_buffer, format="PNG", optimize=True)
comp_kb = len(comp_buffer.getvalue()) / 1024
self.lbl_orig_size.configure(
text=f"Resolution: {orig_w}x{orig_h}\nGrayscale BMP: {orig_kb:.2f} KB"
)
self.lbl_comp_size.configure(
text=f"Resolution: {comp_w}x{comp_h}\nEst. DCT2 Size: {comp_kb:.2f} KB"
)
self.display_orig_img = orig_img.copy()
self.display_comp_img = comp_img.copy()
self.update_zoom(self.zoom_slider.get())
except Exception as e:
messagebox.showerror("Processing Error", str(e))
def update_zoom(self, value):
scale = float(value)
self.zoom_label.configure(text=f"Zoom scale: {int(scale * 100)}%")
if self.display_orig_img:
# We resize keeping aspect ratio relative to a baseline size
base_w, base_h = self.display_orig_img.size
ratio = min(350 / base_w, 350 / base_h)
new_size = (int(base_w * ratio * scale), int(base_h * ratio * scale))
if new_size[0] > 0 and new_size[1] > 0:
resized_orig = self.display_orig_img.resize(new_size, Image.LANCZOS)
ctk_orig = ctk.CTkImage(
light_image=resized_orig,
dark_image=resized_orig,
size=resized_orig.size,
)
self.lbl_orig_img.configure(image=ctk_orig, text="")
if self.display_comp_img:
base_w, base_h = self.display_comp_img.size
ratio = min(350 / base_w, 350 / base_h)
new_size = (int(base_w * ratio * scale), int(base_h * ratio * scale))
if new_size[0] > 0 and new_size[1] > 0:
resized_comp = self.display_comp_img.resize(new_size, Image.LANCZOS)
ctk_comp = ctk.CTkImage(
light_image=resized_comp,
dark_image=resized_comp,
size=resized_comp.size,
)
self.lbl_comp_img.configure(image=ctk_comp, text="")
def save_image(self):
if not self.compressed_image_data:
return
filename = filedialog.asksaveasfilename(
defaultextension=".bmp",
title="Save compressed image",
filetypes=[("BMP Files", "*.bmp"), ("All Files", "*.*")],
)
if filename:
try:
self.compressed_image_data.save(filename, format="BMP")
messagebox.showinfo(
"Success", f"Image saved successfully to:\n{filename}"
)
except Exception as e:
messagebox.showerror("Save Error", str(e))
if __name__ == "__main__":
app = ImageCompressionApp()
app.mainloop()