Generate 500+ NFTs using Python PIL

TechJD
2 min readMar 25, 2022

--

I’m a visual learner, so it’s beneficial for me to see things in action and not just in lines of code. That’s why I came up with a quick little exercise to knock the dust off my python skills as well as help me generate test NFTs quickly. I used python to rapidly generate 500+ images from a list of hexadecimal values of colors. With Python Imaging Library (PIL), there’s no limit to how much you can quickly generate if you need to. Let’s get started!

For this tutorial, I’m going to assume you have python already installed. Now install Pillow.

pip install Pillow

Note: You may have to try pip3 instead. Next, get a list of hexadecimal colors and save them to a .txt file. I found mine here.

Next, start a new .py file and begin it like this:

from PIL import Imagefolder = 'colors/'

Next, read the .txt file you made:

f = open('hexcolors.txt', "r")lines = f.readlines()

Check to see if the data looks right:

print(lines)

If you used the list from the site I linked to, then you will have to strip the extra newlines (‘\n’). We do this with the strip() function, placed inside a good ol’ for-in loop. Next, you have to nest another for-in loop to create a new Image for each. Set the mode, size, and color as seen below. Then, save that image to the colors folder.

for line in lines:    color = line.strip()    for img in color:        img = Image.new(mode='RGB',size=(200,200),color=color)        img.save(folder+color+".jpg")

Perfect! We’ve got 500+ test images to test our NFT contracts against. Maybe I should go try to sell this collection :P

--

--

TechJD

Law, programming, and everything in-between! Coming up with fun coding projects with real-world application.