forked from Jon-Becker/nft-generator-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.py
155 lines (141 loc) · 5.54 KB
/
index.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
from IPython.display import display
from PIL import Image
import random
import json
import os
os.system('cls' if os.name=='nt' else 'clear')
def create_new_image(all_images, config):
new_image = {}
for layer in config["layers"]:
new_image[layer["name"]] = random.choices(layer["values"], layer["weights"])[0]
for incomp in config["incompatibilities"]:
for attr in new_image:
if new_image[incomp["layer"]] == incomp["value"] and new_image[attr] in incomp["incompatible_with"]:
return create_new_image(all_images, config)
if new_image in all_images:
return create_new_image(all_images, config)
else:
return new_image
def generate_unique_images(amount, config):
print("Generating {} unique NFTs...".format(amount))
pad_amount = len(str(amount));
trait_files = {
}
for trait in config["layers"]:
trait_files[trait["name"]] = {}
for x, key in enumerate(trait["values"]):
trait_files[trait["name"]][key] = trait["filename"][x];
all_images = []
for i in range(amount):
new_trait_image = create_new_image(all_images, config)
all_images.append(new_trait_image)
i = 1
for item in all_images:
item["tokenId"] = i
i += 1
for i, token in enumerate(all_images):
attributes = []
for key in token:
if key != "tokenId":
attributes.append({"trait_type": key, "value": token[key]})
token_metadata = {
"image": config["baseURI"] + "/images/" + str(token["tokenId"]) + '.png',
"tokenId": token["tokenId"],
"name": config["name"] + str(token["tokenId"]).zfill(pad_amount),
"description": config["description"],
"attributes": attributes
}
with open('./metadata/' + str(token["tokenId"]) + '.json', 'w') as outfile:
json.dump(token_metadata, outfile, indent=4)
with open('./metadata/all-objects.json', 'w') as outfile:
json.dump(all_images, outfile, indent=4)
for item in all_images:
layers = [];
for index, attr in enumerate(item):
if attr != 'tokenId':
layers.append([])
layers[index] = Image.open(f'{config["layers"][index]["trait_path"]}/{trait_files[attr][item[attr]]}.png').convert('RGBA')
if len(layers) == 1:
rgb_im = layers[0].convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)
elif len(layers) == 2:
main_composite = Image.alpha_composite(layers[0], layers[1])
rgb_im = main_composite.convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)
elif len(layers) >= 3:
main_composite = Image.alpha_composite(layers[0], layers[1])
layers.pop(0)
layers.pop(0)
for index, remaining in enumerate(layers):
main_composite = Image.alpha_composite(main_composite, remaining)
rgb_im = main_composite.convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)
# v1.0.2 addition
print("\nUnique NFT's generated. After uploading images to IPFS, please paste the CID below.\nYou may hit ENTER or CTRL+C to quit.")
cid = input("IPFS Image CID (): ")
if len(cid) > 0:
if not cid.startswith("ipfs://"):
cid = "ipfs://{}".format(cid)
if cid.endswith("/"):
cid = cid[:-1]
for i, item in enumerate(all_images):
with open('./metadata/' + str(item["tokenId"]) + '.json', 'r') as infile:
original_json = json.loads(infile.read())
original_json["image"] = original_json["image"].replace(config["baseURI"]+"/", cid+"/")
with open('./metadata/' + str(item["tokenId"]) + '.json', 'w') as outfile:
json.dump(original_json, outfile, indent=4)
generate_unique_images(120, {
"layers": [
{
"name": "Background",
"values": ["Blue", "Orange", "Purple", "Red", "Yellow"],
"trait_path": "./trait-layers/backgrounds",
"filename": ["blue", "orange", "purple", "red", "yellow"],
"weights": [20,20,20,20,20]
},
{
"name": "Base",
"values": ["Base"],
"trait_path": "./trait-layers/base",
"filename": ["base"],
"weights": [100]
},
{
"name": "Eyes",
"values": ["Eyes 1", "Eyes 2", "Eyes 3", "Eyes 4 ", "Eyes 5"],
"trait_path": "./trait-layers/eyes",
"filename": ["eyes1", "eyes2", "eyes3", "eyes4", "eyes5"],
"weights": [20, 20, 20, 20, 20]
},
{
"name": "Mouth",
"values": ["Mouth 1", "Mouth 2", "Mouth 3", "Mouth 4 ", "Mouth 5"],
"trait_path": "./trait-layers/mouth",
"filename": ["mouth1", "mouth2", "mouth3", "mouth4", "mouth5"],
"weights": [20, 20, 20, 20, 20]
},
{
"name": "Body",
"values": ["Body 1", "Body 2", "Body 3", "Body 4 ", "Body 5"],
"trait_path": "./trait-layers/body",
"filename": ["body1", "body2", "body3", "body4", "body5"],
"weights": [20, 20, 20, 20, 20]
},
{
"name": "Hair",
"values": ["Hair 1", "Hair 2", "Hair 3", "Hair 4 ", "Hair 5"],
"trait_path": "./trait-layers/hair",
"filename": ["hair1", "hair2", "hair3", "hair4", "hair5"],
"weights": [20, 20, 20, 20, 20]
}
],
"incompatibilities": [],
"baseURI": ".",
"name": "Stickman bof",
"description": "This is the first collection created by Abdelkarim Andolerzak."
})
#Additional layer objects can be added following the above formats. They will automatically be composed along with the rest of the layers as long as they are the same size as eachother.
#Objects are layered starting from 0 and increasing, meaning the front layer will be the last object. (Branding)