3

I'm using the following code to retrieve an image online:

import Image
import urllib2
import cStringIO

url = 'http://storage.googleapis.com/bloomsky-img/k65x5Kvpyc3W08jBqJ1kqZqnnZapoQ==.jpg'
img = urllib2.urlopen(url).read()
# error occurred when executing the line below
im = Image.open(cStringIO.StringIO(img))
im.verify()
# must reload the image after verify method !!
im = Image.open(cStringIO.StringIO(img))
im.save('name', 'JPEG')

When run, it gives me image not valid and error code is cannot identify image file <StringIO.StringIO instance at 0x7f6825b12e18> error. But the same code works perfect on my mac. Only when I deployed the code to an Ubuntu server, did I encounter such issue. I checked the documentations and I think I'm using StringIO in the correct way. Can anybody help? Thanks a lot.

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
J Freebird
  • 3,664
  • 7
  • 46
  • 81
  • If you save `img` to a file — `open("foo.jpg", "w").write(img)` — can you open that file with an image viewer? Or is it also corrupt? – David Wolever Jun 29 '15 at 22:21
  • @DavidWolever I'm using a terminal to operate the remote server so I cannot check if I can open it with an image viewer. But I tried your advice and I can save it as a jpg file for sure. – J Freebird Jun 29 '15 at 22:26
  • @DavidWolever And the url is valid, I can open it and see the image in the browser. – J Freebird Jun 29 '15 at 22:28
  • What's the library you use for `Image`? – Ron Klein Jun 29 '15 at 22:37
  • @RonKlein It's Pillow – J Freebird Jun 29 '15 at 22:38
  • According to its first four bytes signature, the image is in JFIF format. Check if JFIF is a supported Pillow format on the Ubuntu machine. – Ron Klein Jun 29 '15 at 22:52
  • In addition, please write where exactly in the code you encounter the exception (which line?). A comment in the code could be very helpful. – Ron Klein Jun 29 '15 at 23:00
  • @RonKlein I edited the question. I think JFIF files are compressed using the JPEG standard, so the format shouldn't be a problem. – J Freebird Jun 29 '15 at 23:19
  • 1
    your question says stringio is the culprit. if it's pillow, you have a different problem. Grab the file, save it, and use the open() to rule out stringio. The code you wrote works for me to open the file using preview. – Jonathan Jun 29 '15 at 23:21
  • Can you please list the relevant python libs installed on the Ubuntu machine? Namely, `pip list` or `pip freeze` (assuming `pip` was used to install them) – Ron Klein Jun 30 '15 at 06:00

2 Answers2

2

I just installed Pillow on a nearly fresh Ubuntu 14.04, using pip install Pillow. The installation was successful. However, take a look on the installation summary:

    --------------------------------------------------------------------
    PIL SETUP SUMMARY
    --------------------------------------------------------------------
    version      Pillow 2.8.2
    platform     linux2 2.7.6 (default, Mar 22 2014, 22:59:56)
                 [GCC 4.8.2]
    --------------------------------------------------------------------
    *** TKINTER support not available
    (Tcl/Tk 8.6 libraries needed)
    *** JPEG support not available
    *** OPENJPEG (JPEG2000) support not available
    --- ZLIB (PNG/ZIP) support available
    *** LIBTIFF support not available
    *** FREETYPE2 support not available
    *** LITTLECMS2 support not available
    *** WEBP support not available
    *** WEBPMUX support not available
    --------------------------------------------------------------------
    To add a missing option, make sure you have the required
    library, and set the corresponding ROOT variable in the
    setup.py script.
    To check the build, run the selftest.py script.
    changing mode of build/scripts-2.7/pilconvert.py from 644 to 755
    changing mode of build/scripts-2.7/pildriver.py from 644 to 755
    changing mode of build/scripts-2.7/pilfile.py from 644 to 755
    changing mode of build/scripts-2.7/pilprint.py from 644 to 755
    changing mode of build/scripts-2.7/pilfont.py from 644 to 755
    changing mode of [...]
Successfully installed Pillow-2.8.2

See the *** JPEG support not available over there? I think that's the key...

Ron Klein
  • 9,178
  • 9
  • 55
  • 88
1

You have to rewind your stringIO object - just call im.seek(0) both before your verify, and before Image.open (and pass im to it as well - no need to create another stringIO object);

jsbueno
  • 99,910
  • 10
  • 151
  • 209