PIL:ValueError:未知的重采样过滤器,如何调整Flask上载的图像的大小? [英] PIL: ValueError: unknown resampling filter, How to resize images uploaded on Flask?
问题描述
我正在使用Flask制作网络应用,并且我想调整上传图像的大小.我正在使用PIL来执行此操作,但是会引发错误.
I'm making a web app using Flask, and I want to resize the images that are uploaded. I'm using PIL to do this, but an error is thrown.
执行此操作的过程是这样的,但效率似乎很低:
The process to do it is like this, but it seems inefficient:
filename = secure_filename(form.image.data.filename)
form.image.data.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
img = Image.open(os.path.join(app.config['UPLOAD_FOLDER'],filename), 'r')
img = img.resize(300, 300)
img.save(filename, quality=100, optimize=True)
我要做的是在用户上传图片后保存图片,打开新文件,调整其大小,然后再次保存.
What I'm trying to do is save the image after the user uploaded it, open the new file, resize it, and save it again.
如何解决我的错误?
还有使用Python库更有效地执行此操作(不保存未调整大小的文件)的方法吗?
Also is there a way to do this more efficiently (without saving the un-resized file), using a Python library?
推荐答案
img = img.resize(300,300)
img = img.resize(300, 300)
=> img = img.resize((300,300))
=> img = img.resize((300,300))
这篇关于PIL:ValueError:未知的重采样过滤器,如何调整Flask上载的图像的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!