-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
175 lines (113 loc) · 3.7 KB
/
Copy pathapp.py
File metadata and controls
175 lines (113 loc) · 3.7 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
"""
app.py
Responsive UI version of SVD Image Compression Tool
"""
import streamlit as st
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from io import BytesIO
from svd_utils import (
compress_image,
calculate_compression_ratio,
reconstruction_error
)
# ---------------- Page Config ----------------
st.set_page_config(
page_title="SVD Image Compression Tool",
page_icon="📊",
layout="wide"
)
# ---------------- Title Section ----------------
st.title("SVD Image Compression Tool")
st.caption(
"Interactive visualization of low-rank image approximation using Singular Value Decomposition (SVD)"
)
# ---------------- Upload Section ----------------
uploaded_file = st.file_uploader(
"Upload an image",
type=["jpg", "jpeg", "png"]
)
if uploaded_file is not None:
image = Image.open(uploaded_file).convert("RGB")
# Resize large images for performance
MAX_SIZE = 512
if max(image.size) > MAX_SIZE:
image.thumbnail((MAX_SIZE, MAX_SIZE))
image_array = np.array(image)
# ---------------- Rank Selection ----------------
st.markdown("### Compression Control")
max_rank = min(image_array.shape[0], image_array.shape[1])
slider_max = min(max_rank, 200)
k = st.slider(
"Select compression rank (k)",
min_value=1,
max_value=slider_max,
value=min(50, slider_max)
)
# ---------------- Compression ----------------
with st.spinner("Compressing image using SVD..."):
compressed_image = compress_image(image_array, k)
# ---------------- Image Comparison ----------------
st.markdown("### Image Comparison")
col1, col2 = st.columns(2)
with col1:
st.image(
image_array,
caption="Original Image",
use_container_width=True
)
with col2:
st.image(
compressed_image,
caption=f"Compressed Image (k = {k})",
use_container_width=True
)
# ---------------- Metrics ----------------
st.markdown("### Compression Metrics")
ratio = calculate_compression_ratio(image_array.shape, k)
error = reconstruction_error(image_array, compressed_image)
m1, m2 = st.columns(2)
m1.metric(
label="Compression Ratio",
value=f"{ratio}% saved"
)
m2.metric(
label="Reconstruction Error",
value=f"{error}"
)
# ---------------- Graph Section ----------------
st.markdown("### Rank vs Compression Analysis")
ranks = list(range(5, slider_max + 1, 10))
compression_ratios = []
reconstruction_errors = []
for r in ranks:
temp = compress_image(image_array, r)
compression_ratios.append(
calculate_compression_ratio(image_array.shape, r)
)
reconstruction_errors.append(
reconstruction_error(image_array, temp)
)
fig, ax = plt.subplots(1, 2)
fig.set_size_inches(10, 4)
ax[0].plot(ranks, compression_ratios)
ax[0].set_title("Compression Ratio vs Rank")
ax[0].set_xlabel("Rank (k)")
ax[0].set_ylabel("Compression Ratio (%)")
ax[1].plot(ranks, reconstruction_errors)
ax[1].set_title("Reconstruction Error vs Rank")
ax[1].set_xlabel("Rank (k)")
ax[1].set_ylabel("Error")
st.pyplot(fig, use_container_width=True)
# ---------------- Download Section ----------------
st.markdown("### Download Result")
compressed_pil = Image.fromarray(compressed_image)
buffer = BytesIO()
compressed_pil.save(buffer, format="PNG")
st.download_button(
label="Download Compressed Image",
data=buffer.getvalue(),
file_name="compressed_image.png",
mime="image/png"
)