Spaces:
Running
Running
File size: 1,294 Bytes
c4c02ae |
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 |
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'<a href="{href}" download="{file_label}">{button_text}</a>'
if __name__ == '__main__':
main()
|