Spaces:
Running
Running
import streamlit as st | |
from PIL import Image | |
import io | |
def resize_image(image, target_size_kb=None, quality=None): | |
"""Resizes and compresses an image based on either target size or quality.""" | |
try: | |
img = Image.open(image) | |
except Exception as e: | |
st.error(f"Error opening image: {e}") | |
return None | |
img_byte_arr = io.BytesIO() | |
original_size_kb = len(image.getvalue()) / 1024 # Calculate the original size in KB | |
if target_size_kb and target_size_kb > original_size_kb: | |
st.error("Target size must be less than or equal to the original image size.") | |
return None | |
if target_size_kb is not None: | |
# Resize based on target size | |
current_quality = 95 # Start with a high quality | |
while True: | |
img_byte_arr = io.BytesIO() | |
img.save(img_byte_arr, format='JPEG', quality=current_quality) | |
current_size_kb = len(img_byte_arr.getvalue()) / 1024 | |
if current_size_kb <= target_size_kb or current_quality <= 10: | |
break | |
current_quality -= 5 # Adjust quality in smaller steps | |
if current_quality <= 10 and current_size_kb > target_size_kb: | |
st.warning("Could not achieve the exact target size. Returning best possible result.") | |
elif quality is not None: | |
# Resize based on quality percentage | |
img.save(img_byte_arr, format='JPEG', quality=quality) | |
else: | |
st.warning("No resize option selected. Displaying original image.") | |
img.save(img_byte_arr, format='JPEG') # Save the original image in byte array | |
resized_img = Image.open(img_byte_arr) # Reopen the image from the byte array | |
return resized_img | |
st.title("Image Resizer") | |
st.write("Resize and compress your images with ease!") | |
uploaded_file = st.file_uploader("Choose an Image:", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
original_size_kb = len(uploaded_file.getvalue()) / 1024 # Get original size in KB | |
st.info(f"Original Image Size: {original_size_kb:.2f} KB") | |
resize_option = st.selectbox("Resize Option:", ("Select an option", "Percentage Quality", "Desired Output Size")) | |
resized_image = None # Initialize resized_image as None | |
if resize_option == "Percentage Quality": | |
quality = st.slider("Quality (%)", min_value=1, max_value=100, value=85) | |
resized_image = resize_image(uploaded_file, quality=quality) | |
elif resize_option == "Desired Output Size": | |
size_unit = st.selectbox("Size Unit", ("KB", "MB")) | |
target_size = st.number_input("Target Size", min_value=1, value=int(original_size_kb)) | |
if size_unit == "MB": | |
target_size_kb = target_size * 1024 | |
else: | |
target_size_kb = target_size | |
if target_size_kb > original_size_kb: | |
st.error("Target size must be less than or equal to the original image size.") | |
else: | |
resized_image = resize_image(uploaded_file, target_size_kb=target_size_kb) | |
elif resize_option == "Select an option": | |
st.info("Please select a resize option to proceed.") | |
if resized_image: | |
st.image(resized_image, caption="Resized Image") | |
with io.BytesIO() as buffer: | |
resized_image.save(buffer, format="JPEG") | |
st.download_button( | |
label="Download Resized Image", | |
data=buffer.getvalue(), | |
file_name="resized_image.jpg", | |
mime="image/jpeg", | |
) | |