2013-06-25 22 views
4

所以這裏是我的困境......我正在編寫一個腳本,它從文件夾中讀取所有.png文件,然後將它們轉換爲多個不同的維度這是我在列表中指定的。一切工作,因爲它應該除了它處理完一個圖像後退出。如何通過zip()函數的兩個元素循環兩次 - Python

這裏是我的代碼:

sizeFormats = ["1024x1024", "114x114", "40x40", "58x58", "60x60", "640x1136", "640x960"] 

def resizeImages(): 

widthList = [] 
heightList = [] 
resizedHeight = 0 
resizedWidth = 0 

#targetPath is the path to the folder that contains the images 
folderToResizeContents = os.listdir(targetPath) 

#This splits the dimensions into 2 separate lists for height and width (ex: 640x960 adds 
#640 to widthList and 960 to heightList 
for index in sizeFormats: 
    widthList.append(index.split("x")[0]) 
    heightList.append(index.split("x")[1]) 

#for every image in the folder, apply the dimensions from the populated lists and save 
for image,w,h in zip(folderToResizeContents,widthList,heightList): 
    resizedWidth = int(w) 
    resizedHeight = int(h) 
    sourceFilePath = os.path.join(targetPath,image) 
    imageFileToConvert = Image.open(sourceFilePath) 
    outputFile = imageFileToConvert.resize((resizedWidth,resizedHeight), Image.ANTIALIAS) 
    outputFile.save(sourceFilePath) 

如果目標文件夾包含2個圖像稱爲image1.png,image2.png(用於可視化的緣故,我要補充的是得到應用的尺寸下將返回爲下劃線之後的圖像):

image1_1024x1024.png, .............., image1_640x690.png(返回image1的細所有7周不同的尺寸)

它在我需要時停在那裏它將相同的轉換應用於image_2。我知道這是因爲widthList和heightList的長度只有7個元素,所以在image2輪到它之前退出循環。有沒有什麼辦法可以通過widthList和heightList爲targetPath中的每個圖像循環?

回答

3

爲什麼不把它簡單:

for image in folderToResizeContents: 
    for fmt in sizeFormats: 
     (w,h) = fmt.split('x') 

NB您正在覆蓋生成的文件,因爲您沒有更改outpath的名稱。

+0

這是有幫助爲了獲得寬度和高度分開,但它並沒有幫助解決我遇到的實際問題,這是獲取目標文件夾中的每個圖像貼上的那些屬性 – Coletrain

+0

啊,我的確覆蓋了文件!添加一點點消息來生成獨特的名稱,並按預期工作。非常感謝您的輸入我很感激 – Coletrain

0

巢您的for循環,你可以將所有7個維度對每個圖像

for image in folderToResizeContents: 
    for w,h in zip(widthList,heightList): 

的第一個for循環將確保它會爲每個圖像,而第二個for循環將確保圖像大小以每個尺寸

+0

我試着築巢,但是當你做這樣每幅圖像取列表的最後一個尺寸(640×960) – Coletrain

+0

@ColeGoodyear它不應該,這意味着你將其設置錯誤 – Stephan

0

您需要重新遍歷每個文件的sizeFormats。 Zip不會這樣做,除非你使用循環迭代器在高度和寬度方面更加棘手。

當一對嵌套的for循環工作正常時,有時候zip等工具會使代碼變得更復雜。我認爲它比分成多個列表更直接,然後再將它們重新壓縮在一起。

sizeFormats = ["1024x1024", "114x114", "40x40", "58x58", "60x60", "640x1136", "640x960"] 
sizeTuples = [(int(w), int(h)) for w,h in map(lambda wh: wh.split('x'), sizeFormats)] 

def resizeImages(): 

    #for every image in the folder, apply the dimensions from the populated lists and save 
    for image in os.listdir(targetPath): 
     for resizedWidth, resizedHeight in sizeTuples: 
      sourceFilePath = os.path.join(targetPath,image) 
      imageFileToConvert = Image.open(sourceFilePath) 
      outputFile = imageFileToConvert.resize((resizedWidth,resizedHeight), Image.ANTIALIAS) 
      outputFile.save(sourceFilePath) 
+0

當我運行這個時遇到了同樣的問題,當我嘗試下面的Stephan的代碼時。每個圖像都是列表的最後一個維度,所以我最終爲每個輸入.png創建了7個圖像,全部都是640x960 – Coletrain

+0

我專注於迭代部分......請參閱@Steve Barnes答案。 'outputFile.save(sourceFilePath)'保持覆蓋你的文件(實際上,你覆蓋了你的原始輸入文件)。您需要生成唯一的名稱,可能是將sizeFormats字符串附加到文件中。 – tdelaney

+0

啊,你是一位聖人,我不敢相信我忽略了這一點!添加一點點消息來生成獨特的名稱,並按預期工作。感謝您的幫助 – Coletrain