avatarK. N

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3866

Abstract

m/v2/resize:fit:800/1*[email protected]"><figcaption></figcaption></figure><figure id="377d"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*[email protected]"><figcaption></figcaption></figure><figure id="c298"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*[email protected]"><figcaption></figcaption></figure><p id="e120">Now we will write code that will combine these layers into unique images.</p><p id="00af"><b>The Code</b></p><p id="6b6a">The following chart represents the flow of things and directory structure in our code:</p><figure id="2173"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*1RNhv8Uhe4m_Ouwtq-SDAg.png"><figcaption></figcaption></figure><p id="d3db">The simplest way is to use a numbering system for filenames. We can create folders for each layers and name them as background, object and label. Each folder will have image files with each variation names with a number. Since we have 3 variations for each layer, 1, 2 and 3.</p><p id="c547">Lets define some parameters.</p><div id="776c"><pre><span class="hljs-attribute">variations</span> <span class="hljs-operator">=</span> <span class="hljs-number">3</span></pre></div><div id="ab36"><pre><span class="hljs-attr">images_count</span> = <span class="hljs-number">10</span></pre></div><div id="fa93"><pre><span class="hljs-attribute">layers</span> <span class="hljs-operator">=</span> <span class="hljs-number">3</span></pre></div><p id="3677">Remember that the maximum number of unique images you can have depends on the number of variations. You can calculate that by multiplying the number of variations of each layer. Since we have 3 layers and 3 variations in each layer, we can calculate the maximum count of unique images as:</p><div id="da17"><pre><span class="hljs-attr">3x3x3</span> = <span class="hljs-number">27</span></pre></div><p id="be7d">We will pick a random number from 1 to the value of variations for each layer and join them into a string that represents a filename and add it to a list. Each number in this string represents a variation of a layer. So if the string is 231, it would mean file 2 from layer 1 folder, file 3 from layer 2 folder and file 1 from layer 3 folder.</p><div id="1c69"><pre>#<span class="hljs-keyword">Function</span> <span class="hljs-keyword">to</span> generate a filename, <span class="hljs-keyword">with</span> number <span class="hljs-keyword">of</span> characters same <span class="hljs-keyword">to</span> #number <span class="hljs-keyword">of</span> layers, <span class="hljs-keyword">each</span> character <span class="hljs-built_in">is</span> a number, randomly selected #<span class="hljs-keyword">from</span> <span class="hljs-number">1</span> <span class="hljs-keyword">to</span> the number <span class="hljs-keyword">of</span> variations <span class="hljs-keyword">in</span> <span class="hljs-keyword">each</span> layer.</pre></div><div id="ecb7"><pre>def generate_filename(): <span class="hljs-keyword">name</span>=”” for i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(layers): <span class="hljs-keyword">name</span>=<span class="hljs-keyword">name</span>+str(randint(<span class="hljs-number">1</span>,variations)) <span class="hljs-keyword">return</span> <span class="hljs-keyword">name</span></pre></div><div id="117c"><pre><span class="hljs-comment">#List that will contain the filenames.</span></pre></div><div id="9b46"><pre><span class="hljs-attribute">files</span><span class="hljs-operator">=</span>[]</pre></div><div id="7d3e"><pre><span class="hljs-comment">#Now dependent on how many images we want, we call the function to</span> <span class="hljs-comment">#generate a filename and add it to out list if a similar filename </span> <span class="hljs-comment">#doe

Options

