Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from PIL import Image
|
3 |
+
import io
|
4 |
+
|
5 |
+
def resize_image(image, target_size_kb=None, quality=85):
|
6 |
+
"""Resizes and compresses an image."""
|
7 |
+
img = Image.open(image)
|
8 |
+
img_byte_arr = io.BytesIO()
|
9 |
+
img.save(img_byte_arr, format='JPEG', quality=quality)
|
10 |
+
current_size_kb = len(img_byte_arr.getvalue()) / 1024
|
11 |
+
|
12 |
+
# Resize based on target size (if provided)
|
13 |
+
if target_size_kb:
|
14 |
+
while current_size_kb > target_size_kb and quality > 10:
|
15 |
+
quality -= 5
|
16 |
+
img_byte_arr = io.BytesIO()
|
17 |
+
img.save(img_byte_arr, format='JPEG', quality=quality)
|
18 |
+
current_size_kb = len(img_byte_arr.getvalue()) / 1024
|
19 |
+
else:
|
20 |
+
# Resize based on quality
|
21 |
+
pass # No resizing for quality-based option
|
22 |
+
|
23 |
+
img = Image.open(img_byte_arr)
|
24 |
+
return img
|
25 |
+
|
26 |
+
st.title("Image Resizer")
|
27 |
+
st.write("Resize and compress your images!")
|
28 |
+
|
29 |
+
# Upload image
|
30 |
+
uploaded_file = st.file_uploader("Choose an Image:", type=["jpg", "jpeg", "png"])
|
31 |
+
|
32 |
+
if uploaded_file is not None:
|
33 |
+
# Option selection for resizing
|
34 |
+
resize_option = st.selectbox("Resize Option:", ("Percentage Quality", "Desired Output Size"))
|
35 |
+
|
36 |
+
if resize_option == "Percentage Quality":
|
37 |
+
# User input for quality
|
38 |
+
quality = st.slider("Quality (%)", min_value=10, max_value=100, value=85)
|
39 |
+
|
40 |
+
elif resize_option == "Desired Output Size":
|
41 |
+
# Options for size units
|
42 |
+
size_unit_options = ("KB", "MB")
|
43 |
+
size_unit = st.selectbox("Size Unit", size_unit_options)
|
44 |
+
|
45 |
+
# User input for size
|
46 |
+
target_size = st.number_input("Target Size", min_value=1)
|
47 |
+
|
48 |
+
# Convert target size to KB based on unit
|
49 |
+
if size_unit == "MB":
|
50 |
+
target_size_kb = target_size * 1024
|
51 |
+
else:
|
52 |
+
target_size_kb = target_size
|
53 |
+
|
54 |
+
# Process and display image
|
55 |
+
if uploaded_file and (quality or target_size_kb):
|
56 |
+
resized_image = resize_image(uploaded_file, target_size_kb, quality)
|
57 |
+
st.image(resized_image, caption="Resized Image")
|
58 |
+
|
59 |
+
# Download link
|
60 |
+
with io.BytesIO() as buffer:
|
61 |
+
resized_image.save(buffer, format="JPEG")
|
62 |
+
download_link = st.download_button(
|
63 |
+
label="Download Resized Image",
|
64 |
+
data=buffer.getvalue(),
|
65 |
+
file_name="resized_image.jpg",
|
66 |
+
mime="image/jpeg",
|
67 |
+
)
|
68 |
+
|
69 |
+
# Requirements (req.txt)
|
70 |
+
|
71 |
+
# Since the app uses only built-in libraries (streamlit and Pillow), there's no need for an external requirements file.
|
72 |
+
# However, if you plan to use additional libraries, you can create a req.txt file listing them.
|
73 |
+
# For example:
|
74 |
+
# streamlit
|
75 |
+
# Pillow
|