简体   繁体   English

使用PIL在python中调整图像大小

[英]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. 我正在尝试调整一组图像的大小,大约是366,所以我创建了一个脚本,我首先在3上测试并且它成功了。

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. thumb = ImageOps.fit(image, size, Image.ANTIALIAS)但我相信它会thumb = ImageOps.fit(image, size, Image.ANTIALIAS)图像而不是调整它们的大小。

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. 如果你想下采样,ANTIALIAS是一个不错的选择,如果你想上传,你可以使用其他过滤器。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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