python批量改变图像大小

from PIL import Image #python3安装pillow库
import os.path
import glob

def convertSize(jpgfile,outdir,width=256,height=256):  #图片的大小256*256
    img = Image.open(jpgfile)
    try:
        new_img = img.resize((width, height), Image.BILINEAR)
        '''
        if new_img.mode == 'P':
            new_img = new_img.convert("RGB")
        if new_img.mode == 'RGBA':
            new_img = new_img.convert("RGB")
        '''
        new_img.save(os.path.join(outdir, os.path.basename(jpgfile)))
    except Exception as e:
        print(e)

for jpgfile in glob.glob("./label/*.jpg"):  #修改该文件夹下的jpg图片
    convertSize(jpgfile,"./label")  #另存为的文件夹路径

你可能感兴趣的:(POCD,python)