-
Notifications
You must be signed in to change notification settings - Fork 1
/
robodevmy_generator.py
158 lines (126 loc) · 3.89 KB
/
robodevmy_generator.py
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
146
147
148
149
150
151
152
153
154
155
156
157
158
from PIL import Image
from IPython.display import display
import random
import json
import os
# Each image is made up a series of traits
# The weightings for each trait drive the rarity and add up to 100%
face = ["White"]
face_weights = [100]
eyes = ["eye1", "eye2", "eye3", "eye4", "eye5", "eye6", "eye7", "eye8", "eye9", "eye10"]
eyes_weights = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
hair = ['hair1', 'hair2', 'hair3', 'hair4', 'hair5', 'hair6', 'hair7', 'hair8', 'hair9', 'hair10']
hair_weights = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
mouth = ['m1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8', 'm9', 'm10']
mouth_weights = [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
# Dictionary variable for each trait.
# Eech trait corresponds to its file name
face_files = {
"White": "face1"
}
eyes_files = {
"eye1": "eye1",
"eye2": "eye2",
"eye3": "eye3",
"eye4": "eye4",
"eye5": "eye5",
"eye6": "eye6",
"eye7": "eye7",
"eye8": "eye8",
"eye9": "eye9",
"eye10": "eye10"
}
hair_files = {
"hair1": "hair1",
"hair2": "hair2",
"hair3": "hair3",
"hair4": "hair4",
"hair5": "hair5",
"hair6": "hair6",
"hair7": "hair7",
"hair8": "hair8",
"hair9": "hair9",
"hair10": "hair10"
}
mouth_files = {
"m1": "m1",
"m2": "m2",
"m3": "m3",
"m4": "m4",
"m5": "m5",
"m6": "m6",
"m7": "m7",
"m8": "m8",
"m9": "m9",
"m10": "m10"
}
## Generate Traits
TOTAL_IMAGES = 100 # Number of random unique images we want to generate
all_images = []
# A recursive function to generate unique image combinations
def create_new_image():
new_image = {} #
# For each trait category, select a random trait based on the weightings
new_image ["Face"] = random.choices(face, face_weights)[0]
new_image ["Eyes"] = random.choices(eyes, eyes_weights)[0]
new_image ["Hair"] = random.choices(hair, hair_weights)[0]
new_image ["Mouth"] = random.choices(mouth, mouth_weights)[0]
if new_image in all_images:
return create_new_image()
else:
return new_image
# Generate the unique combinations based on trait weightings
for i in range(TOTAL_IMAGES):
new_trait_image = create_new_image()
all_images.append(new_trait_image)
# Returns true if all images are unique
def all_images_unique(all_images):
seen = list()
return not any(i in seen or seen.append(i) for i in all_images)
print("Are all images unique?", all_images_unique(all_images))
# Add token Id to each image
i = 0
for item in all_images:
item["tokenId"] = i
i = i + 1
print(all_images)
# Get Trait Counts
face_count = {}
for item in face:
face_count[item] = 0
eyes_count = {}
for item in eyes:
eyes_count[item] = 0
hair_count = {}
for item in hair:
hair_count[item] = 0
mouth_count = {}
for item in mouth:
mouth_count[item] = 0
for image in all_images:
face_count[image["Face"]] += 1
eyes_count[image["Eyes"]] += 1
hair_count[image["Hair"]] += 1
mouth_count[image["Mouth"]] += 1
print(face_count)
print(eyes_count)
print(hair_count)
print(mouth_count)
#### Generate Images
os.makedirs("./images/")
for item in all_images:
im1 = Image.open(f'./face_parts/face/{face_files[item["Face"]]}.png').convert('RGBA')
im2 = Image.open(f'./face_parts/eyes/{eyes_files[item["Eyes"]]}.png').convert('RGBA')
im2 = im2.resize(im1.size)
im3 = Image.open(f'./face_parts/hair/{hair_files[item["Hair"]]}.png').convert('RGBA')
im3 = im3.resize(im1.size)
im4 = Image.open(f'./face_parts/mouth/{mouth_files[item["Mouth"]]}.png').convert('RGBA')
im4 = im4.resize(im1.size)
#Create each composite
com1 = Image.alpha_composite(im1, im2)
com2 = Image.alpha_composite(com1, im3)
com3 = Image.alpha_composite(com2, im4)
#Convert to RGB
rgb_im = com3.convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)