Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from PIL import Image
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def compress_image(input_image):
|
| 7 |
+
"""
|
| 8 |
+
Compresses a PIL image object into a lossless WebP format
|
| 9 |
+
and returns the file path.
|
| 10 |
+
"""
|
| 11 |
+
if input_image is None:
|
| 12 |
+
raise gr.Error("No image uploaded. Please upload an image to compress.")
|
| 13 |
+
|
| 14 |
+
try:
|
| 15 |
+
# Create a temporary file to store the compressed image.
|
| 16 |
+
# We use delete=False so that Gradio can access the file
|
| 17 |
+
# after this function returns. Gradio will handle cleanup.
|
| 18 |
+
with tempfile.NamedTemporaryFile(suffix=".webp", delete=False) as temp_file:
|
| 19 |
+
|
| 20 |
+
# Save the image using Pillow in lossless WebP format
|
| 21 |
+
# quality=100 is often recommended with lossless=True
|
| 22 |
+
input_image.save(temp_file.name, format="WEBP", lossless=True, quality=100)
|
| 23 |
+
|
| 24 |
+
output_filepath = temp_file.name
|
| 25 |
+
|
| 26 |
+
# Get file sizes for comparison
|
| 27 |
+
# We need to get the size of the original PIL image.
|
| 28 |
+
# This is a bit tricky, so we'll just show the output size.
|
| 29 |
+
# A more complex app might take a file path input to compare.
|
| 30 |
+
output_size = os.path.getsize(output_filepath)
|
| 31 |
+
|
| 32 |
+
print(f"Image saved to: {output_filepath}")
|
| 33 |
+
print(f"Compressed file size: {output_size / 1024:.2f} KB")
|
| 34 |
+
|
| 35 |
+
# Return the file path to the gr.File component
|
| 36 |
+
return output_filepath
|
| 37 |
+
|
| 38 |
+
except Exception as e:
|
| 39 |
+
print(f"Error compressing image: {e}")
|
| 40 |
+
raise gr.Error(f"Failed to compress image: {str(e)}")
|
| 41 |
+
|
| 42 |
+
# --- Gradio App Interface ---
|
| 43 |
+
|
| 44 |
+
# Use gr.Blocks for a more custom layout
|
| 45 |
+
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 46 |
+
gr.Markdown(
|
| 47 |
+
"""
|
| 48 |
+
# Lossless Image Compressor
|
| 49 |
+
Upload any image (PNG, JPG, BMP, etc.) to convert it into a **Lossless WebP** file.
|
| 50 |
+
This modern format significantly reduces file size without *any* loss in image quality.
|
| 51 |
+
"""
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
with gr.Row(equal_height=True):
|
| 55 |
+
with gr.Column(scale=1):
|
| 56 |
+
image_input = gr.Image(
|
| 57 |
+
type="pil",
|
| 58 |
+
label="Upload Your Image",
|
| 59 |
+
sources=["upload", "clipboard"]
|
| 60 |
+
)
|
| 61 |
+
compress_button = gr.Button("Compress Image", variant="primary")
|
| 62 |
+
|
| 63 |
+
with gr.Column(scale=1):
|
| 64 |
+
file_output = gr.File(
|
| 65 |
+
label="Download Compressed WebP File",
|
| 66 |
+
interactive=False # User cannot upload to this component
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
gr.Markdown("---")
|
| 70 |
+
gr.Markdown("### Examples")
|
| 71 |
+
|
| 72 |
+
# Add examples for users to try
|
| 73 |
+
gr.Examples(
|
| 74 |
+
examples=[
|
| 75 |
+
# Using placeholder images as examples
|
| 76 |
+
"https://placehold.co/800x600/png?text=Sample+PNG+Image",
|
| 77 |
+
"https://placehold.co/800x600/jpg?text=Sample+JPG+Image",
|
| 78 |
+
],
|
| 79 |
+
inputs=image_input,
|
| 80 |
+
outputs=file_output,
|
| 81 |
+
fn=compress_image,
|
| 82 |
+
cache_examples=True # Speeds up example loading
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
# Connect the button to the function
|
| 86 |
+
compress_button.click(
|
| 87 |
+
fn=compress_image,
|
| 88 |
+
inputs=image_input,
|
| 89 |
+
outputs=file_output,
|
| 90 |
+
api_name="compress"
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
demo.launch()
|