import streamlit as st from PIL import Image import io import base64 def main(): st.title('Image Resizer') uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: try: img = Image.open(uploaded_file) width = st.number_input('Enter the width:', min_value=1) height = st.number_input('Enter the height:', min_value=1) if st.button('Resize'): resized_img = img.resize((int(width), int(height))) output = io.BytesIO() resized_img.save(output, format='JPEG') # Change format if desired (JPEG, PNG, etc.) st.markdown(get_binary_file_downloader_html(output, 'resized_image.jpg', 'Download Resized Image'), unsafe_allow_html=True) except Exception as e: st.error(f"An error occurred: {str(e)}") def get_binary_file_downloader_html(bin_file, file_label='File', button_text='Download'): # Helper function to create download link bin_str = bin_file.getvalue() bin_file.close() href = f'data:application/octet-stream;base64,{base64.b64encode(bin_str).decode()}' return f'{button_text}' if __name__ == '__main__': main()