NaimaAqeel commited on
Commit
3ab289d
·
verified ·
1 Parent(s): a7df863

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -6
app.py CHANGED
@@ -1,6 +1,7 @@
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):
@@ -9,14 +10,21 @@ def svd_compress(image_channel, k):
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)
@@ -26,10 +34,10 @@ def process_image(image, k):
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
@@ -40,5 +48,4 @@ gr.Interface(fn=process_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()
 
1
  import numpy as np
2
  import gradio as gr
3
  from skimage import io
4
+ from skimage.transform import resize
5
  from PIL import Image
6
 
7
  def svd_compress(image_channel, k):
 
10
  compressed_channel = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
11
  return compressed_channel
12
 
13
+ def resize_image(image_np, target_shape=(500, 500)):
14
+ """Resize the image to reduce the computation time for SVD."""
15
+ return resize(image_np, target_shape, anti_aliasing=True)
16
+
17
  def process_image(image, k):
18
  """Process the uploaded image, compress it using SVD for each color channel, and return the result."""
19
  # Convert PIL Image to NumPy array
20
  image_np = np.array(image)
21
 
22
+ # Resize the image to speed up SVD computation
23
+ image_np_resized = resize_image(image_np)
24
+
25
  # Separate the RGB channels
26
+ if len(image_np_resized.shape) == 3: # Color image
27
+ r_channel, g_channel, b_channel = image_np_resized[:, :, 0], image_np_resized[:, :, 1], image_np_resized[:, :, 2]
28
 
29
  # Compress each channel using SVD
30
  r_compressed = svd_compress(r_channel, k)
 
34
  # Stack the compressed channels back together
35
  compressed_image = np.stack([r_compressed, g_compressed, b_compressed], axis=2)
36
  else: # Grayscale image
37
+ compressed_image = svd_compress(image_np_resized, k)
38
 
39
  # Clip the values to ensure valid pixel range and convert to PIL Image for output
40
+ compressed_image = np.clip(compressed_image * 255, 0, 255)
41
  compressed_image_pil = Image.fromarray(compressed_image.astype(np.uint8))
42
 
43
  return compressed_image_pil
 
48
  gr.Slider(1, 100, step=1, value=50, label="Compression Rank")],
49
  outputs=gr.Image(type="pil", label="Compressed Image"),
50
  title="Color Image Compression using SVD",
51
+ description="Upload an image (color or grayscale), and adjust the compression rank.").launch()