Python/PIL Resize all images in a folder

后端 未结 8 1250
感动是毒
感动是毒 2020-12-08 00:31

I have the following code that I thought would resize the images in the specified path But when I run it, nothing works and yet python doesn\'t throw any error so I don\'t

8条回答
  •  轮回少年
    2020-12-08 01:11

    Expanded the answer of Andrei M. In order to only change the height of the picture and automatically size the width.

    from PIL import Image
    import os
    
    path = "D:/.../.../.../resized/"
    dirs = os.listdir(path)
    
    def resize():
        for item in dirs:
            if item == '.jpg':
                continue
            if os.path.isfile(path+item):
                image = Image.open(path+item)
                file_path, extension = os.path.splitext(path+item)
                size = image.size
    
                new_image_height = 190
                new_image_width = int(size[1] / size[0] * new_image_height)
    
                image = image.resize((new_image_height, new_image_width), Image.ANTIALIAS)
                image.save(file_path + "_small" + extension, 'JPEG', quality=90)
    
    
    resize()
    

提交回复
热议问题