File size: 1,342 Bytes
e38bf4d d243d0e a19fb9f d243d0e aec5ec6 a19fb9f 014d1cd d243d0e a19fb9f d243d0e 9b319b3 a19fb9f 9b319b3 aec5ec6 a19fb9f b13bb13 d243d0e a19fb9f a655cfe d243d0e aec5ec6 d243d0e a19fb9f f51b0f2 eefc239 |
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 37 |
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(fn=process_image,
inputs=[gr.Image(type="pil", shape=(500, 500)),
gr.Slider(1, 100, step=1, value=50, label="Compression Rank")],
outputs=gr.Image(type="pil"),
title="Interactive Image Compression using SVD",
description="Upload an image (500x500 max) and adjust the compression rank.")
gr_interface.launch() |