Python Pillow的缩略图方法返回None [英] Python Pillow's thumbnail method returning None

查看:194
本文介绍了Python Pillow的缩略图方法返回None的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小脚本,使用Python的Pillow库批量调整图像大小.该脚本适用于resize方法,但是宽高比会发生变化,从而使图像失真,因此我尝试使用缩略图方法测试相同的脚本.

I have a small script I'm using to batch resize images using Python's Pillow library. The script works for the resize method however the aspect ratio changes which distorts the image so I'm trying to test the same script with the thumbnail method.

从文档和其他堆栈问题来看,我感到非常困惑,我可以将resize方法换为thumbnail方法.但是,当我切换到缩略图时,将返回一个无类型的对象.

I'm quite perplexed as it seems from the docs and other stack questions that I can just swap the resize method for the thumbnail method. However when I switch to thumbnail, a none-type object is returned.

我正在使用Python 3.5和Pillow 5.0.有什么想法吗?

I'm using Python 3.5 and Pillow 5.0. Any ideas?

from PIL import Image
import glob

file_list = glob.glob('images_pre/*.jpg')

for f in file_list:
        image = Image.open(f)
        # image = image.resize((170, 170), Image.ANTIALIAS)
        print('image pre: ' + str(image))
        image = image.thumbnail((128, 128), Image.BICUBIC)
        print('image post: ' + str(image))
        file_name = f.split('/')[-1]
        try:
                image.save('images_post/'+file_name, "JPEG")
        except AttributeError:
                print('image failed to save: ' + str(image))

推荐答案

Image.thumbnail修改图像,不返回任何内容.

Image.thumbnail modifies the image in place and doesn't return anything.

文档 http://pillow.readthedocs.io/en/5.1.x/reference/Image.html#PIL.Image.Image.thumbnail 说:

请注意,此函数会修改Image对象.如果还需要使用全分辨率图像,请将此方法应用于原始图像的copy().

Note that this function modifies the Image object in place. If you need to use the full resolution image as well, apply this method to a copy() of the original image.

这篇关于Python Pillow的缩略图方法返回None的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