Python Pillow 的缩略图方法返回 None

标签 python python-3.x image python-imaging-library

我有一个小脚本,用于使用 Python 的 Pillow 库批量调整图像大小。该脚本适用于调整大小方法,但是纵横比的变化会扭曲图像,因此我尝试使用缩略图方法测试相同的脚本。

我很困惑,因为从文档和其他堆栈问题看来,我可以将调整大小方法替换为缩略图方法。但是,当我切换到缩略图时,会返回一个非类型对象。

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

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 就地修改图像并且不返回任何内容。

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

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,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685370/

相关文章:

Python 日志记录卡在特定字节 - 0x90

python - 无法获取不同项目的所有链接

python - 如何用matplotlib绘制小比例地形图?

python - 我正在使用 Python3 编写 CGI 脚本,它识别出错误的目录

java - 适用于组合栅格和叠加几何数据的文件类型?

css - 在固定宽度的 DIV 容器内调整 IMG 的边框

python - 使用 Google App Engine (Webapp) 访问 Django 模板中的 session 变量 - Python

python - 标准库中的所有内容都会在 Python 3.0 中将字符串视为 unicode 吗?

python - 如何计算被随机字母包围的特定字符

ruby - 在 Jekyll 支持的网站上显示目录中的所有图像