Spaces:
Running
Running
Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -12,6 +12,11 @@ def resize_image(image, target_size_kb=None, quality=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
|
@@ -30,38 +35,44 @@ def resize_image(image, target_size_kb=None, quality=None):
|
|
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')
|
34 |
-
|
35 |
-
|
36 |
|
37 |
-
return
|
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 |
-
|
|
|
|
|
|
|
|
|
58 |
elif resize_option == "Select an option":
|
59 |
st.info("Please select a resize option to proceed.")
|
60 |
-
|
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:
|
@@ -71,4 +82,4 @@ if uploaded_file is not None:
|
|
71 |
data=buffer.getvalue(),
|
72 |
file_name="resized_image.jpg",
|
73 |
mime="image/jpeg",
|
74 |
-
)
|
|
|
12 |
|
13 |
img_byte_arr = io.BytesIO()
|
14 |
|
15 |
+
original_size_kb = len(image.getvalue()) / 1024 # Calculate the original size in KB
|
16 |
+
if target_size_kb and target_size_kb > original_size_kb:
|
17 |
+
st.error("Target size must be less than or equal to the original image size.")
|
18 |
+
return None
|
19 |
+
|
20 |
if target_size_kb is not None:
|
21 |
# Resize based on target size
|
22 |
current_quality = 95 # Start with a high quality
|
|
|
35 |
img.save(img_byte_arr, format='JPEG', quality=quality)
|
36 |
else:
|
37 |
st.warning("No resize option selected. Displaying original image.")
|
38 |
+
img.save(img_byte_arr, format='JPEG') # Save the original image in byte array
|
39 |
+
|
40 |
+
resized_img = Image.open(img_byte_arr) # Reopen the image from the byte array
|
41 |
|
42 |
+
return resized_img
|
43 |
|
44 |
st.title("Image Resizer")
|
45 |
+
st.write("Resize and compress your images with ease!")
|
46 |
|
47 |
uploaded_file = st.file_uploader("Choose an Image:", type=["jpg", "jpeg", "png"])
|
48 |
|
49 |
if uploaded_file is not None:
|
50 |
+
original_size_kb = len(uploaded_file.getvalue()) / 1024 # Get original size in KB
|
51 |
+
st.info(f"Original Image Size: {original_size_kb:.2f} KB")
|
52 |
+
|
53 |
resize_option = st.selectbox("Resize Option:", ("Select an option", "Percentage Quality", "Desired Output Size"))
|
54 |
|
55 |
+
resized_image = None # Initialize resized_image as None
|
56 |
+
|
57 |
if resize_option == "Percentage Quality":
|
58 |
quality = st.slider("Quality (%)", min_value=1, max_value=100, value=85)
|
59 |
resized_image = resize_image(uploaded_file, quality=quality)
|
60 |
elif resize_option == "Desired Output Size":
|
61 |
size_unit = st.selectbox("Size Unit", ("KB", "MB"))
|
62 |
+
target_size = st.number_input("Target Size", min_value=1, value=int(original_size_kb))
|
63 |
+
|
64 |
if size_unit == "MB":
|
65 |
target_size_kb = target_size * 1024
|
66 |
else:
|
67 |
target_size_kb = target_size
|
68 |
+
|
69 |
+
if target_size_kb > original_size_kb:
|
70 |
+
st.error("Target size must be less than or equal to the original image size.")
|
71 |
+
else:
|
72 |
+
resized_image = resize_image(uploaded_file, target_size_kb=target_size_kb)
|
73 |
elif resize_option == "Select an option":
|
74 |
st.info("Please select a resize option to proceed.")
|
75 |
+
|
|
|
|
|
|
|
|
|
76 |
if resized_image:
|
77 |
st.image(resized_image, caption="Resized Image")
|
78 |
with io.BytesIO() as buffer:
|
|
|
82 |
data=buffer.getvalue(),
|
83 |
file_name="resized_image.jpg",
|
84 |
mime="image/jpeg",
|
85 |
+
)
|