|
import numpy as np |
|
import gradio as gr |
|
from skimage import color |
|
from sklearn.decomposition import TruncatedSVD |
|
from PIL import Image |
|
|
|
def truncated_svd_compress(image, k): |
|
"""Compress the image using Truncated SVD by keeping only the top k singular values.""" |
|
svd = TruncatedSVD(n_components=k) |
|
U = svd.fit_transform(image) |
|
S = svd.singular_values_ |
|
Vt = svd.components_ |
|
compressed_image = np.dot(U, np.dot(np.diag(S), Vt)) |
|
return compressed_image |
|
|
|
def process_image(image, k): |
|
"""Process the uploaded image, compress it using Truncated SVD, and return the result.""" |
|
|
|
image_np = np.array(image) |
|
|
|
|
|
gray_image = color.rgb2gray(image_np) |
|
|
|
|
|
if len(gray_image.shape) == 3: |
|
gray_image = gray_image[:, :, 0] |
|
|
|
|
|
compressed_image = truncated_svd_compress(gray_image, k) |
|
|
|
|
|
compressed_image = np.clip(compressed_image, 0, 1) |
|
compressed_image = (compressed_image * 255).astype(np.uint8) |
|
|
|
|
|
compressed_image_pil = Image.fromarray(compressed_image) |
|
|
|
|
|
if compressed_image_pil.mode != 'RGB': |
|
compressed_image_pil = compressed_image_pil.convert('RGB') |
|
|
|
return compressed_image_pil |
|
|
|
|
|
gr_interface = gr.Interface( |
|
fn=process_image, |
|
inputs=[ |
|
gr.Image(type="pil", label="Upload Image"), |
|
gr.Slider(1, 100, step=1, value=50, label="Compression Rank") |
|
], |
|
outputs=gr.Image(type="pil", label="Compressed Image"), |
|
title="Interactive Image Compression using Truncated SVD", |
|
description="Upload an image and adjust the compression rank to see the compressed version. The app compresses the image while retaining important features." |
|
) |
|
|
|
|
|
gr_interface.launch() |
|
|