[AstroPy] Coding in Python to Read/Write a fits file.

Megan Sosey sosey@stsci....
Wed Apr 8 09:51:11 CDT 2009


Hi Wayne,

something like this work for me, perhaps it will help
get you closer to what you want. I attached the sample
image I used.


cheers,
Megan

-----
#!/usr/bin/env python
import pyfits
import numpy
import Image
import time

image = Image.open('hs-2009-14-a-web.jpg')
#image.show()
xsize,ysize=image.size
r,g,b=image.split()
#r.show()
#g.show()
#b.show()
rdata=r.getdata() #data is now an array of length ysize*xsize
gdata=g.getdata()
bdata=b.getdata()

npr=numpy.reshape(rdata,(ysize,xsize))
npg=numpy.reshape(gdata,(ysize,xsize))
npb=numpy.reshape(bdata,(ysize,xsize))

red=pyfits.PrimaryHDU()
red.header.update('LATOBS',"32:11:56")
red.header.update('LONGOBS',"110:56")
red.header.update('TIME',time.ctime())
red.data=npr
red.writeto('red.fits')

green=pyfits.PrimaryHDU()
green.header.update('LATOBS',"32:11:56")
green.header.update('LONGOBS',"110:56")
green.header.update('TIME',time.ctime())
green.data=npg
green.writeto('green.fits')

blue=pyfits.PrimaryHDU()
blue.header.update('LATOBS',"32:11:56")
blue.header.update('LONGOBS',"110:56")
blue.header.update('TIME',time.ctime())
blue.data=npb
blue.writeto('blue.fits')
-------


Wayne Watson wrote:
> I'm trying to focus on a part of the thread above ("Getting Started 
> ...."), so that it isn't lost in that very long thread and is simpler to 
> deal with.  It appears that, at least, one can use pyfits to read fits 
> files, and one can embed this code in a Python program.  I posted the 
> coding aspects of doing this in a thread above, and finally someone 
> responded with code that looks like:
> 
>     from matplotlib import pyplot as plt
>     import pyfits
> 
>     image = pyfits.getdata('mpl51.fits')
> 
>     fig = plt.figure()
>     ax = fig.add_subplot(111)
>     ax.imshow(image)
>     fig.canvas.draw()
>     plt.show()
> 
> This is close to what I'm looking for. That is, executable Python code, 
> and not interactive code one enters line by line. Right now I'm lacking 
> a way to read, say, a jpg, fits, or gif file, and convert it to fits. 
> I'm also lacking a way to convert the internal image format to fits. The 
> latter is very important. The internal format is simple. An image 
> 640x480 pixels and 8-bits deep, b/w. jpg, etc. is already done for 
> writing the internal file.  For fits files, I'd like to write very 
> simple headers that have, say, lat/long of observer, a time stamp, and 
> some other assorted items.
> 
> Items like plotting, manipulating (IP, for example) and displaying are 
> secondary to the need to write a fits file from the internal file.

-- 
Megan Sosey
Science Software Branch
Operations and Engineering Division
Space Telescope Science Institute
3700 San Martin Drive, Baltimore MD 21218
Phone: +00.1.410.338.4365
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hs-2009-14-a-web.jpg
Type: image/jpeg
Size: 23490 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/astropy/attachments/20090408/727ace71/attachment-0001.jpg 


More information about the AstroPy mailing list