File size: 5,470 Bytes
1fd8395
 
9c6079a
 
1fd8395
 
 
 
 
 
 
 
 
 
9c6079a
 
b566f5d
 
 
 
 
 
 
 
 
 
 
 
 
9c6079a
b566f5d
 
9c6079a
 
 
b566f5d
 
 
 
 
 
 
 
 
 
9c6079a
 
 
 
b566f5d
 
 
 
 
 
9c6079a
b566f5d
 
9c6079a
b566f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fd8395
 
 
 
 
 
 
 
b566f5d
 
 
1fd8395
b566f5d
1fd8395
 
 
 
84843fd
b566f5d
 
1fd8395
 
 
 
 
 
 
 
 
 
 
9c6079a
b566f5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e25c54a
b566f5d
 
 
 
 
 
 
 
 
 
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import streamlit as st
import os
import csv
import time

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

def update_csv_file(uploaded_file, name, image_type):
    csv_file_path = "Resources.csv"
    # read existing file contents
    file_contents = set()
    if os.path.exists(csv_file_path):
        with open(csv_file_path, mode='r') as csv_file:
            csv_reader = csv.reader(csv_file)
            for row in csv_reader:
                if row:
                    file_contents.add(tuple(row))
    # add new file content
    file_content = (name, uploaded_file.name, image_type)
    file_contents.add(file_content)
    # write updated file contents
    with open(csv_file_path, mode='w', newline='') as csv_file:
        csv_writer = csv.writer(csv_file)
        for row in file_contents:
            csv_writer.writerow(row)

def get_uploaded_files_info():
    csv_file_path = "Resources.csv"
    if os.path.exists(csv_file_path):
        with open(csv_file_path, mode='r') as csv_file:
            csv_reader = csv.reader(csv_file)
            files_info = []
            for row in csv_reader:
                if row:
                    files_info.append(row)
            return files_info
    else:
        return []

def display_images_from_csv():
    files_info = get_uploaded_files_info()
    for row in files_info:
        name, file_name, image_type = row
        # remove extension from file name
        file_name_without_extension = os.path.splitext(file_name)[0]
        if image_type == 'characters':
            img_path = f"data/uploadedImages/{image_type}/{name}/{file_name}"
            st.sidebar.image(img_path, width=100, caption=name)
        else:
            img_path = f"data/uploadedImages/{image_type}/{name}/{file_name}"
            st.image(img_path, width=100, caption=name)

def get_table_from_csv():
    csv_file_path = "Resources.csv"
    if os.path.exists(csv_file_path):
        with open(csv_file_path, mode='r') as csv_file:
            csv_reader = csv.reader(csv_file)
            table_rows = [row for row in csv_reader if row]
            # create markdown table from table rows
            table_md = "| Name | File Name | Image Type |\n| --- | --- | --- |\n"
            for row in table_rows:
                name, file_name, image_type = row
                # remove extension from file name
                file_name_without_extension = os.path.splitext(file_name)[0]
                table_md += f"| {name} | {file_name_without_extension} | {image_type} |\n"
            return table_md
    else:
        return ""

# show uploaded images and save them to file
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)
    update_csv_file(uploaded_file, name, image_type)

def display_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)

def display_terrain():
    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

def display_uploaded_images():
    st.write("## Uploaded Images")
    if image_type == 'characters':
        display_characters()
    else:
        display_terrain()

    st.write("## Uploaded Images Table")
    # display markdown table from csv file
    table_md = get_table_from_csv()
    if table_md:
        st.markdown(table_md, unsafe_allow_html=True)
    else:
        st.write("No images uploaded yet.")

    while True:
        time.sleep(20)
        st.empty()
        display_images_from_csv()


# show uploaded images and save them to file
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(key="part2", '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)
        update_csv_file(uploaded_file, name, image_type)

display_uploaded_images()