snt exist, this way we make sure all filenames are unique.</span></pre></div><div id="e25e"><pre>for i <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(images_count): <span class="hljs-keyword">name</span>=generate_filename() <span class="hljs-keyword">if</span> <span class="hljs-keyword">name</span> not <span class="hljs-keyword">in</span> files: files.append(<span class="hljs-keyword">name</span>)</pre></div><div id="4fd3"><pre><span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image <span class="hljs-keyword">from</span> IPython.display <span class="hljs-keyword">import</span> display</pre></div><div id="b764"><pre><span class="hljs-attribute">for</span> item in files: <span class="hljs-comment"># loading the image from each layer</span> <span class="hljs-attribute">layer1</span>=Image.open(f’./<span class="hljs-number">1</span>/{item[<span class="hljs-number">0</span>]}.png’).convert(‘RGBA’) <span class="hljs-attribute">layer2</span>=Image.open(f’./<span class="hljs-number">2</span>/{item[<span class="hljs-number">1</span>]}.png’).convert(‘RGBA’) <span class="hljs-attribute">layer3</span>=Image.open(f’./<span class="hljs-number">3</span>/{item[<span class="hljs-number">2</span>]}.png’).convert(‘RGBA’)</pre></div><div id="8f99"><pre># Now combining the layers

<span class="hljs-built_in">com1</span> = Image.alpha_composite(layer1, layer2)
<span class="hljs-built_in">com2</span> = Image.alpha_composite(<span class="hljs-built_in">com1</span>, layer3)

rgb_im = <span class="hljs-built_in">com2</span>.<span class="hljs-built_in">convert</span>(‘RGB’)
file_name = item + “.png”
rgb_im.save(“./images/” + file_name)</pre></div><p id="5ce9">Following are some of the images generated:</p><figure id="15b3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*odqGjmf1QYR6G1k0GvwqYg.png"><figcaption></figcaption></figure><figure id="f579"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Gx8yEvHleiv7tegsMPq2ug.png"><figcaption></figcaption></figure><figure id="4db2"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*upi8LT8dNL-fEgH3EHyJ0w.png"><figcaption></figcaption></figure><figure id="f16e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*KeViaFwk9EiM5C5EyUh0_A.png"><figcaption></figcaption></figure><figure id="e066"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*smo1tGfFn0pWmwuzXL5eKQ.png"><figcaption></figcaption></figure><figure id="662e"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*gxkrw0cbbDPOLOE4MxzPvQ.png"><figcaption>Images generated by our code</figcaption></figure><p id="39e9">Obviously these images don’t make sense but the idea is to show how to generate images by combining various layers.</p><p id="6cce"><b>Things to remember</b></p><p id="3444">Files have to be named numerically, so each layer folder should have files names as 1.png, 2.png, 3.png so on.</p><p id="5fba">You can modify the code as to whatever number of layers and variations of your choice.</p><p id="e03a">Make sure the background is transparent.</p><p id="10b3">Layers are loaded in sequence of what’s specified in the code. The last layer is on top of the previous layer so if your image doesn’t seem right, you may want to look at the sequence in which you’re loading the layers.</p><p id="bb72">You can set the number of images to lower number initially to experiment.</p><p id="8c4e">Please feel free to provide feedback, I will be happy to clarify/improve if I can, let’s learn together!</p><p id="9551">Please do check out my <a href="https://knawab.medium.com/generate-nft-images-metadata-in-bulk-with-python-e8820bb2b807">updated article</a>, it goes over generating metadata along with the files.</p></article></body>

Generate Thousands of NFT Images With Python

(This article goes over generating images only, but if you want to know how to generate metadata along with the image, please check out my newer article here. Also, after writing these articles and learning more, I went ahead and built a small website using the same concept to let users generate their collection. For now, I have limited it to 1000 images, but hoping to continue to work on it if there are enough visits. Please do check it out!)

NFTs have opened up a whole new playground for artists, graphic designers and crypto-enthusiasts all together. In this tutorial, we will learn how to use python to generate an NFT collection containing a large number of unique images.

Layers are the key

The approach is simple, you create the image in layers and then write code to generate images by randomly picking the layers and combining them. For example, if you want to create an NFT collection containing a cartoon figure, you can create different layers with each containing a component of the image, say one for background, one for face, one for hair, one for shirt and so on. Then for each layer, you create different variations of each layer, say for hair, you have different color variations, similarly, different variations of the skin tone in the face layer and then different colors for the background layer. Your code then picks a variation randomly for each layer and combines them into an image. The process if repeat again and again each time generation a new image.

