1

I'm trying to save a jpg image from a Flask app form. The following code works fine:

blob = request.files[canvas_key]
blob.stream.seek(0)
data = blob.stream.read()
string_io = cStringIO.StringIO(data)

string_io has type <cStringIO.StringI object at 0x10bf2bf10>.

Yet when I try: Image.open(string_io), I get this error: IOError: cannot identify image file.

Using stream.seek(0) seems to solve this problem for other people, but in my case it hasn't.

Community
  • 1
  • 1
Rose Perrone
  • 61,572
  • 58
  • 208
  • 243
  • Does the stream have a HTTP header, or is it really a JPEG? Does it have the string 'JFIF' in the first dozen bytes? – johntellsall Jun 03 '14 at 21:02
  • This is the `string_io` prefix: 'RIFF\xa2_\x00\x00WEBPVP8 ', which makes sense, because I'm getting the image from 'image/webp`, and "In 2010 Google introduced the WebP picture format, which uses RIFF as a container". This is the specific line of javascript that creates the image: `var dataURL = canvas.toDataURL('image/webp');` – Rose Perrone Jun 03 '14 at 21:04
  • 4
    I don't think `WebP` even existed when PIL was last updated. – Mark Ransom Jun 03 '14 at 21:09
  • 2
    to build on @Marks' comment -- switch to the Pillow library. It's a fork of PIL that's being updated, and supports WebP -- https://pypi.python.org/pypi/Pillow/2.0.0 – johntellsall Jun 03 '14 at 21:10

1 Answers1

1

Thanks to @Mark, I changed the image type I capture from WebP to PNG.

Rose Perrone
  • 61,572
  • 58
  • 208
  • 243