-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnft-generator.py
executable file
·137 lines (85 loc) · 2.74 KB
/
nft-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
#!/usr/bin/env python
# coding: utf-8
# In[104]:
# code based on https://betterprogramming.pub/create-your-own-nft-collection-with-python-82af40abf99f
# original code in folder
from PIL import Image
from IPython.display import display
import random
import json
import os
# In[105]:
##GLOBAL CONFIG
assetsFolder = 'assets'
TOTAL_IMAGES = 100 # Number of random unique images we want to generate
# In[106]:
# Search for files in assets folder tree (carefful, use only png images here)
folderContent = os.listdir('./' + assetsFolder)
folder = {}
for f in folderContent:
folder[f] = os.listdir('./' + assetsFolder + '/' + f)
# In[107]:
# Generate Traits
all_images = []
# A recursive function to generate unique image combinations
def create_new_image():
new_image = {} #
for i in folder:
new_image[i] = random.choices(folder[i])[0]
# For each trait category, select a random trait based on the weightings
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)
# print(all_images)
# In[108]:
# 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
# In[109]:
# Get Trait Counts
counts = {}
for image in all_images:
for i in folder:
counts[i] = 0
for image in all_images:
for i in folder:
counts[i] += 1
# In[110]:
#### Generate Images
# Check whether the specified path exists or not
isExist = os.path.exists(f'./images_' + assetsFolder)
if not isExist:
# Create a new directory because it does not exist
os.mkdir(f'./images_' + assetsFolder)
print("The new directory is created!")
images_img = {}
for item in all_images:
for i in folder:
# print(f'./{assetsFolder}/{i}/{item[i]}')
images_img[i] = Image.open(f'./{assetsFolder}/{i}/{item[i]}').convert('RGBA')
# Create each composite
for ii in images_img:
if 'com' in locals():
com = Image.alpha_composite(com, images_img[ii])
else:
com = Image.alpha_composite(images_img[ii], images_img[ii])
# Convert to RGB
rgb_im = com.convert('RGBA')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save('./images_' + assetsFolder + '/' + file_name)
# list images from the directory
#folderImg = os.listdir('./images_' + assetsFolder)
#for f in folderImg:
# print(f)