binaychandra commited on
Commit
5558592
·
verified ·
1 Parent(s): 1513d3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -28
app.py CHANGED
@@ -25,38 +25,25 @@ def calculate_storage(shape, r_value):
25
  }
26
 
27
  def process_image(image_data, r_value):
28
- # Open image as RGB
29
  img = Image.open(io.BytesIO(image_data))
30
- img_array = np.array(img)
31
 
32
- # Check if image is RGB (3 channels)
33
- if len(img_array.shape) == 3 and img_array.shape[2] == 3:
34
- # Process each channel separately
35
- reconstructed = np.zeros_like(img_array)
36
-
37
- for channel in range(3):
38
- # Extract the channel
39
- channel_data = img_array[:, :, channel]
40
-
41
- # Perform SVD on this channel
42
- U, s, Vt = svd(channel_data, full_matrices=False)
43
-
44
- # Reconstruct image with r singular values
45
- r = min(r_value, len(s))
46
- reconstructed[:, :, channel] = np.dot(U[:, :r] * s[:r], Vt[:r, :])
47
-
48
- # Clip values to valid range and convert to uint8
49
- reconstructed = np.clip(reconstructed, 0, 255).astype(np.uint8)
50
- else:
51
- # Fallback to grayscale processing if not RGB
52
- img_array = np.array(img.convert('L'))
53
- U, s, Vt = svd(img_array, full_matrices=False)
54
- r = min(r_value, len(s))
55
- reconstructed = np.dot(U[:, :r] * s[:r], Vt[:r, :])
56
- reconstructed = np.clip(reconstructed, 0, 255).astype(np.uint8)
57
 
58
  # Convert back to image
59
- reconstructed_img = Image.fromarray(reconstructed)
60
 
61
  # Save to base64 string
62
  buffered = io.BytesIO()
 
25
  }
26
 
27
  def process_image(image_data, r_value):
28
+ # Open image and convert to grayscale
29
  img = Image.open(io.BytesIO(image_data))
 
30
 
31
+ # Always convert to grayscale
32
+ grayscale_img = img.convert('L')
33
+ img_array = np.array(grayscale_img)
34
+
35
+ # Perform SVD on the grayscale data
36
+ U, s, Vt = svd(img_array, full_matrices=False)
37
+
38
+ # Reconstruct image with r singular values
39
+ r = min(r_value, len(s))
40
+ reconstructed = np.dot(U[:, :r] * s[:r], Vt[:r, :])
41
+
42
+ # Clip values to valid range and convert to uint8
43
+ reconstructed = np.clip(reconstructed, 0, 255).astype(np.uint8)
 
 
 
 
 
 
 
 
 
 
 
 
44
 
45
  # Convert back to image
46
+ reconstructed_img = Image.fromarray(reconstructed, mode='L')
47
 
48
  # Save to base64 string
49
  buffered = io.BytesIO()