File size: 1,370 Bytes
e38bf4d aec5ec6 e38bf4d aec5ec6 e38bf4d aec5ec6 e38bf4d aec5ec6 e38bf4d aec5ec6 e38bf4d aec5ec6 e38bf4d aec5ec6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import numpy as np
import gradio as gr
from skimage import io, color
from numpy.linalg import norm
from PIL import Image
def svd_compress(image, k):
"""Compress the image using SVD by keeping only the top k singular values."""
U, S, Vt = np.linalg.svd(image, full_matrices=False)
compressed_image = np.dot(U[:, :k], np.dot(np.diag(S[:k]), Vt[:k, :]))
return compressed_image
def process_image(image, k):
"""Process the uploaded image, compress it using SVD, and return the result."""
# Convert PIL Image to NumPy array
image_np = np.array(image)
# Convert to grayscale
gray_image = color.rgb2gray(image_np)
# Compress the image
compressed_image = svd_compress(gray_image, k)
# Convert compressed image back to PIL Image for Gradio output
compressed_image_pil = Image.fromarray((compressed_image * 255).astype(np.uint8))
return compressed_image_pil
# Gradio interface
gr_interface = gr.Interface(fn=process_image,
inputs=[gr.inputs.Image(type="pil"), gr.inputs.Slider(1, 100, step=1, default=50)],
outputs="image",
title="Interactive Image Compression using SVD",
description="Upload an image and adjust the compression rank to see the compressed version.")
gr_interface.launch() |