NaimaAqeel commited on
Commit
d2c6d8a
·
verified ·
1 Parent(s): f42b290

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -26
app.py CHANGED
@@ -1,43 +1,44 @@
1
  import numpy as np
2
  import gradio as gr
3
- from skimage import color
4
  from PIL import Image
5
- from skimage.transform import resize
6
 
7
- def resize_image(image, target_shape=(500, 500)):
8
- """Resize image to target shape."""
9
- return resize(image, target_shape, anti_aliasing=True)
10
-
11
- def svd_compress(image, k):
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
- # Convert to grayscale
23
- gray_image = color.rgb2gray(image_np)
24
-
25
- # Resize image to 500x500 for faster processing
26
- resized_image = resize_image(gray_image, target_shape=(500, 500))
27
-
28
- # Compress the image
29
- compressed_image = svd_compress(resized_image, k)
 
 
 
 
 
30
 
31
- # Convert compressed image back to PIL Image for Gradio output
32
- compressed_image_pil = Image.fromarray((compressed_image * 255).astype(np.uint8))
 
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="Interactive Image Compression using SVD",
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()