在jpgfile初输入自己需要处理的图像的路径
outdir输入自己的需要保存处理好图像的路径
width, height分别代表宽和高
if filename.endswith('bmp'): 填写自己图像的格式
convertjpg(jpgfile, outdir, 100, 100) 输入地址+输出地址+宽+高
from PIL import Image
import os.path
import glob
jpgfile=' ./imgdata1'
outdir=' ./imgdata2'
def convertjpg(jpgfile, outdir, width, height):
for parent, dirnames, filenames in os.walk(jpgfile, followlinks=True):
for filename in filenames:
if filename.endswith('bmp'):
file_path = os.path.join(jpgfile, filename)
img = Image.open(file_path)
new_img = img.resize((width, height), Image.BILINEAR) ###核心代码
new_img.save(os.path.join(outdir, filename))
if __name__ == "__main__":
convertjpg(jpgfile, outdir, 100, 100)