Update app.py
Browse files
app.py
CHANGED
|
@@ -1,26 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
def compress_webp(input_image,
|
| 5 |
"""
|
| 6 |
-
Compresses a WebP image with the specified quality.
|
| 7 |
|
| 8 |
Args:
|
| 9 |
input_image (str): Path to the input WebP image.
|
| 10 |
-
output_image (str): Path to save the compressed WebP image.
|
| 11 |
quality (int): Compression quality (0-100), where 0 is the lowest quality and 100 is the highest.
|
|
|
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
try:
|
| 14 |
img = Image.open(input_image)
|
|
|
|
|
|
|
| 15 |
img.save(output_image, "webp", quality=quality)
|
| 16 |
except Exception as e:
|
| 17 |
st.error(f"Error: {e}")
|
| 18 |
-
return
|
| 19 |
|
| 20 |
-
st.success(
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
|
| 24 |
|
| 25 |
def main():
|
| 26 |
st.title("WebP Image Compressor")
|
|
@@ -36,7 +42,12 @@ def main():
|
|
| 36 |
|
| 37 |
if st.sidebar.button("Compress"):
|
| 38 |
with st.spinner("Compressing..."):
|
| 39 |
-
compress_webp(input_image,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
if __name__ == "__main__":
|
| 42 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from PIL import Image
|
| 3 |
+
import tempfile
|
| 4 |
+
import os
|
| 5 |
|
| 6 |
+
def compress_webp(input_image, quality):
|
| 7 |
"""
|
| 8 |
+
Compresses a WebP image with the specified quality and returns a temporary URL.
|
| 9 |
|
| 10 |
Args:
|
| 11 |
input_image (str): Path to the input WebP image.
|
|
|
|
| 12 |
quality (int): Compression quality (0-100), where 0 is the lowest quality and 100 is the highest.
|
| 13 |
+
|
| 14 |
+
Returns:
|
| 15 |
+
str: Temporary URL for the compressed WebP image.
|
| 16 |
"""
|
| 17 |
try:
|
| 18 |
img = Image.open(input_image)
|
| 19 |
+
temp_dir = tempfile.mkdtemp()
|
| 20 |
+
output_image = os.path.join(temp_dir, "compressed_image.webp")
|
| 21 |
img.save(output_image, "webp", quality=quality)
|
| 22 |
except Exception as e:
|
| 23 |
st.error(f"Error: {e}")
|
| 24 |
+
return None
|
| 25 |
|
| 26 |
+
st.success("Image compressed successfully.")
|
| 27 |
|
| 28 |
+
# Return the temporary URL for the compressed image
|
| 29 |
+
return output_image
|
| 30 |
|
| 31 |
def main():
|
| 32 |
st.title("WebP Image Compressor")
|
|
|
|
| 42 |
|
| 43 |
if st.sidebar.button("Compress"):
|
| 44 |
with st.spinner("Compressing..."):
|
| 45 |
+
compressed_image_path = compress_webp(input_image, quality)
|
| 46 |
+
|
| 47 |
+
if compressed_image_path:
|
| 48 |
+
# Provide a download link for the compressed image
|
| 49 |
+
st.subheader("Download Compressed Image")
|
| 50 |
+
st.markdown(f"Click [here]({compressed_image_path}) to download the compressed image.")
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
main()
|