Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -2,74 +2,73 @@ import streamlit as st
|
|
2 |
from PIL import Image
|
3 |
import io
|
4 |
|
5 |
-
def resize_image(image, target_size_kb=None, quality=
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
|
|
11 |
|
12 |
-
|
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 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
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 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
else:
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
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
|
|
|
2 |
from PIL import Image
|
3 |
import io
|
4 |
|
5 |
+
def resize_image(image, target_size_kb=None, quality=None):
|
6 |
+
"""Resizes and compresses an image based on either target size or quality."""
|
7 |
+
try:
|
8 |
+
img = Image.open(image)
|
9 |
+
except Exception as e:
|
10 |
+
st.error(f"Error opening image: {e}")
|
11 |
+
return None
|
12 |
|
13 |
+
img_byte_arr = io.BytesIO()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
+
if target_size_kb is not None:
|
16 |
+
# Resize based on target size
|
17 |
+
current_quality = 95 # Start with a high quality
|
18 |
+
while True:
|
19 |
+
img_byte_arr = io.BytesIO()
|
20 |
+
img.save(img_byte_arr, format='JPEG', quality=current_quality)
|
21 |
+
current_size_kb = len(img_byte_arr.getvalue()) / 1024
|
22 |
+
if current_size_kb <= target_size_kb or current_quality <= 10:
|
23 |
+
break
|
24 |
+
current_quality -= 5 # Adjust quality in smaller steps
|
25 |
+
if current_quality <= 10 and current_size_kb > target_size_kb:
|
26 |
+
st.warning("Could not achieve the exact target size. Returning best possible result.")
|
27 |
+
|
28 |
+
elif quality is not None:
|
29 |
+
# Resize based on quality percentage
|
30 |
+
img.save(img_byte_arr, format='JPEG', quality=quality)
|
31 |
+
else:
|
32 |
+
st.warning("No resize option selected. Displaying original image.")
|
33 |
+
img.save(img_byte_arr, format='JPEG') #save the original image in byte array
|
34 |
+
|
35 |
+
img = Image.open(img_byte_arr) #reopen the image from the byte array
|
36 |
+
|
37 |
+
return img
|
38 |
|
39 |
st.title("Image Resizer")
|
40 |
st.write("Resize and compress your images!")
|
41 |
|
|
|
42 |
uploaded_file = st.file_uploader("Choose an Image:", type=["jpg", "jpeg", "png"])
|
43 |
|
44 |
if uploaded_file is not None:
|
45 |
+
resize_option = st.selectbox("Resize Option:", ("Select an option", "Percentage Quality", "Desired Output Size"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
|
47 |
+
if resize_option == "Percentage Quality":
|
48 |
+
quality = st.slider("Quality (%)", min_value=1, max_value=100, value=85)
|
49 |
+
resized_image = resize_image(uploaded_file, quality=quality)
|
50 |
+
elif resize_option == "Desired Output Size":
|
51 |
+
size_unit = st.selectbox("Size Unit", ("KB", "MB"))
|
52 |
+
target_size = st.number_input("Target Size", min_value=1)
|
53 |
+
if size_unit == "MB":
|
54 |
+
target_size_kb = target_size * 1024
|
55 |
+
else:
|
56 |
+
target_size_kb = target_size
|
57 |
+
resized_image = resize_image(uploaded_file, target_size_kb=target_size_kb)
|
58 |
+
elif resize_option == "Select an option":
|
59 |
+
st.info("Please select a resize option to proceed.")
|
60 |
+
resized_image = Image.open(uploaded_file)
|
61 |
else:
|
62 |
+
st.error("Invalid resize option selected.")
|
63 |
+
resized_image = None
|
64 |
+
|
65 |
+
if resized_image:
|
66 |
+
st.image(resized_image, caption="Resized Image")
|
67 |
+
with io.BytesIO() as buffer:
|
68 |
+
resized_image.save(buffer, format="JPEG")
|
69 |
+
st.download_button(
|
70 |
+
label="Download Resized Image",
|
71 |
+
data=buffer.getvalue(),
|
72 |
+
file_name="resized_image.jpg",
|
73 |
+
mime="image/jpeg",
|
74 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|