我有以下字典:
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']}
為了以特定順序(即笛卡爾積)獲得每個可能的排列而不重復,我使用以下代碼:
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}')
然后,為了將每次迭代保存到一個資料幀,比如它會拋出一個這樣的輸出:
| 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 |
我使用以下代碼:
new = [' '.join(x) for x in
it.product(*(the_dictionary_list[Name] for Name in AllKeysNames))]
df = pd.DataFrame({'Permutations':" ".join(AllKeysNames), 'FilePermutations':new})
現在,假設上述程式位于同一路徑(即r"./")中,巧合的是以下檔案夾也位于其中:

這些檔案夾僅包含檔案影像,巧合的是,它們也與the_dictionary_list.
df除了檔案名和檔案夾名之外,變數還存盤了這些影像必須合并的正確順序,以及排列的總數
該程式如何從以下位置獲取該資訊 df 并使用以下功能:
Image.open(r"./")
Image.alpha_composite()
resize((350, 350), resample=Image.NEAREST
從 Python Imaging Library (PIL)
以df顯示的順序生成新的合并影像?
筆記:
影像檔案名可以等于
df.由于該
None元素實際上并不存在于檔案夾中,因此在需要時,程式必須將前一個影像與下一個影像合并(即不呼叫Image.open(r"./")也不Image.alpha_composite()在None出現“ ”時繼續對下一個元素執行此操作)只有在合并了一行的檔案影像后,它才會呼叫
resize((350, 350), resample=Image.NEAREST然后使用保存最終輸出.save(r"./Test/str(Index(i)) ".png"),然后重復該程序,直到達到最終索引df
uj5u.com熱心網友回復:
以下解決方案是由@christian從stackoverflow en espa?ol構建的,他的回答翻譯如下:
Pandas DataFrames 有一個名為 iterrorws() 的方法,它回傳一個生成器,我們可以遍歷它,它回傳行本身作為包含兩個物件的元組,第一個是行的索引,第二個是 Pandas 系列包含下一列的值。
我不建議加入一個名字
,你可以簡單地把它們以串列形式,因為以后我們會再需要他們的串列,您可以通過使用避免轉換保存到一個串列' '.join(x)。
for i, per in df.iterrows():
images = per["FilePermutations"].split(" ")
files = per["Permutations"].split(" ")
有了這個,我們將獲得相應列的值,并在該
split方法的幫助下將其轉換回串列。一旦我們有了這個,我們就可以繼續創建新影像,這是加入images串列中指定的所有影像的結果。為此,我們首先要知道每個影像的目錄,并且“巧合”files串列的第一個元素是串列第一個元素的影像所在的目錄,images對于每個影像,依此類推。它碰巧使用了zip()匹配每個元素的函式(如果串列的元素較少,它只會匹配最小的串列,而不會包括其他串列,如果有這種情況,您可以使用該zip_longest函式itertools模塊) 。有了這個,我們就已經有了path影像,我們只需要打開它,組合它,最后調整它的大小并保存它。
for i, per in df.iterrows():
images = per["FilePermutations"].split(" ")
files = per["Permutations"].split(" ")
result_image = None #aquí se almacenará la imagen resultante
for direc, img in zip(files, images): #iteramos
if img=="None": continue #si es None omitimos
path = f"{direc}/{img}" #definimos la ruta donde se encuentra la imagen
#en la primera iteración no habrá imagen inicial por lo que no podrá combinarse con otra
# por lo que asignamos la imagen
if result_image == None:
result_image = Image.open(f"./{path}")
else: #combinamos la imagen
img2 = Image.open(f"./{path}")
result_image = Image.alpha_composite(result_image, img2)
# redimensionamos y guardamos
result_image = result_image.resize((350, 350), resample=Image.NEAREST)
result_image.save(f"./Test/image_{i}.png")
這將組合每一行中的所有指定影像并將它們保存到指定路徑。正如您所看到的,這并沒有什么神奇之處,只有幾個
for回圈,第一個回圈遍歷行,第二個回圈創建path并獲取影像。在第二次for的每次迭代中,的結果都會result_image被覆寫,因此它總是包含當前影像和前一個影像的組合結果,這樣所有的影像都被混合了,顯然那些不存在的被標記為'None'被省略
這非常好地回答了我的問題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/399158.html
