0% found this document useful (0 votes)
8 views2 pages

Generate

The document is a Python script designed to generate a specified number of SVG images by combining various attributes from predefined folders. It includes functionality to load SVG files, randomly select attributes based on inclusion chances, and save the generated images along with their metadata in a CSV file. The script also cleans up any previously generated images before creating new ones.

Uploaded by

slayter.amery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views2 pages

Generate

The document is a Python script designed to generate a specified number of SVG images by combining various attributes from predefined folders. It includes functionality to load SVG files, randomly select attributes based on inclusion chances, and save the generated images along with their metadata in a CSV file. The script also cleans up any previously generated images before creating new ones.

Uploaded by

slayter.amery
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import glob

import os
import random
import re

BASE = 'BASE'

# CHANGE this if you want more images


IMAGES_TO_GENERATE = 1000

# 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)

attributes = ['backgrounds', 'weather', 'frames', 'horns', BASE, 'mouths', 'eyes',


'hairs', 'earings', 'bodymods',
'clothes', 'accessories']

# attributes that are not mandotary


optional = {'accessories', 'backgrounds', 'bodymods', 'clothes',
'earings', 'frames', 'hairs', 'weather'}

# 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

# name of images in each folder #


# feel free to change names to something meaningful, right now most of them are
*Artboard.svg
# the name of used images will be printed in metadata.csv file
options = [load_options(a) for a in attributes]

opening_tag = '<svg xmlns:xlink="http://www.w3.org/1999/xlink"


xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 1000">'
closing_tag = '</svg>'

with open("./images/base.svg", "r") as f:


base_svg = f.read()

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)

print('Total number of possible images:', number_of_images)

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())

return opening_tag+image+closing_tag, csv_entry.rstrip(',')

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()

You might also like