我有大量的图像数据,我想在 python 中以特定大小在循环中自动调整它的大小

问题描述

我有这个代码,但我不能把它放在一个循环中: from PIL 导入图片

# Opens a image in RGB mode 
im = Image.open(r"imagPath.png") 

# Setting the points for cropped image 
left = 155
top = 65
right = 360
bottom = 270

# Cropped image of above dimension 
# (It will not change orginal image) 
im1 = im.crop((left,top,right,bottom)) 



# Shows the image in image viewer 
im1.show() 

它可以工作,但我希望它在 for 循环中读取文件夹中的所有图像

解决方法

使用 os.listdir 列出文件夹中的所有图像,然后您可以遍历它们。