import numpy as np from skimage import io, color from skimage.transform import resize from sklearn.decomposition import TruncatedSVD import streamlit as st def resize_image(image, target_shape=(500, 500)): """Resize image to reduce processing time.""" return resize(image, target_shape, anti_aliasing=True) 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) V = svd.components_ S = svd.singular_values_ compressed_image = np.dot(U, np.dot(np.diag(S), V)) return compressed_image # Streamlit app st.title("Fast Image Compression using Truncated SVD") # Upload an image uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"]) if uploaded_file is not None: # Load the image image = io.imread(uploaded_file) gray_image = color.rgb2gray(image) # Resize the image for faster processing resized_image = resize_image(gray_image) # Select compression rank k = st.slider("Select the rank for compression", min_value=1, max_value=min(resized_image.shape), value=50) # Compress the image using Truncated SVD compressed_image = truncated_svd_compress(resized_image, k) # Display original and compressed images st.image([gray_image, compressed_image], caption=["Original", "Compressed"])