Download app.py from MFF212/apphoster: direct link, hf CLI and curl.
- Browser
- Download file 1.7 kB
-
https://huggingface.co/spaces/MFF212/apphoster/resolve/main/app.py
- Command line
-
hf download hf://spaces/MFF212/apphoster/app.py
-
curl -L -o app.py https://huggingface.co/spaces/MFF212/apphoster/resolve/main/app.py
1.7 kB
| 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() | |