Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,39 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
def main():
|
| 6 |
-
st.title("
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
if
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
f.write(uploaded_file.getvalue())
|
| 14 |
-
|
| 15 |
-
# Run the uploaded Streamlit app as a subprocess
|
| 16 |
-
cmd = ["streamlit", "run", "app.py"]
|
| 17 |
-
|
| 18 |
-
try:
|
| 19 |
-
st.write("Running the uploaded app...")
|
| 20 |
-
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
|
| 21 |
-
st.write(result.stdout)
|
| 22 |
-
except subprocess.CalledProcessError as e:
|
| 23 |
-
st.error(f"Error running the app:\n{e.stderr}")
|
| 24 |
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
if __name__ == "__main__":
|
| 29 |
main()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from PIL import Image
|
| 3 |
+
|
| 4 |
+
def compress_webp(input_image, output_image, quality):
|
| 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(f"Image compressed successfully and saved as {output_image}")
|
| 21 |
|
| 22 |
def main():
|
| 23 |
+
st.title("WebP Image Compressor")
|
| 24 |
+
st.sidebar.header("Settings")
|
| 25 |
|
| 26 |
+
input_image = st.sidebar.file_uploader("Upload a WebP Image", type=["webp"])
|
| 27 |
+
|
| 28 |
+
if not input_image:
|
| 29 |
+
st.warning("Please upload a WebP image.")
|
| 30 |
+
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
quality = st.sidebar.slider("Compression Quality (0-100)", min_value=0, max_value=100, value=80)
|
| 33 |
+
|
| 34 |
+
if st.sidebar.button("Compress"):
|
| 35 |
+
with st.spinner("Compressing..."):
|
| 36 |
+
compress_webp(input_image, "compressed_image.webp", quality)
|
| 37 |
|
| 38 |
if __name__ == "__main__":
|
| 39 |
main()
|