PIL FITs image decoder

jbrewer jeremy.d.brewer at gmail.com
Wed Nov 23 07:21:44 CET 2005

I tried following your simple example (I already had something similar)
but with no luck.  I'm completely stumped as to why this doesn't work.
I even tried manually scaling the data to be in the range 0-255 out of
desperation.  The data is definitely contiguous and 32 bit floating
point.  At this point I've tried everything I can think of.  Any ideas?


# open the fits file using pyfits
fits = pyfits.open(fitsfile)
xsize, ysize = fits[0].data.shape
data = fits[0].data.astype("Float32")

# now create an image object
image = Image.frombuffer("F", (xsize, ysize), data)

# scale the data be 256 bit unsigned integers
#int_data = numarray.zeros((xsize, ysize)).astype("UInt8")
#min = data.min()
#max = data.max()
#for i in arange(xsize):
#    for j in arange(ysize):
#        scaled_value = (data[i, j] - min) * (255.0 / (max - min))
#        int_data[i, j] = int(scaled_value + 0.5)

#image = Image.frombuffer("L", (xsize, ysize), int_data)

More information about the Python-list mailing list