[SciPy-User] FreeImage <-> numpy IO wrappers

Zachary Pincus zachary.pincus at yale.edu
Thu Oct 21 11:49:21 EDT 2010


On Oct 21, 2010, at 11:37 AM, Stéfan van der Walt wrote:

> Hi guys,
>
> [...]
>
> On Tue, Oct 19, 2010 at 8:16 PM, Zachary Pincus <zachary.pincus at yale.edu 
> > wrote:
>>> I had never heard that image data (file formats, OpenGL, etc.) was  
>>> not
>>> ordered in y,x (C-order, i.e. x being fast) memory layout - I though
>>> the top vs. bottom origin was the only "uncertainty" - well, there
>>> maybe more FORTRAN around than I thought ...
>
> Whatever fixes you make, please remember to contribute them back to
> scikits.image!  The plugin is being maintained there as well.

Oh gosh, I didn't realize that -- great! (Did you ever diagnose that  
segfault?)

Anyway, the only serious change I've made has to do with properly  
saving 2D uint8 images, which need to have a palette defined for  
certain formats (PNG maybe? I don't recall...). Anyway, the  
scikits.image version has diverged in structure somewhat from the one  
I use internally in my code, but here's the core of the change:

At the end of _array_to_bitmap(array), right before the return  
statement, I have the following stanza to copy in a greyscale palette:

     if len(shape) == 2 and dtype.type == numpy.uint8:
       palette = _FI.FreeImage_GetPalette(bitmap)
       if not palette:
         raise RuntimeError('Could not get palette of 8-bit greyscale  
image')
       ctypes.memmove(palette, _GREY_PALETTE.ctypes.data, 1024)

where _GREY_PALETTE is defined globally as:
# 4-byte quads of 0,v,v,v from 0,0,0,0 to 0,255,255,255
_GREY_PALETTE = numpy.arange(0, 0x01000000, 0x00010101,  
dtype=numpy.uint32)




More information about the SciPy-User mailing list