Skip to content
Advertisement

Issue with creating multiple images through alpha compositing together in batches

I am basically creating a program using Pillow for Python where it randomly generates pictures of owls. It does this by choosing different parts for the owl, that being filepaths for different premade art asset images, from various nested dictionaries. These parts include: Bases (the body of the owl), patterns (patterns layered onto the owls feathers), and accessories (like hats, glasses, and such). The filepaths that make up each of these chosen parts are then put into one big list and are put through a for loop that uses the file paths to create each image, colors them, and then alphacomposites the finished images into a final image. The issue I am having is that sometimes, mainly when I have the program create multiple owls in one go as a batch, sometimes an owl will have assets on them that were not intended. The types of uncalled for parts I see on these incorrect owls are always either incorrect patterns and/or accessories. An example of which is below:

The Owl that came out of the batch: The Owl that came out of the batch

Now, each owl created is given a special code as part of their file name. This code is basically a list of dictionary keys that, when put back into the program, allows it to use those keys to sift through the dictionaries of filepaths for the parts of the owl and then recreate the owl image.

The owl I showed above has a code key of: BC0_PC2_CC_Orca_White_ACG15. I’ll go through each part of the code to show what each part means. BC0 means this Owls base is supposed to be a Great Grey Owl, which it is. PC2 means its pattern is supposed to be leaves, which it seems to have, but it also has a checkerboard pattern. CC means it will have a simple color scheme. Orca means its base will be colored in the orca color scheme and White means its eyes, beak, and feet will be colored white. ACG15 means the owl will have a certain type of glasses accessory.

When I put in this owl’s code back into the program, it recreates the owl correctly, as you can see below:

The Owl after its code was put back into the program: enter image description here

I am looking for advice on how to deal with this issue. For context, Below I will explain the basic process on how an owl image is created through commented code, especially in regard to it’s patterns and accessories.

Part 1: Creating an Owl’s Code Key

def owl_pull():

JavaScript

def create_code(rarity):

JavaScript

Part 1a: Creating the pattern Code Key

def create_pattern_code(rarity, base):

JavaScript

Part 1b: Creating the accessories Code Key

def create_accessory_type_code(rarity):

JavaScript

def create_accessory_code(type):

JavaScript

Part 2: Generating an Owl

def reincarnate_owl(code):

JavaScript

Part 2a: Generating the Pattern

def generate_pattern(part_key, base_key):

JavaScript

Part 2b: Generating the Accessories

def generate_accessory(part_key):

JavaScript

Part 3: Relevant Dictionaries

Below are the relevant samples of the dictionaries for the owls file Owl Patterns

JavaScript

Owl Accessories

JavaScript

Advertisement

Answer

Instead of this:

JavaScript

Do this:

JavaScript

Now you’re not modifying a list while iterating it, and you aren’t trying to track the list indexes.

Although, now that I look at it, since that generate_pattern call doesn’t depend on the loop value, what is the purpose of this? Are you really trying to apply the pattern multiple times? What you have is the same as:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement