Python Pillow 的缩略图方法返回 None
Python Pillow's thumbnail method returning None
我有一个小脚本,用于使用 Python 的 Pillow 库批量调整图像大小。该脚本适用于调整大小方法,但是纵横比发生变化会扭曲图像,因此我尝试使用缩略图方法测试相同的脚本。
我很困惑,因为从文档和其他堆栈问题看来,我可以将调整大小方法换成缩略图方法。但是,当我切换到缩略图时,会返回一个 none 类型的对象。
我正在使用 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
就地修改图像,return 不做任何修改。
文档 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 类型的对象。
我正在使用 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
就地修改图像,return 不做任何修改。
文档 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 acopy()
of the original image.