Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,44 @@
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
-
from skimage import
|
4 |
from PIL import Image
|
5 |
-
from skimage.transform import resize
|
6 |
|
7 |
-
def
|
8 |
-
"""
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
"""Compress the image using SVD by keeping only the top k singular values."""
|
13 |
-
U, S, Vt = np.linalg.svd(image, full_matrices=False)
|
14 |
-
compressed_image = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
|
15 |
-
return compressed_image
|
16 |
|
17 |
def process_image(image, k):
|
18 |
-
"""Process the uploaded image, compress it using SVD, and return the result."""
|
19 |
# Convert PIL Image to NumPy array
|
20 |
image_np = np.array(image)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
-
#
|
32 |
-
|
|
|
33 |
|
34 |
return compressed_image_pil
|
35 |
|
36 |
# Gradio interface
|
37 |
gr.Interface(fn=process_image,
|
38 |
-
inputs=[gr.Image(type="pil"),
|
39 |
gr.Slider(1, 100, step=1, value=50, label="Compression Rank")],
|
40 |
-
outputs=gr.Image(type="pil"),
|
41 |
-
title="
|
42 |
-
description="Upload an image and adjust the compression rank to see the compressed version."
|
43 |
).launch()
|
|
|
1 |
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
from skimage import io
|
4 |
from PIL import Image
|
|
|
5 |
|
6 |
+
def svd_compress(image_channel, k):
|
7 |
+
"""Compress a single channel of the image using SVD by keeping only the top k singular values."""
|
8 |
+
U, S, Vt = np.linalg.svd(image_channel, full_matrices=False)
|
9 |
+
compressed_channel = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
|
10 |
+
return compressed_channel
|
|
|
|
|
|
|
|
|
11 |
|
12 |
def process_image(image, k):
|
13 |
+
"""Process the uploaded image, compress it using SVD for each color channel, and return the result."""
|
14 |
# Convert PIL Image to NumPy array
|
15 |
image_np = np.array(image)
|
16 |
|
17 |
+
# Separate the RGB channels
|
18 |
+
if len(image_np.shape) == 3: # Color image
|
19 |
+
r_channel, g_channel, b_channel = image_np[:, :, 0], image_np[:, :, 1], image_np[:, :, 2]
|
20 |
+
|
21 |
+
# Compress each channel using SVD
|
22 |
+
r_compressed = svd_compress(r_channel, k)
|
23 |
+
g_compressed = svd_compress(g_channel, k)
|
24 |
+
b_compressed = svd_compress(b_channel, k)
|
25 |
+
|
26 |
+
# Stack the compressed channels back together
|
27 |
+
compressed_image = np.stack([r_compressed, g_compressed, b_compressed], axis=2)
|
28 |
+
else: # Grayscale image
|
29 |
+
compressed_image = svd_compress(image_np, k)
|
30 |
|
31 |
+
# Clip the values to ensure valid pixel range and convert to PIL Image for output
|
32 |
+
compressed_image = np.clip(compressed_image, 0, 255)
|
33 |
+
compressed_image_pil = Image.fromarray(compressed_image.astype(np.uint8))
|
34 |
|
35 |
return compressed_image_pil
|
36 |
|
37 |
# Gradio interface
|
38 |
gr.Interface(fn=process_image,
|
39 |
+
inputs=[gr.Image(type="pil", label="Upload Image"),
|
40 |
gr.Slider(1, 100, step=1, value=50, label="Compression Rank")],
|
41 |
+
outputs=gr.Image(type="pil", label="Compressed Image"),
|
42 |
+
title="Color Image Compression using SVD",
|
43 |
+
description="Upload an image (color or grayscale) and adjust the compression rank to see the compressed version."
|
44 |
).launch()
|