-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
144 lines (102 loc) · 2.96 KB
/
generate.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
import os
import pathlib
import random
import json
from PIL import Image
#data
id = 'image_'
imgType = '.png'
# layers = os.listdir('layers')
layers = ['layer1','layer2','layer3','layer4'] #arrange layers bottom to top
#template
print('')
print('=========================================')
print('===== NFT GENERATOR [BY CHAMOD] =========')
print('=========================================')
print('')
os.system ('pause')
print('')
#load layers
print('### loading layers ####')
print('')
count = len(layers)
total = 1
TraitCounts = []
traits = []
#Load traits and calculate total possible NFTs
print('### Traits ###')
for i in range(count):
trait = os.listdir('layers/'+layers[i])
traits.append(trait)
TraitCount = len(trait)
TraitCounts.append(TraitCount-1) #list which contains count of traits in every layer
total = total*TraitCount
print(layers[i]+' =',trait)
print('')
print('Total Possible NFTs = ', total)
print('')
# input no of NFTs
while True:
try:
nftReq= int(input("Enter the required no of NFTs : "))
if nftReq <= total :
break
else:
print("Please input integer below than 'Total Possible NFTs'")
print('')
except ValueError:
print("Please input integer below than 'Total Possible NFTs'")
print('')
continue
##### generate NFT #####
#load generated combinations
# jsonContent = open("combinations.json", "r").read() #read JSON
# combinations = json.loads(jsonContent)
# comb_Count = len(combinations)
combinations = []
#get combinations
print('')
print('### Randomizing ###')
print('')
x = 0
while x < nftReq:
combination = []
for i in range(count):
gen = random.randint(0, TraitCounts[i])
combination.append(gen)
if combination not in combinations:
combinations.append(combination)
x = x + 1
# print(combinations)
#save combinations
# with open('combinations.json', 'w') as outfile:
# json.dump(combinations, outfile , indent=3)
#generate NFT
print('### Generating NFTs###')
print('')
nftIDlist = []
for y in range(nftReq):
#initial background
nft = Image.open(f'./layers/'+layers[0]+'/'+traits[0][0]).convert('RGBA')
nft.putalpha(0)
for i in range(count):
img = Image.open(f'./layers/'+layers[i]+'/'+traits[i][combinations[y][i]]).convert('RGBA')
nft = Image.alpha_composite(nft , img)
id = 'image_' + "".join(map(str,combinations[y]))
nftName = id + imgType
nftIDlist.append(id)
nft.save("NFT/" + nftName)
print(y+1)
#generate MetaData
print('')
print('### Generating MetaData###')
for z in range(nftReq):
MetaData = {}
MetaData['NFT name/ID'] = nftIDlist[z]
for i in range(count):
traitName = 'layers/' + layers[i]+'/'+ traits[i][combinations[z][i]]
MetaData[layers[i]] = pathlib.Path(traitName).stem
with open('MetaData/'+nftIDlist[z]+'.json', 'w') as outfile:
json.dump(MetaData, outfile , indent=4)
print('')
print('### Done ###')