How to skip one element of zip () function twice - Python

So, here is my dilemma ... I am writing a script that reads all .png files from a folder, and then converts them into several different dimensions, which I specified in the list. Everything works as it should, except that it exits after processing a single image.

Here is my 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) 

In case the target folder contains 2 images with the name image1.png, image2.png (for visualization, I will add dimensions that will be applied to the image after underlining):

image1_1024x1024.png, .............., image1_640x690.png (Returns all 7 different sizes for image 1)

it stops there when I need to apply the same transformations to image_2. I know this is due to the fact that the widthList and heightList are only 7 elements long and therefore go out of the loop before image2 returns. Is there a way that I can cycle through widthList and heightList for each image in targetPath?

+4
source share
3 answers

Why not keep it simple:

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

NB You overwrite files created because you do not change the name outpath.

+3
source

A nest for your cycles, and you can apply all 7 dimensions to each image

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

the first cycle of the cycle ensures that this happens for each image, while the second cycle will ensure that the image is resized to each size

0
source

You need to iterate through sizeFormats for each file. Zip does not do this unless you become even more complex with circular iterators in height and width.

Sometimes tools like zip make more complex code when multiple nested loops work fine. I think that it is more straightforward than dividing into several lists, and then fastens them back again.

 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
source

Source: https://habr.com/ru/post/1488156/


All Articles