我有一个小脚本,用于使用 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 acopy()
of the original image.
关于Python Pillow 的缩略图方法返回 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685370/