Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to loop through one element of a zip() function twice - Python
    text
    copied!<p>So here's my dilema... I'm writing a script that reads all .png files from a folder and then converts them to a number of different dimensions which I have specified in a list. Everything works as it should except it quits after handling one image. </p> <p>Here is my code:</p> <pre><code>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) </code></pre> <p>The following will be returned if the target folder contains 2 images called image1.png,image2.png (for sake of visualization I'll add the dimensions that get applied to the image after an underscore):</p> <p>image1_1024x1024.png, .............., image1_640x690.png (Returns all 7 different dimensions for image1 fine)</p> <p>it stops there when I need it to apply the same transformations to image_2. I know this is because the length of widthList and heightList are only 7 elements long and so exits the loop before image2 gets its turn. Is there any way I can go about looping through widthList and heightList for every image in the targetPath? </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload