User upload a image file by the Form, I don't want to save original uploaded image file to disk, and resize image by Pillow opening image from disk.
I want to resize this image file in memory first, then save the resized image file to disk. so I import StringIO as buffer, but it does't work with Pillow.
Here is the code:
Python3.4, Flask==0.10.1, Pillow==3.4.2
forms.py
class Form():
img = FileField()
submit = SubmitField()
views.py
from io import StringIO
from PIL import Image
from .forms import Form
@app.route('/upload_img', methods=['GET', 'POST'])
def upload_img():
form = Form()
im = Image.open(StringIO(form.img.data.read())
pass
TypeError: initial_value must be str or None, not bytes
From Pillow docs:
What you're passing to
open
is aStringIO
. It creates a file-like object from anstr
object that is opened in text mode.The issue is caused by the argument in
StringIO
.form.img.data.read()
returns abytes
object, passing it into the constructor is forbidden. But in your case, aStringIO
won't work.Instead, use
io.BytesIO
. It has pretty much the same interface, except that it takesbytes
objects and returns a file-like object opened in binary mode, which is what you need.