Python/PIL Resize all images in a folder

后端 未结 8 1276
感动是毒
感动是毒 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:32

    This code just worked for me to resize images..

    from PIL import Image
    import glob
    import os
    
    # new folder path (may need to alter for Windows OS)
    # change path to your path
    path = 'yourpath/Resized_Shapes' #the path where to save resized images
    # create new folder
    if not os.path.exists(path):
        os.makedirs(path)
    
    # loop over existing images and resize
    # change path to your path
    for filename in glob.glob('your_path/*.jpg'): #path of raw images
        img = Image.open(filename).resize((306,306))
        # save resized images to new folder with existing filename
        img.save('{}{}{}'.format(path,'/',os.path.split(filename)[1]))
    

提交回复
热议问题