Suck the cat with code! This paper is participating in[Cat Essay Campaign].

Batch generation unique NFT cat map, this project wang Duoyu will vote?

Zero, preface,

Our project is based on the NFT-image-Generator written by Benyamin Ahmed’s children. The teen made 2.5 million yuan in a summer holiday selling NFT whale memes, weird- Whales Images. In addition, he also open source the NFT picture generator, today we are based on his open source tool to generate cat emoticons.

First, set the weight of the composite parts

cat = ["01"."."."3"."04"."5"."6"."7"]
cat_weights = [10.40.15.5.10.10.10]
​
title = ["01"."."."3"."04"]
title_weights = [20.40.15.15]
Copy the code

We have 7 and 4 cat pictures here. We combine these seven cats with four cats to create a unique image.

P.S. the weights add up to 100.

2. Name of the file corresponding to each picture

cat_files = {
    "01": "cat1".".": "cat2"."3": "cat3"."04": "cat4"."5": "cat5"."6": "cat6"."7": "cat7"
}
​
title_files = {
    "01": "title01".".": "title02"."3": "title03"."04": "title04"
}
​
Copy the code

We are putting the file name in the folder into the dictionary.

Thirdly, the image combination based on weight is generated

TOTAL_IMAGES = 10 # Number of random unique images we want to generate
​
all_images = [] 
​
# A recursive function to generate unique image combinations
def create_new_image() :
    
    new_image = {} #
​
    # For each trait category, select a random trait based on the weightings 
​
    new_image ["Cat"] = random.choices(cat, cat_weights)[0]
    new_image ["Title"] = random.choices(title, title_weights)[0]
    
    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)
    
Copy the code

The TOTAL_IMAGES variable is the maximum number of images we want to generate.

Create_new_image is a recursion that randomly selects an image for each image category based on its weight.

Judge whether all the pictures are unique

def all_images_unique(all_images) :
    seen = list(a)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))
Copy the code

Here we go through all the images to see if there are duplicate images.

5. Add Token Id to each image

# Add token Id to each image
i = 0
for item in all_images:
    item["tokenId"] = i
    i = i + 1
print(all_images)
Copy the code

After that, all_images will read:

[{'Cat': '05'.'Title': '01'.'tokenId': 0}, {'Cat': '02'.'Title': '04'.'tokenId': 1}, {'Cat': '01'.'Title': '03'.'tokenId': 2}, {'Cat': '01'.'Title': '02'.'tokenId': 3}, {'Cat': '02'.'Title': '03'.'tokenId': 4}, {'Cat': '07'.'Title': '02'.'tokenId': 5}, {'Cat': '02'.'Title': '02'.'tokenId': 6}, {'Cat': '07'.'Title': '01'.'tokenId': 7}, {'Cat': '03'.'Title': '02'.'tokenId': 8}, {'Cat': '05'.'Title': '02'.'tokenId': 9}]
Copy the code

Contains pictures of tokenId and two types of cats.

Six, get the number of pictures used

# Get Trait Counts
​
    
title_count = {}
for item in title:
    title_count[item] = 0
​
cat_count = {}
for item in cat:
    cat_count[item] = 0for image in all_images:
    cat_count[image["Cat"]] + =1
    title_count[image["Title"]] + =1
    
print(cat_count)
print(title_count)
Copy the code

Use a dictionary to store the number of times each image has been used.

7. Make up pictures

#### Generate Images
for item in all_images:
​
    im1 = Image.open(f'./trait-layers/cat/{cat_files[item["Cat"]]}.png').convert('RGBA')
    im2 = Image.open(f'./trait-layers/title/{title_files[item["Title"]]}.png').convert('RGBA')
    
    im1 = im1.resize((400.400))
    im2 = im2.resize((400.400))
    #Create each composite
​
    com = Image.alpha_composite(im1, im2)
    
    #Convert to RGB
    rgb_im = com.convert('RGB')
    file_name = str(item["tokenId"]) + ".png"
    rgb_im.save("./images/" + file_name)
    
Copy the code

The idea here is simple, convert the Image to the same size, overlay it with image.alpha_composite, and then save the Image against tokenId.

Take a look at the unique images I created