PIL批量更改图片像素大小,python

import os, sys, shutil
from PIL import Image

path = os.getcwd().replace("\\", "/")



for file in os.listdir(path+"/奥特曼图片"):
    if (".png" in file) and ("小" not in file):
        try:
            print(file, path+"/奥特曼图片", path +"/奥特曼图片/"+ file)
            
            image = Image.open(path +"/奥特曼图片/"+ file)
            print(image.size)  #打印图片大小
            x = image.size[0]  #简化后期代码
            y = image.size[1]
            print(x, y)

            if x>500:
                y2 = int(y * 500/x)   #宽高同比例更改大小
                image.resize((500, y2))
                
            image.save(path +"/奥特曼图片/小"+ file)
        except:            
            pass

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