这是我的dilema。。。我正在编写一个脚本,从一个文件夹中读取所有的.png文件,然后将它们转换成我在列表中指定的许多不同维度。除了在处理一个图像后退出之外,一切都正常工作。在
这是我的代码: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)
如果目标文件夹包含两个名为image1.png、image2.png的图像,则将返回以下内容(为了可视化起见,我将在下划线后添加应用于图像的维度):
图像1_1024x1024.png,
..............,
image1_640x690.png(返回image1 fine的所有7个不同维度)
当我需要它对图像2应用相同的变换时,它就停止了。我知道这是因为widthList和heightList的长度只有7个元素,所以在image2轮到它之前就退出了循环。有什么方法可以让我循环遍历targetPath中每个图像的widthList和heightList吗?在