Python Forum
How can I merge several images via loop using the information of a dataframe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I merge several images via loop using the information of a dataframe?
#1
Question 
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
Reply
#2
Solved here
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Increase the speed of a python loop over a pandas dataframe mcva 0 1,321 Jan-21-2022, 06:24 PM
Last Post: mcva
  for loop in dataframe in pandas Paulman 7 2,770 Dec-02-2021, 12:15 AM
Last Post: bowlofred
  Unable to capture all images of a multipage TIFF file in a merge bendersbender 0 2,218 Nov-19-2020, 03:09 PM
Last Post: bendersbender
  Mail Merge from DataFrame into Word Label Template PEGylated_User 0 1,961 Nov-10-2020, 01:01 PM
Last Post: PEGylated_User
  Dataframe mean calculation problem: do we have to loop? sparkt 1 2,178 Aug-28-2020, 02:41 PM
Last Post: sparkt
  Adding loop results as rows in dataframe Shreya10o 2 2,197 May-09-2020, 11:00 AM
Last Post: Shreya10o
  Pasting multiple images on to one image with a for loop. DreamingInsanity 2 6,485 Feb-01-2019, 12:39 PM
Last Post: DreamingInsanity
  Pands DataFrame merge question FORTITUDE 0 1,929 Sep-20-2018, 01:14 PM
Last Post: FORTITUDE
  Append to dataframe with for loop nocoffeenoworkee 0 13,045 May-01-2017, 11:44 AM
Last Post: nocoffeenoworkee
  Append to dataframe with for loop nocoffeenoworkee 2 6,170 Apr-27-2017, 06:55 PM
Last Post: nocoffeenoworkee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020