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 44 45 46 47 48 49 50 51 52 53 54 55 56 |
import streamlit as st from PIL import Image from io import BytesIO # Title of the app st.title("Merge Multiple Images") # Instructions for the user st.write("Upload multiple images, choose the merge direction, and download the merged image.") # File uploader for multiple image files uploaded_files = st.file_uploader("Choose images", type=["jpg", "jpeg", "png"], accept_multiple_files=True) # Radio button for selecting merge direction merge_direction = st.radio("Choose merge direction", ("Horizontal", "Vertical")) # Button to merge images if st.button("Merge Images"): if uploaded_files: images = [Image.open(img).convert("RGB") for img in uploaded_files] # Determine the size of the merged image based on the direction if merge_direction == "Horizontal": total_width = sum(img.width for img in images) max_height = max(img.height for img in images) merged_image = Image.new("RGB", (total_width, max_height)) # Paste images side by side x_offset = 0 for img in images: merged_image.paste(img, (x_offset, 0)) x_offset += img.width else: # Vertical merge max_width = max(img.width for img in images) total_height = sum(img.height for img in images) merged_image = Image.new("RGB", (max_width, total_height)) # Paste images one below the other y_offset = 0 for img in images: merged_image.paste(img, (0, y_offset)) y_offset += img.height # Save the merged image to a BytesIO buffer img_buffer = BytesIO() merged_image.save(img_buffer, format="JPEG") img_buffer.seek(0) # Provide download button for merged image st.download_button(label="Download Merged Image", data=img_buffer, file_name="merged_image.jpg", mime="image/jpeg") else: st.warning("Please upload at least two images to merge.") |