Creating the layers

You have many options for creating the layers. The easiest I found was to use my iPad with a pen and draw it. However, any application on any platform that can handle .PNG files should be good. You can start by finding an image on google images. You can try adding keywords like “transparent png”, that will help in finding images where there’s no background data. Another useful (with a free basic version) is this website remove.bg, where you can upload an image, it will remove the background and let you download a transparent PNG. You can then use your application to create different variations of the layer.

The Strategy

The basic idea is that you are going to mix the layers to generate unique images. For example, you want to have a 3 layers image with 3 variations of each layer. So layer 1 is the background, say you have 3 colors, as shown below.

Layer 2 contains an object, a mask, say you have 3 variations,

Layer 3 contains another object, a hat, and has 3 variations as shown below:

Now we will write code that will combine these layers into unique images.

The Code

The following chart represents the flow of things and directory structure in our code:

The simplest way is to use a numbering system for filenames. We can create folders for each layers and name them as background, object and label. Each folder will have image files with each variation names with a number. Since we have 3 variations for each layer, 1, 2 and 3.

Lets define some parameters.

variations = 3
images_count = 10
layers = 3

Remember that the maximum number of unique images you can have depends on the number of variations. You can calculate that by multiplying the number of variations of each layer. Since we have 3 layers and 3 variations in each layer, we can calculate the maximum count of unique images as:

3x3x3 = 27

We will pick a random number from 1 to the value of variations for each layer and join them into a string that represents a filename and add it to a list. Each number in this string represents a variation of a layer. So if the string is 231, it would mean file 2 from layer 1 folder, file 3 from layer 2 folder and file 1 from layer 3 folder.

#Function to generate a filename, with number of characters same to 
#number of layers, each character is a number, randomly selected 
#from 1 to the number of variations in each layer.
def generate_filename():
    name=””
    for i in range(layers):
         name=name+str(randint(1,variations))
    return name
#List that will contain the filenames.
files=[]
#Now dependent on how many images we want, we call the function to
#generate a filename and add it to out list if a similar filename 
#doesnt exist, this way we make sure all filenames are unique.
for i in range(images_count):
    name=generate_filename()
    if name not in files:
         files.append(name)
from PIL import Image 
from IPython.display import display
for item in files:
# loading the image from each layer
    layer1=Image.open(f’./1/{item[0]}.png’).convert(‘RGBA’)
    layer2=Image.open(f’./2/{item[1]}.png’).convert(‘RGBA’)
    layer3=Image.open(f’./3/{item[2]}.png’).convert(‘RGBA’)
# Now combining the layers
 
    com1 = Image.alpha_composite(layer1, layer2)
    com2 = Image.alpha_composite(com1, layer3)
 
    rgb_im = com2.convert(‘RGB’)
    file_name = item + “.png”
    rgb_im.save(“./images/” + file_name)

Following are some of the images generated:

Images generated by our code

Obviously these images don’t make sense but the idea is to show how to generate images by combining various layers.

Things to remember

Files have to be named numerically, so each layer folder should have files names as 1.png, 2.png, 3.png so on.

You can modify the code as to whatever number of layers and variations of your choice.

Make sure the background is transparent.

Layers are loaded in sequence of what’s specified in the code. The last layer is on top of the previous layer so if your image doesn’t seem right, you may want to look at the sequence in which you’re loading the layers.

You can set the number of images to lower number initially to experiment.

Please feel free to provide feedback, I will be happy to clarify/improve if I can, let’s learn together!

Please do check out my updated article, it goes over generating metadata along with the files.

Nft
Cryptocurrency
Python
Programming
Graphic Design
Recommended from ReadMedium
avatarUmer Waqas | Python Developer | AI Writer | Saas
How to use OpenAI vision API for bulk images?

4 min read