File size: 1,697 Bytes
637fb25
acd9c2f
05f9c75
 
acd9c2f
05f9c75
acd9c2f
05f9c75
acd9c2f
 
 
 
05f9c75
 
 
acd9c2f
 
 
05f9c75
 
acd9c2f
 
 
05f9c75
acd9c2f
05f9c75
637fb25
05f9c75
 
6e498e2
637fb25
acd9c2f
 
637fb25
acd9c2f
 
 
 
 
637fb25
acd9c2f
 
 
 
05f9c75
 
 
 
 
 
637fb25
 
 
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import streamlit as st
from PIL import Image
import tempfile
import os

def compress_webp(input_image, quality):
    """
    Compresses a WebP image with the specified quality and returns a temporary URL.
    
    Args:
        input_image (str): Path to the input WebP image.
        quality (int): Compression quality (0-100), where 0 is the lowest quality and 100 is the highest.
    
    Returns:
        str: Temporary URL for the compressed WebP image.
    """
    try:
        img = Image.open(input_image)
        temp_dir = tempfile.mkdtemp()
        output_image = os.path.join(temp_dir, "compressed_image.webp")
        img.save(output_image, "webp", quality=quality)
    except Exception as e:
        st.error(f"Error: {e}")
        return None

    st.success("Image compressed successfully.")

    # Return the temporary URL for the compressed image
    return output_image

def main():
    st.title("WebP Image Compressor")
    st.sidebar.header("Settings")

    input_image = st.sidebar.file_uploader("Upload a WebP Image", type=["webp"])

    if not input_image:
        st.warning("Please upload a WebP image.")
        return

    quality = st.sidebar.slider("Compression Quality (0-100)", min_value=0, max_value=100, value=80)
    
    if st.sidebar.button("Compress"):
        with st.spinner("Compressing..."):
            compressed_image_path = compress_webp(input_image, quality)

        if compressed_image_path:
            # Provide a download link for the compressed image
            st.subheader("Download Compressed Image")
            st.markdown(f"Click [here]({compressed_image_path}) to download the compressed image.")

if __name__ == "__main__":
    main()