[SciPy-User] fromimage/imread segfaults on my images

Daniel Lenski dlenski at gmail.com
Tue Mar 9 01:35:09 EST 2010


On Mon, 08 Mar 2010 11:58:08 -0800, Christopher Barker wrote:

> Daniel Lenski wrote:
>> I'm using SciPy to process some sparse binary images with a resolution
>> of about 5000x5000 pixels.  I try to load images like this:
>> 
>>   im = Image.open('/tmp/foo.pbm')
>>   print "Image loaded, size is %s and mode is %s." % (im.size, im.mode)
>>   arr = fromimage(im)
>> 
>> or
>> 
>>   arr = imread('/tmp/foo.pbm')
> 
> where do fromimage and imread come from? "namespaces are one honking
> great idea".

Whoops, sorry.  Those are from scipy.misc.

>> In either case, I get a segfault.  The segfault is definitely in the
>> SciPy code, rather than the PIL code, since PIL's Image.open runs fine.
> 
> don't be so sure -- I think PIL used lazy loading, so it doesn't
> actually read all the data with the open call, but rather when you try
> to do something with it.
> 
> I'd try making a few calls on the PIl image, and make sure it is what
> you expect. If it is, the easiest way to get it into a numpy array is:

Everything works fine as long as I stick with PIL calls only... I can 
even dump the entire contents of the image to a string with 
PIL.Image.fromstring().  No problems there.

> np.asarray(pil_image)

I looked at the code for scipy.misc.fromimage, and this is basically all 
it does.  This is where the segfault occurs.

> do you have a smaller image in the same format you can experiment with?
> That might make it easier to figure out.

Smaller images work fine.  I haven't figured out exactly where the cutoff 
is, but 500x500 works fine, for instance.

>>  I don't know any other good, reliable way
>> to get a big image into an array :-(
> 
> if you really can't get PIL to work, pbm looks really simple:
> 
> http://netpbm.sourceforge.net/doc/pbm.html
> 
> read the header, then read the data with np.fromfile(), then convert to
> a uint8 array with np.unpackbits().

That's what I've ended up doing... rolled my own loadpbm() function.

> You could also take a look at MPL's imread() and see what it does -- I
> don't think MPL requires PIL, though I could be wrong.

MPL requires PIL to load and save images in formats /other than/ PNG.

Dan





More information about the SciPy-User mailing list