Reputation:
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.
How can I fix my error?
Also is there a way to do this more efficiently (without saving the un-resized file), using a Python library?
Upvotes: 19
Views: 34880
Reputation: 897
The most important thing to remember is not to pass the size as object but as a tuple in resize function new_image = image.resize((img_width,img_size))
and NOT AS
new_image = image.resize(img_width,img_size)
Upvotes: 7