I'm making a simple API in Flask that accepts an image encoded in base64, then decodes it for further processing using Pillow.
I've looked at some examples (1, 2, 3), and I think I get the gist of the process, but I keep getting an error where Pillow can't read the string I gave it.
Here's what I've got so far:
import cStringIO
from PIL import Image
import base64
data = request.form
image_string = cStringIO.StringIO(base64.b64decode(data['img']))
image = Image.open(image_string)
which gives the error:
IOError: cannot identify image file <cStringIO.StringIO object at 0x10f84c7a0>
data['img']
? Log that out, or print it out.img
tag to make sure the image data wasn't corrupted.b64decode()
? Is there any other encoding going on, e.g. urlencoding? Is it really an image supported by PIL?print repr(data['img'])
produce on your console for example?b64 decode
: imgur.com/7ZJNuPf I was trying to follow the example here. @Martijn Pieters here's whatprint repr(data['img'])
looks like: imgur.com/xmrvNmI