NaimaAqeel commited on
Commit
014d1cd
·
verified ·
1 Parent(s): 3edaa7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -43
app.py CHANGED
@@ -1,51 +1,40 @@
1
  import numpy as np
2
- import gradio as gr
3
  from skimage import io, color
4
- from numpy.linalg import norm
5
- from PIL import Image
 
6
 
7
- def svd_compress_color(image, k):
8
- """Compress the color image using SVD by keeping only the top k singular values for each channel."""
9
- # Initialize an empty array for the compressed image
10
- compressed_image = np.zeros_like(image, dtype=np.float64)
11
-
12
- # Process each color channel separately
13
- for channel in range(3): # Assuming RGB
14
- U, S, Vt = np.linalg.svd(image[:, :, channel], full_matrices=False)
15
- compressed_channel = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
16
- compressed_image[:, :, channel] = compressed_channel
17
-
18
- # Clip values to valid range [0, 255] and convert to uint8
19
- compressed_image = np.clip(compressed_image, 0, 255)
20
- return compressed_image.astype(np.uint8)
21
 
22
- def process_image_color(image, k):
23
- """Process the uploaded image, compress it using SVD for each color channel, and return the result."""
24
- # Convert PIL Image to NumPy array
25
- image_np = np.array(image)
26
-
27
- # Ensure the image has three color channels
28
- if image_np.ndim != 3 or image_np.shape[2] != 3:
29
- raise ValueError("Input image must be a color image with 3 channels (RGB).")
30
-
31
- # Compress the image
32
- compressed_image = svd_compress_color(image_np, k)
 
 
 
 
 
 
 
33
 
34
- # Convert compressed image back to PIL Image for Gradio output
35
- compressed_image_pil = Image.fromarray(compressed_image)
36
 
37
- return compressed_image_pil
 
38
 
39
- # Gradio interface
40
- gr_interface = gr.Interface(
41
- fn=process_image_color,
42
- inputs=[
43
- gr.Image(type="pil", label="Upload Image"),
44
- gr.Slider(1, 100, step=1, value=50, label="Compression Rank")
45
- ],
46
- outputs=gr.Image(type="pil", label="Compressed Image"),
47
- title="Interactive Image Compression using SVD",
48
- description="Upload a color image and adjust the compression rank to see the compressed version while retaining color."
49
- )
50
 
51
- gr_interface.launch()
 
 
1
  import numpy as np
 
2
  from skimage import io, color
3
+ from skimage.transform import resize
4
+ from sklearn.decomposition import TruncatedSVD
5
+ import streamlit as st
6
 
7
+ def resize_image(image, target_shape=(500, 500)):
8
+ """Resize image to reduce processing time."""
9
+ return resize(image, target_shape, anti_aliasing=True)
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ def truncated_svd_compress(image, k):
12
+ """Compress the image using Truncated SVD by keeping only the top k singular values."""
13
+ svd = TruncatedSVD(n_components=k)
14
+ U = svd.fit_transform(image)
15
+ V = svd.components_
16
+ S = svd.singular_values_
17
+ compressed_image = np.dot(U, np.dot(np.diag(S), V))
18
+ return compressed_image
19
+
20
+ # Streamlit app
21
+ st.title("Fast Image Compression using Truncated SVD")
22
+
23
+ # Upload an image
24
+ uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
25
+ if uploaded_file is not None:
26
+ # Load the image
27
+ image = io.imread(uploaded_file)
28
+ gray_image = color.rgb2gray(image)
29
 
30
+ # Resize the image for faster processing
31
+ resized_image = resize_image(gray_image)
32
 
33
+ # Select compression rank
34
+ k = st.slider("Select the rank for compression", min_value=1, max_value=min(resized_image.shape), value=50)
35
 
36
+ # Compress the image using Truncated SVD
37
+ compressed_image = truncated_svd_compress(resized_image, k)
 
 
 
 
 
 
 
 
 
38
 
39
+ # Display original and compressed images
40
+ st.image([gray_image, compressed_image], caption=["Original", "Compressed"])