Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 29 |
img = Image.open(io.BytesIO(image_data))
|
| 30 |
-
img_array = np.array(img)
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 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()
|