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?
source share