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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -16
app.py CHANGED
@@ -4,33 +4,48 @@ from skimage import io, color
4
  from numpy.linalg import norm
5
  from PIL import Image
6
 
7
- def svd_compress(image, k):
8
- """Compress the image using SVD by keeping only the top k singular values."""
9
- U, S, Vt = np.linalg.svd(image, full_matrices=False)
10
- compressed_image = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
11
- return compressed_image
 
 
 
 
 
 
 
 
 
12
 
13
- def process_image(image, k):
14
- """Process the uploaded image, compress it using SVD, and return the result."""
15
  # Convert PIL Image to NumPy array
16
  image_np = np.array(image)
17
 
18
- # Convert to grayscale
19
- gray_image = color.rgb2gray(image_np)
 
20
 
21
  # Compress the image
22
- compressed_image = svd_compress(gray_image, k)
23
 
24
  # Convert compressed image back to PIL Image for Gradio output
25
- compressed_image_pil = Image.fromarray((compressed_image * 255).astype(np.uint8))
26
 
27
  return compressed_image_pil
28
 
29
  # Gradio interface
30
- gr_interface = gr.Interface(fn=process_image,
31
- inputs=[gr.Image(type="pil"), gr.Slider(1, 100, step=1, value=50, label="Compression Rank")],
32
- outputs=gr.Image(type="pil"),
33
- title="Interactive Image Compression using SVD",
34
- description="Upload an image and adjust the compression rank to see the compressed version.")
 
 
 
 
 
35
 
36
  gr_interface.launch()
 
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()