NFT image generator

Here is an example in Python to batch generate images based on multiple layers. If you are interested in minting NFT images in a bulk, you may like to take a look at this post for efficient generation.

Concept

We are going to generate images by combining various layers of images. Let say we have individual layers about different designs of faces, eyes, mouths and heads. We prepare these images ready and save them as individual image files. Make sure you make the background transparent so that the layering can work.

We use nested loops to apply every layer of different images. The output image will be generated individully.

Very simple!

If you have 2 face designs, 2 eye designs, 2 mouth designs and 2 head designs, you will finally have 2x2x2x2 = 16 output images.

Fantastic!

Source code in Python

import os;

from os import listdir
from os.path import isfile, isdir, join

import shutil;

from PIL import Image


# Counter of all generated images
cnt = 0;

# NFT root directory
nft_home_dir = "C:/AAA/BBB/CCC";

files=[]

# Source images are designed as multiple parts.
# Image parts include folders of "Face", "Eye", "Mouth", "Head"
# The output image will be generated by combining multiple layers of images.

check_dir_face = nft_home_dir + "/Face"
files_face = listdir(check_dir_face);

check_dir_eye = nft_home_dir + "/Eye"
files_eye = listdir(check_dir_eye);

check_dir_mouth = nft_home_dir + "/Mouth"
files_mouth = listdir(check_dir_mouth);

check_dir_head = nft_home_dir + "/Head"
files_head = listdir(check_dir_head);

# Output directory
output_dir = "result";

if os.path.exists(output_dir) :
    shutil.rmtree(output_dir);

os.makedirs(output_dir);

# Log file
file_log = open("./generation_history.log", 'w');

# Start nested loops to iterate all different combination of image layers
for f_face in files_face:
    name_face = os.path.splitext(f_face)[0];
    fullpath = join(check_dir_face, f_face);

    if isfile(fullpath):
        for f_eye in files_eye:
            name_eye = os.path.splitext(f_eye)[0];
            fullpath = join(check_dir_eye, f_eye);

            if isfile(fullpath):
                for f_mouth in files_mouth:
                    name_mouth = os.path.splitext(f_mouth)[0];
                    fullpath = join(check_dir_mouth, f_mouth);

                    if isfile(fullpath):
                        for f_head in files_head:
                            name_head = os.path.splitext(f_head)[0];
                            fullpath = join(check_dir_head, f_head);

                            if isfile(fullpath):
                                # Produce the output filename
                                target_filename = name_face + "_" + name_eye + "_" + name_mouth + "_" + name_head + ".png";

                                layer_face = Image.open(check_dir_face + "/" + f_face).convert('RGBA');
                                layer_eye = Image.open(check_dir_eye + "/" + f_eye).convert('RGBA');
                                layer_mouth = Image.open(check_dir_mouth + "/" + f_mouth).convert('RGBA');
                                layer_head = Image.open(check_dir_head + "/" + f_head).convert('RGBA');

                                # Process the layer pasting from the TOP layer
                                layer_mouth.paste(layer_head, (0, 0), layer_head);
                                layer_eye.paste(layer_mouth, (0, 0), layer_mouth);
                                layer_face.paste(layer_eye, (0, 0), layer_eye);

                                # Save the output image
                                layer_face.save(output_dir + "/" + target_filename, format="png");

                                # Write log
                                file_log.write(output_dir + "/" + target_filename + "\n");
                                file_log.flush();

                                cnt = cnt + 1; # Counter increment

                                # Display console message
                                print(str(cnt) ,"outputting file", target_filename);

print("Final count =", cnt);

oliver

Leave a Reply

Your email address will not be published.