枕头的 Image.thumbnail() 没有做任何事情
Pillow's Image.thumbnail() doesn't do anything
我尝试使用以下代码制作缩略图:
import Image
# Skipping creation of file-like object 'f'
im = Image.open(f)
im.thumbnail((256, im.height))
im.save(f, 'WebP')
f.flush()
docs say "This method modifies the image to contain a thumbnail version of itself, no larger than the given size." Thus, I expect the output to be fit within 256px width while preserving the aspect ratio。然而,上面的代码没有任何效果,输出图像具有与输入相同的分辨率,始终大于 256px 宽度。
怎样才能达到想要的效果?
Pillow 文档指定文件需要以二进制模式打开,但此处使用 w+b
时,新图像实际上会附加到旧图像上。它需要完全加载到内存和文件中才能被截断。工作代码是:
import Image
# Skipping creation of file-like object 'f'
im = Image.open(f)
im.load()
f.seek(0)
f.file.truncate()
im.thumbnail((256, im.height))
im.save(f, 'WebP')
f.flush()
我尝试使用以下代码制作缩略图:
import Image
# Skipping creation of file-like object 'f'
im = Image.open(f)
im.thumbnail((256, im.height))
im.save(f, 'WebP')
f.flush()
docs say "This method modifies the image to contain a thumbnail version of itself, no larger than the given size." Thus, I expect the output to be fit within 256px width while preserving the aspect ratio。然而,上面的代码没有任何效果,输出图像具有与输入相同的分辨率,始终大于 256px 宽度。
怎样才能达到想要的效果?
Pillow 文档指定文件需要以二进制模式打开,但此处使用 w+b
时,新图像实际上会附加到旧图像上。它需要完全加载到内存和文件中才能被截断。工作代码是:
import Image
# Skipping creation of file-like object 'f'
im = Image.open(f)
im.load()
f.seek(0)
f.file.truncate()
im.thumbnail((256, im.height))
im.save(f, 'WebP')
f.flush()