Develop an nft image generator that uses a set of layers to generate a series of unique images

  1. Install Python

Download link: https://www.python.org/downloads/

  1. Install PIP Download PIP get-pip.py
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
python get-pip.py
  1. Install Python Pillow
pip install pillow
  1. Install Python display
pip install display
  1. Install Jupyter Notebook
pip install jupyter

Folder structure

image.png

  1. Run Jupyter in the project directory
jupyter notebook
  1. Create a new notebook
from PIL import Image
from IPython.display import display
import random
import json
  1. Inject all shapes and set their weights

# Each image is composed of a series of features
# The weight of each feature determines the rarity, which adds up to 100%

background = ["Blue", "Orange"]
background_weights = [30, 40]

circle = ["Blue", "Orange"]
circle_weights = [30, 15]

square = ["Blue","Orange"]
square_weights = [30, 15]

# Dictionary variables for each feature.
# Eech trait corresponds to its file name
# Add more shapes and colors as needed

background_files = {
    "Blue": "blue",
    "Orange": "orange",
}

square_files = {
    "Blue": "blue-square",
    "Orange": "orange-square",
}

circle_files = {
    "Blue": "blue-circle",
    "Orange": "orange-circle",
}
  1. Create a function to generate unique image combinations
TOTAL_IMAGES = 30 #The number of random unique images we want to generate

all_images = []

def create_new_image():

    new_image = {} #

    #For each feature category, select a random feature according to the weight
    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


#Generate unique combinations based on feature weights
for i in range(TOTAL_IMAGES):

    new_trait_image = create_new_image()

    all_images.append(new_trait_image)
  1. If all images are unique, return 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))
  1. Add a token ID for each image
i = 0
for item in all_images:
    item["tokenId"] = i
    i = i + 1
  1. Print all images
print(all_images)
  1. Get feature count
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)
  1. Generate metadata for all features
METADATA_FILE_NAME ='./metadata/all-traits.json';
with open(METADATA_FILE_NAME,'w') as outfile:
    json.dump(all_images, outfile, indent=4)
  1. Generate image
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.Generate metadata for each image

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()

It outputs all generated images to the /images folder and metadata to the /metadata folder. The file name will refer to tokenIds

Likes(1)

Comment list count 0 Comments

No Comments

WeChat Self-Service

WeChat Consult

TaoBao

support@elephdev.com

发表
评论
Go
Top