This project was intended to teach myself how to cut a sprite sheet with Python. I then decided I wanted to be able to create a spectrum of the Pokémon’s colors. The following is the tutorial on how to do it using the Emerald sprites of all the 151 Pokémon of the first generation.

First of all, you need to have all the Pokémon sprites separated in separate files. Since I didn’t have that. I used the sprite sheet on spriters-resource. Download that, remove the blue backgrounds around the sprites in a single click using GIMP’s Select By Color tool and put it in a new folder where you will later place your Python application.

To manipulate an image with Python 3, you need to install Pillow. To do so, simply enter the following in a command prompt:

pip install Pillow

We can now split our sprite sheet.

Start by creating a Python file. I’ve name mine “cut_sheet.py” but yours can be different. Then, enter the following:

from PIL import Image #This is the width of the white border that remains after removing the blue backgrounds MARGIN = 5 #Our sprites have a 64x64 resolution SPRITE_RESOLUTION = 64 #The path to our sprite sheet SHEET_FILENAME = "sheet.png" #We open the sprite sheet sheet = Image.open(SHEET_FILENAME) #Set the Pokémon number to 0 pokemon_number = 0 #Iterate through the rows for i in range(0, sheet.size[1] // (SPRITE_RESOLUTION + MARGIN)): #Iterate through the columns for j in range(0, sheet.size[0] // (SPRITE_RESOLUTION + MARGIN)): #Since the sheet has two sprite of each Pokémon and we want only the first one, we skip every second ones (odd numbers) if (j % 2 == 0): #Increase the Pokemon number by one pokemon_number = pokemon_number + 1 #Set the coordinate of the sprite x = j * (SPRITE_RESOLUTION + MARGIN) + MARGIN y = i * (SPRITE_RESOLUTION + MARGIN) + MARGIN #Select the section we want (the sprite) section = (x, y, x + 64, y + 64) #Crop it sprite = sheet.crop(section) #Save it with the Pokémon number in the filename sprite.save("pokemon_" + str(pokemon_number) + ".png")

If everything went well, your folder should now be flooded by sprite files. It’s time to create the spectrum. Create another Python application (I’ve named mine “avg_color.py”) and enter the following:

from PIL import Image, ImageDraw def countVisiblePixels(image): #Load the image pixels = image.load() #Set the counter to 0 visible_pixels = 0 #Iterate through every pixels for i in range(0, image.width): for j in range(0, image.height): #If not transparent if pixels[i, j][3] != 0: #Increment counter visible_pixels = visible_pixels + 1 return visible_pixels def createImageContainingVisiblePixels(image): #Load the image pixels = image.load() #Create the image background = (0, 0, 0, 255) visible_colors = Image.new('RGB', (1, countVisiblePixels(image)), background) draw = ImageDraw.Draw(visible_colors) #Set counter to 0 pixel_counter = 0 #Iterate through every pixels for j in range(0, image.width): for k in range(0, image.height): #If not transparent if pixels[j, k][3] != 0: #Increment counter pixel_counter = pixel_counter + 1 #Draw pixel draw.point((0, pixel_counter, 0, 0), fill=pixels[j, k]) return visible_colors def createSpectrum(width_of_stripe, height_of_stripe, number_of_stripe, avg_colors): #Background of our spectrum, it is not going to be visible anyway background = (0, 0, 0, 255) #Creating a new image for the spectrum result_image = Image.new('RGB', (number_of_stripe * width_of_stripe, height_of_stripe), background) #creating an ImageDraw so that we can draw on the spectrum draw = ImageDraw.Draw(result_image) #Iterating over the color list for i in range(0, len(avg_colors)): #Drawing a line of the average color of the Pokémon draw.rectangle((i * width_of_stripe, 0, (i + 1) * width_of_stripe, height_of_stripe), fill=avg_colors[i]) return result_image def main(): #The number of Pokémon your spectrum will contain, here we do only the first generation so that makes it a total of 151 Pokémon NUMBER_OF_POKEMON = 151 #The height of the resulting spectrum HEIGHT_OF_SPECTRUM = 500 #The width of the stripes WIDTH_OF_STRIPES = 10 #Lists where we will stock our colors later avg_colors = [] #Iterating over every Pokémon for i in range(0, NUMBER_OF_POKEMON): #Opening the file pokemon = Image.open("pokemon_" + str(i + 1) + ".png") #Creating an image containing only the visible pixels, removing the transparency from the average visible_colors = createImageContainingVisiblePixels(pokemon) #Getting data h = visible_colors.histogram() #Getting RGB values r = h[0:256] g = h[256:256*2] b = h[256*2:256*3] #Appending the average RGB value to the list avg_colors.append((int(sum( i*w for i, w in enumerate(r) ) / sum(r)), int(sum( i*w for i, w in enumerate(g) ) / sum(g)), int(sum( i*w for i, w in enumerate(b) ) / sum(b)))) #Create and save the spectrum result_image = createSpectrum(WIDTH_OF_STRIPES, HEIGHT_OF_SPECTRUM, NUMBER_OF_POKEMON, avg_colors) result_image.save("result.png") main()

You should now see in your folder a file named “result.png” which is the color spectrum. It should look like this: