Py学习  »  Python

如何使程序使用Python上的数据帧信息通过循环合并多个图像?Python映像库相关

NoahVerner • 3 年前 • 371 次点击  

我有以下几点 df :

   |            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    |

如何创建这样的循环:

对于df中的第一行:

0|Fondo+Cuerpo+Ojos+Color+Pinzas+Puas |Oceano.png+Cuerpo_cangrejo.png+Antenas.png+Amarillo.png+None+None                         |

执行以下操作:

from PIL import Image
first = Image.open(r"./Fondo/Oceano.png") #Opens the very first image using the first element of Permutations column and the first element of FilePermutations column
second = Image.open(r"./Cuerpo/Cuerpo_cangrejo.png") #Opens the second image using the second element of Permutations column and the second element of FilePermutations column
mix1 = Image.alpha_composite(first, second) #merge the previous images together
third = Image.open(r"./Ojos/Antenas.png") #Opens the third image using the third element of Permutations column and the third element of FilePermutations column
mix2 = Image.alpha_composite(mix1, third) #merge mix1 with the third image together
fourth = Image.open(r"./Color/Amarillo.png") #Opens the fourth image using the fourth element of Permutations column and the fourth element of FilePermutations column
mix3 = Image.alpha_composite(mix2, fourth) #merge mix2 with the fourth image together
if current_word != 'None':
    fifth = Image.open(r"./Pinzas/None")
    mix4 = Image.alpha_composite(mix3, fifth)
else:
    continue #continue with the next one
if current_word != 'None':
    sixth = Image.open(r"./Puas/None")
    mix4 = Image.alpha_composite(mix4, sixth)
else:
    continue #continue with the next one
if no more elements in Permutations or FilePermutations:
    resized_img = mix3.resize((350, 350), resample=Image.NEAREST)
    resized_img.save(r"./New_images/"+str(index[i])+".png")

然后重复前面的过程,直到达到 df公司

  • 笔记:

假设文件夹名称来自 Permutations 仅包含图像。

此程序将位于这些文件夹及其相应图像所在的路径中。

元素“None”是文本而不是对象 None ,它只是用来表示对于特定的行,未来的最终图像将不会有来自相应文件夹的图像。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/136367