Forums

PIL error "IOError: cannot identify image file" with Web2py application

Hi, I have a web2py application that have an upload field in a form; after upload I want to resize uploaded image with PIL:

from PIL import Image
infile = request['_vars'][img_field].file  #file object, not file name. PIL accept both.
im = Image.open(infile,mode="r")

Image.open generates "IOError: cannot identify image file" (The image is uploaded correctly). In my pc (Linux, python 2.7, Pillow instead PIL) all in ok. In Pythonanywhere there are both PIL than Pillow, but:

  • PIL is no more manteined and I want to use Pillow
  • there is no way by code to choose Pillow instead PIL (from PIL ... is used by Pillow too same as PIL)
  • the only way to use Pillow if PIL in installed is uninstall PIL but I can't (only root can)

There is a way to uninstall or disable PIL?

A similar problem (solved) there is in 2013 conversation https://www.pythonanywhere.com/forums/topic/582/

Thanks! Fabio

We only have pillow (not pil) installed for Python3.4- which may be the easiest change.

Thanks, but web2py support only Python 2.5/2.6/2.7. I solved problem adding:

request['_vars'][img_field].file.seek(0)

before

infile = request['_vars'][img_field].file  #riferimento al file (non il nome del file)
im = Image.open(infile,mode="r")

(https://groups.google.com/forum/#!searchin/web2py/IOError$3A$20cannot$20identify$20image$20file/web2py/7fon58jVT0g/gKdUTb3S3_cJ) I hope this can be useful for same others user.

Thanks

Maybe this helps