[SciPy-dev] Very strange interaction between scipy and PIL

Fernando Perez fperez at colorado.edu
Fri Oct 24 17:34:35 CDT 2003


Hi all,

I'm seeing an extremely strange behavior.  Consider the following:

In [1]: import Image

In [2]: im=Image.open('slide_img0065_sm.jpg')

In [3]: import scipy

In [4]: arr=scipy.imread('slide_img0065_sm.jpg')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)

?

/usr/lib/python2.2/site-packages/scipy/pilutil.py in 
imread(name='slide_img0065_sm.jpg', flatten=0)
      39      grayscale layer.
      40     """
---> 41     im = Image.open(name)
         im = undefined, global Image = <module 'Image' from 
'/usr/lib/python2.2/site-packages/PIL/Image.pyc' [imported]>, global open = 
undefined, name = 'slide_img0065_sm.jpg'
      42     return fromimage(im,flatten=flatten)
      43

/usr/lib/python2.2/site-packages/PIL/Image.py in open(fp=<open file 
'slide_img0065_sm.jpg', mode 'rb'>, mode='r')
    1572             pass
    1573
-> 1574     raise IOError("cannot identify image file")
         global IOError = undefined
    1575
    1576 #

IOError: cannot identify image file


When you realize that the code for imread() does only:

def imread(name,flatten=0):
     """Read an image file from a filename.

     Optional arguments:

      - flatten (0): if true, the image is flattened by calling convert('F') on
      the resulting image object.  This flattens the color layers into a single
      grayscale layer.
     """
     im = Image.open(name)
     return fromimage(im,flatten=flatten)

it makes no sense whatsoever!  Somehow, the fact of importing scipy seems to 
mess something internally in whatever it is that Image.open does, if Image was 
imported first.  If you ONLY use scipy, it works fine:

In [1]: import scipy

In [2]: arr=scipy.imread('slide_img0065_sm.jpg')

In [3]: arr.shape
Out[3]: (387, 577, 3)


And here's an even better one.  Compare these two scenarios:

In [1]: import Image,scipy

In [2]: im=Image.open('slide_img0065_sm.jpg')
---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)

?

/usr/lib/python2.2/site-packages/PIL/Image.py in open(fp=<open file 
'slide_img0065_sm.jpg', mode 'rb'>, mode='r')
    1572             pass
    1573
-> 1574     raise IOError("cannot identify image file")
         global IOError = undefined
    1575
    1576 #

IOError: cannot identify image file

In [3]:

VS:

In [1]: import scipy,Image

In [2]: im=Image.open('slide_img0065_sm.jpg')

In [3]:

This means that importing scipy AFTER Image breaks something, but importing it 
before does not.

If anyone can crack this one, I'd love to hear.  I tried for a while already, 
but didn't get very far.

Best,

f.



More information about the Scipy-dev mailing list