File size: 1,640 Bytes
1fd8395 |
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
import os
uploaded_images = {'characters': {}, 'terrain': {}}
def get_image_path(img, name, image_type):
file_path = f"data/uploadedImages/{image_type}/{name}/{img.name}"
os.makedirs(os.path.dirname(file_path), exist_ok=True)
with open(file_path, "wb") as img_file:
img_file.write(img.getbuffer())
return file_path
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
name = st.text_input('Enter a name for the image:')
uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept_multiple_files=True)
for uploaded_file in uploaded_files:
if uploaded_file is not None:
# Get actual image file
bytes_data = get_image_path(uploaded_file, name, image_type)
uploaded_images[image_type].setdefault(name, [])
uploaded_images[image_type][name].append(bytes_data)
st.image(bytes_data, use_column_width=True)
if image_type == 'characters':
if uploaded_images['characters']:
st.sidebar.write('**Characters**')
for name, files in uploaded_images['characters'].items():
for file in files:
st.sidebar.image(file, width=100, caption=name)
else:
if uploaded_images['terrain']:
st.write('**Terrain**')
row = []
for name, files in uploaded_images['terrain'].items():
for file in files:
row.append(file)
if len(row) == 3:
st.image(row, width=100 * 3)
row = []
if row:
st.image(row, width=100 * len(row)) # Last row, if not complete
|