Vachana / Home.py
BlendMMM's picture
Update Home.py
23b7e72 verified
import streamlit as st
import pandas as pd
import os
import base64
from pathlib import Path
import streamlit as st
import os
from PIL import Image
# path = os.path.dirname(__file__)
# file_ = open(f"{path}/logo.png", "rb")
# contents = file_.read()
# data_url = base64.b64encode(contents).decode("utf-8")
# file_.close()
# def load_local_css(file_name):
# with open(file_name) as f:
# st.markdown(f"<style>{f.read()}</style>", unsafe_allow_html=True)
# def set_header():
# return st.markdown(
# f"""<div class='main-header'>
# <h1>Synthetic Control</h1>
# <img src="data:image;base64,{data_url}", alt="Logo">
# </div>""",
# unsafe_allow_html=True,
# )
st.set_page_config()
# load_local_css("styles.css")
# set_header()
st.title("The Art of Words: Vachana's Collection")
def load_images_from_folder(folder_path):
"""
Load all images from the specified folder and store them in a dictionary.
Args:
folder_path (str): Path to the folder containing images.
Returns:
dict: A dictionary with image filenames as keys and PIL Image objects as values.
"""
images_dict = {}
try:
for filename in os.listdir(folder_path):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
img_path = os.path.join(folder_path, filename)
images_dict[filename] = Image.open(img_path)
except Exception as e:
st.error(f"Error loading images: {e}")
return images_dict
# Folder selection
with st.expander("Click to Discover the Masterpiece"):
folder_path = 'Photos'
def load_images_from_folder(folder_path, target_size=(200, 200)):
"""
Load all images from the specified folder, resize them to a uniform size, and store them in a dictionary.
Args:
folder_path (str): Path to the folder containing images.
target_size (tuple): Desired size for all images (width, height).
Returns:
dict: A dictionary with image filenames as keys and resized PIL Image objects as values.
"""
images_dict = {}
try:
for filename in os.listdir(folder_path):
if filename.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp')):
img_path = os.path.join(folder_path, filename)
img = Image.open(img_path).convert("RGB") # Convert to RGB for consistency
img = img.resize(target_size) # Resize image
images_dict[filename] = img
except Exception as e:
st.error(f"Error loading images: {e}")
return images_dict
# Streamlit UI
if folder_path:
if not os.path.exists(folder_path):
st.error("The specified folder path does not exist. Please enter a valid path.")
else:
# Load images
images = load_images_from_folder(folder_path, target_size=(300, 400)) # Set desired size
if images:
# Display images side by side with a row break
cols_per_row = 3 # Adjust the number of images displayed per row
images_list = list(images.items())
for i in range(0, len(images_list), cols_per_row):
cols = st.columns(cols_per_row)
for col, (img_name, img) in zip(cols, images_list[i:i + cols_per_row]):
with col:
st.image(img, use_container_width=True)
# Add a break after each row
st.divider() # Simple divider
else:
st.warning("No images found in the specified folder.")
else:
st.info("Please enter a folder path to load and display images.")