开发一个 nft 图像生成器,使用它一组图层生成一系列独特的图像
1.安装Python
下载链接:https://www.python.org/downloads/
2.安装 PIP 下载 PIP get-pip.py
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
3.安装 Python Pillow
pip install pillow
4.安装 Python display
pip install display
5.安装 Jupyter Notebook
pip install jupyter
文件夹结构
6.在项目目录下运行 Jupyter
jupyter notebook
7.创建一个新的 notebook
from PIL import Image
from IPython.display import display
import random
import json
8.注入所有形状并设置它们的权重
# 每个图像由一系列特征组成
# 每个特征的权重决定了稀有度,加起来为 100%
background = ["Blue", "Orange"]
background_weights = [30, 40]
circle = ["Blue", "Orange"]
circle_weights = [30, 15]
square = ["Blue","Orange"]
square_weights = [30, 15]
# 每个特征的字典变量。
# Eech trait 对应其文件名
# 根据需要添加更多形状和颜色
background_files = {
"Blue": "blue",
"Orange": "orange",
}
square_files = {
"Blue": "blue-square",
"Orange": "orange-square",
}
circle_files = {
"Blue": "blue-circle",
"Orange": "orange-circle",
}
9.创建一个函数来生成独特的图像组合
TOTAL_IMAGES = 30 #我们要生成的随机唯一图像的数量
all_images = []
def create_new_image():
new_image = {} #
#对于每个特征类别,根据权重选择一个随机特征
new_image ["Background"] = random.choices(background, background_weights)[0]
new_image ["Circle"] = random.choices(circle, circle_weights)[0]
new_image ["Square"] = random.choices(square, square_weights)[0]
if new_image in all_images:
return create_new_image()
else:
return new_image
#根据特征权重生成独特的组合
for i in range(TOTAL_IMAGES):
new_trait_image = create_new_image()
all_images.append(new_trait_image)
10.如果所有图像都是唯一的,则返回 true
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))
11.为每个图像添加令牌 ID
i = 0
for item in all_images:
item["tokenId"] = i
i = i + 1
12.打印所有图像
print(all_images)
13.获取特征计数
background_count = {}
for item in background:
background_count[item] = 0
circle_count = {}
for item in circle:
circle_count[item] = 0
square_count = {}
for item in square:
square_count[item] = 0
for image in all_images:
background_count[image["Background"]] += 1
circle_count[image["Circle"]] += 1
square_count[image["Square"]] += 1
print(background_count)
print(circle_count)
print(square_count)
14.为所有特征生成元数据
METADATA_FILE_NAME = './metadata/all-traits.json';
with open(METADATA_FILE_NAME, 'w') as outfile:
json.dump(all_images, outfile, indent=4)
15.生成图像
for item in all_images:
im1 = Image.open(f'./layers/backgrounds/{background_files[item["Background"]]}.jpg').convert('RGBA')
im2 = Image.open(f'./layers/circles/{circle_files[item["Circle"]]}.png').convert('RGBA')
im3 = Image.open(f'./layers/squares/{square_files[item["Square"]]}.png').convert('RGBA')
#Create each composite
com1 = Image.alpha_composite(im1, im2)
com2 = Image.alpha_composite(com1, im3)
#Convert to RGB
rgb_im = com2.convert('RGB')
file_name = str(item["tokenId"]) + ".png"
rgb_im.save("./images/" + file_name)
16.为每个图像生成元数据
f = open('./metadata/all-traits.json',)
data = json.load(f)
IMAGES_BASE_URI = "ADD_IMAGES_BASE_URI_HERE"
PROJECT_NAME = "ADD_PROJECT_NAME_HERE"
def getAttribute(key, value):
return {
"trait_type": key,
"value": value
}
for i in data:
token_id = i['tokenId']
token = {
"image": IMAGES_BASE_URI + str(token_id) + '.png',
"tokenId": token_id,
"name": PROJECT_NAME + ' ' + str(token_id),
"attributes": []
}
token["attributes"].append(getAttribute("Background", i["Background"]))
token["attributes"].append(getAttribute("Circle", i["Circle"]))
token["attributes"].append(getAttribute("Square", i["Square"]))
with open('./metadata/' + str(token_id), 'w') as outfile:
json.dump(token, outfile, indent=4)
f.close()
它将所有生成的图像输出到 /images
文件夹,并将元数据输出到 /metadata
文件夹。文件名将引用 tokenIds
发表评论 取消回复