Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
|
4 |
+
uploaded_images = {'characters': {}, 'terrain': {}}
|
5 |
+
|
6 |
+
def get_image_path(img, name, image_type):
|
7 |
+
file_path = f"data/uploadedImages/{image_type}/{name}/{img.name}"
|
8 |
+
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
9 |
+
with open(file_path, "wb") as img_file:
|
10 |
+
img_file.write(img.getbuffer())
|
11 |
+
return file_path
|
12 |
+
|
13 |
+
image_type = st.selectbox('Choose image type:', options=['characters', 'terrain'])
|
14 |
+
name = st.text_input('Enter a name for the image:')
|
15 |
+
uploaded_files = st.file_uploader('Upload image(s)', type=['png', 'jpg'], accept_multiple_files=True)
|
16 |
+
|
17 |
+
for uploaded_file in uploaded_files:
|
18 |
+
if uploaded_file is not None:
|
19 |
+
# Get actual image file
|
20 |
+
bytes_data = get_image_path(uploaded_file, name, image_type)
|
21 |
+
uploaded_images[image_type].setdefault(name, [])
|
22 |
+
uploaded_images[image_type][name].append(bytes_data)
|
23 |
+
st.image(bytes_data, use_column_width=True)
|
24 |
+
|
25 |
+
if image_type == 'characters':
|
26 |
+
if uploaded_images['characters']:
|
27 |
+
st.sidebar.write('**Characters**')
|
28 |
+
for name, files in uploaded_images['characters'].items():
|
29 |
+
for file in files:
|
30 |
+
st.sidebar.image(file, width=100, caption=name)
|
31 |
+
else:
|
32 |
+
if uploaded_images['terrain']:
|
33 |
+
st.write('**Terrain**')
|
34 |
+
row = []
|
35 |
+
for name, files in uploaded_images['terrain'].items():
|
36 |
+
for file in files:
|
37 |
+
row.append(file)
|
38 |
+
if len(row) == 3:
|
39 |
+
st.image(row, width=100 * 3)
|
40 |
+
row = []
|
41 |
+
if row:
|
42 |
+
st.image(row, width=100 * len(row)) # Last row, if not complete
|