简体   繁体   中英

Resize images in python using PIL

I'm trying to resize a set of images, approximatively 366, so I created a script that I tested first on 3 and it was successful.

The issue is when I process the whole folder, it returns me this error :

resizeimage.imageexceptions.ImageSizeError: 'Image is too small, Image size : (275, 183), Required size : (399, 399)'

My script is supposed to iterate an entire folder, resize images then store the output files in another folder:

import os

from PIL import Image

from resizeimage import resizeimage

path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)

for file in os.listdir(path):
    with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
        with Image.open(f) as image:
            cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
            cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)

I did use this instruction:

thumb = ImageOps.fit(image, size, Image.ANTIALIAS) but I believe that it crops images instead of resizing them.

If you have any ideas about how to solve this issue, it would be great.

Downsampling an image (making it smaller) is one thing, and upsampling (making it bigger) is another thing. If you want to downsample, ANTIALIAS is a good choice, if you want to upsample, there are other filters you could use.

import os

from PIL import Image

from resizeimage import resizeimage

path = "/Users/sigc2sige/PycharmProjects/helloworld/photos"
size = (399, 399)

for file in os.listdir(path):
    with open('/Users/sigc2sige/PycharmProjects/helloworld/photos/'+file, 'r+b') as f:
        with Image.open(f) as image:
            if (image.size) >= size:
                cover = resizeimage.resize_cover(image, size, Image.ANTIALIAS)
                cover.save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)
            else:
                cover = image.resize(size, Image.BICUBIC).save('/Users/sigc2sige/PycharmProjects/helloworld/photos_2/'+file, image.format)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM