Python Forum

Full Version: How can I merge several images via loop using the information of a dataframe?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have the following dictionary:
the_dictionary_list = {'Fondo': ['Oceano.png'],
                       'Cuerpo': ['Cuerpo_cangrejo.png'], 
                       'Ojos': ['Antenas.png', 'Pico.png', 'Verticales.png'],
                       'Color': ['Amarillo.png', 'Blanco.png', 'Rojirosado.png', 'Turquesa.png', 'Verde_oscuro.png', 'Zapote.png'], 
                       'Pinzas': ['None', 'Pinzitas.png', 'Pinzotas.png', 'Pinzota_pinzita.png'], 
                       'Puas': ['None', 'Arena.png', 'Marron.png', 'Purpura.png', 'Verde.png']}
To get each possible permutation without repetition in a specific order (i.e. cartesian products) I use the following code:

import itertools as it
    
AllKeysNames = ['Fondo', 'Cuerpo', 'Ojos', 'Color', 'Pinzas', 'Puas']
Combinations = list(it.product(*(the_dictionary_list[Name] for Name in AllKeysNames)))
print(f'{Combinations}')
Then, for saving each iteration to a dataframe such as it throws an output like this one:
Output:
| Permutations | FilePermutations | 0|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+None | 1|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Arena.png | 2|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Marron.png | 3|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Purpura.png | 4|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+Verde.png | 5|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+Pinzitas.png+None | 6|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+Pinzitas.png+Arena.png | 7|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+Pinzitas.png+Marron.png | 8|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+Pinzitas.png+Purpura.png | 9|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+Pinzitas.png+Verde.png | . . . 358|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Verticales.png+Zapote.png+Pinzota_pinzita.png+Purpura.png | 359|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Verticales.png+Zapote.png+Pinzota_pinzita.png+Verde.png |
I use the following code:

new = ['+'.join(x) for x in it.product(*(the_dictionary_list[Name] for Name in AllKeysNames))]
    
df = pd.DataFrame({'Permutations':"+".join(AllKeysNames), 'FilePermutations':new})
Now, suppose that the above program is located in the same path (i.e. r"./") in which coincidentally the following folders are also located:

[Image: PC6QM.png]

These folders only contain file images, which *also coincidentally* have the same name of those values in the_dictionary_list.

As the df variable has stored the right order in which these images must be merged besides the filenames and foldernames, and also the total amount of permutations

How could this program take that information from the df and use the functions of:

Image.open(r"./")

Image.alpha_composite()

resize((350, 350), resample=Image.NEAREST

from Python Imaging Library (PIL)

To produce the new merged images in the order in which the df shows it?

Quote:Notes:

> The image filenames can be equal to the respective index of df.
>
> As the None element doesn't actually exist in the folders, when needed, the program would have to merge the previous images with the next one (i.e. not calling Image.open(r"./") nor Image.alpha_composite() when 'None' appears and continue to do so with the next element)
>
>Only after having merged the file images of a row, it would call resize((350, 350), resample=Image.NEAREST for then saving the final output using .save(r"./Test/str(Index(i))+".png") and then repeat the process until it has reached the final index of df
Solved here