app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import streamlit as st from PIL import Image from io import BytesIO # Import BytesIO # Title of the app st.title("Image Resizer with Live Preview") # Instructions for the user st.write("Upload an image and resize it using the input fields below.") # File uploader for image uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"]) # Initialize variables if uploaded_file is not None: # Open and display the uploaded image image = Image.open(uploaded_file) st.image(image, caption="Original Image", use_column_width=True) # Get the current dimensions width, height = image.size # Input fields for resizing st.write("Resize Image:") new_width = st.number_input("Width", min_value=1, max_value=3000, value=width, step=1) new_height = st.number_input("Height", min_value=1, max_value=3000, value=height, step=1) # Resize the image and display the preview if st.button("Resize"): resized_image = image.resize((new_width, new_height)) st.image(resized_image, caption="Resized Image", use_column_width=True) # Create a download button for the resized image buffered = BytesIO() resized_image.save(buffered, format="PNG") buffered.seek(0) st.download_button(label="Download Resized Image", data=buffered, file_name="resized_image.png", mime="image/png") else: st.info("Please upload an image to get started.") |