Python/PIL Resize all images in a folder

后端 未结 8 1242
感动是毒
感动是毒 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:08
    #!/usr/bin/python
    from PIL import Image
    import os, sys
    
    path = "/root/Desktop/python/images/"
    dirs = os.listdir( path )
    
    def resize():
        for item in dirs:
            if os.path.isfile(path+item):
                im = Image.open(path+item)
                f, e = os.path.splitext(path+item)
                imResize = im.resize((200,200), Image.ANTIALIAS)
                imResize.save(f + ' resized.jpg', 'JPEG', quality=90)
    
    resize()
    

    Your mistake is belong to full path of the files. Instead of item must be path+item

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-12-08 01:16

    John Ottenlips's solution created pictures with black borders on top/bottom i think because he used

      Image.new("RGB", (final_size, final_size))
    

    which creates a square new image with the final_size as dimension, even if the original picture was not a square.

    This fixes the problem and, in my opinion, makes the solution a bit clearer:

    from PIL import Image
    import os
    
    path = "C:/path/needs/to/end/with/a/"
    resize_ratio = 0.5  # where 0.5 is half size, 2 is double size
    
    def resize_aspect_fit():
        dirs = os.listdir(path)
        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)
    
                new_image_height = int(image.size[0] / (1/resize_ratio))
                new_image_length = int(image.size[1] / (1/resize_ratio))
    
                image = image.resize((new_image_height, new_image_length), Image.ANTIALIAS)
                image.save(file_path + "_small" + extension, 'JPEG', quality=90)
    
    
    resize_aspect_fit()
    
    0 讨论(0)
  • 2020-12-08 01:23

    In case you want to keep the same aspect ratio of the image you can use this script.

    from PIL import Image
    import os, sys
    
    path = "/path/images/"
    dirs = os.listdir( path )
    final_size = 244;
    
    def resize_aspect_fit():
        for item in dirs:
             if item == '.DS_Store':
                 continue
             if os.path.isfile(path+item):
                 im = Image.open(path+item)
                 f, e = os.path.splitext(path+item)
                 size = im.size
                 ratio = float(final_size) / max(size)
                 new_image_size = tuple([int(x*ratio) for x in size])
                 im = im.resize(new_image_size, Image.ANTIALIAS)
                 new_im = Image.new("RGB", (final_size, final_size))
                 new_im.paste(im, ((final_size-new_image_size[0])//2, (final_size-new_image_size[1])//2))
                 new_im.save(f + 'resized.jpg', 'JPEG', quality=90)
    resize_aspect_fit()
    
    0 讨论(0)
  • 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]))
    
    0 讨论(0)
  • 2020-12-08 01:33

    Expanding on the great solution of @Sanjar Stone

    for including subfolders and also avoid DS warnings you can use the glob library:

    from PIL import Image
    import os, sys
    import glob
    
    root_dir = "/.../.../.../"
    
    
    for filename in glob.iglob(root_dir + '**/*.jpg', recursive=True):
        print(filename)
        im = Image.open(filename)
        imResize = im.resize((28,28), Image.ANTIALIAS)
        imResize.save(filename , 'JPEG', quality=90)
    
    0 讨论(0)
提交回复
热议问题