Generate
Generate
import os
import random
import re
BASE = 'BASE'
# must match folder names inside images/parts, layers will be rendered in this
order
# it is ok to remove any of them
# add 'images' if layer "Зображення 1062" should be used (also add it to optional
if you want)
# update inlusion chances here, feel free to add new attributes or remove any of
them
include_chances = {'accessories': 10, 'backgrounds': 100, 'bodymods': 22,
'clothes': 80,
'earings': 45, 'frames': 10, 'hairs': 87, 'weather': 10}
def load_options(attribute):
files = glob.glob(f"./images/parts/{attribute}/SVG/*")
return files
def strip_svg(text):
stripped = re.sub(r'<svg.*?>', '', text)
stripped = re.sub(r'</svg>', '', stripped)
return stripped
base_svg = strip_svg(base_svg)
number_of_images = 1
for option in options:
number_of_images *= max(len(option), 1)
def generate_image():
image = ''
csv_entry = ''
for i in range(len(attributes)):
if len(options[i]) == 0:
image += base_svg
continue
exclude_chance = 100 - include_chances.get(attributes[i], 100)
chance = random.randint(0, 99)
if chance < exclude_chance:
csv_entry += ','
continue
j = random.randint(0, len(options[i])-1)
csv_entry += options[i][j].split('/')[-1].replace('.svg', '')+","
with open(options[i][j]) as file:
image += strip_svg(file.read())
def main():
dirname = os.path.dirname(__file__)
generated_dir = os.path.join(dirname, 'gen')
os.makedirs(generated_dir, exist_ok=True)
files = glob.glob(f'{generated_dir}/*')
for f in files:
os.remove(f)
csv_header_list = [
attribute for attribute in attributes if attribute != BASE]
csv_header = ','.join(['image', *csv_header_list])
csv_list = [csv_header]
for i in range(IMAGES_TO_GENERATE):
image, csv = generate_image()
csv_list.append(f'{i},{csv}')
path = os.path.join(generated_dir, f'image{i}.svg')
f = open(path, "w+")
f.write(image)
f.close()
f = open(f"{dirname}/metadata.csv", "w+")
f.write('\n'.join(csv_list))
f.close()
if __name__ == '__main__':
main()