f2n.py - fits to net
f2n is a tiny python module to transfrom FITS images into PNG files, built around pyfits and PIL. I tend to include these PNG files into html pages for fast visualization, that's why the module is called "fits to net".
Note right away that to simply convert your FITS files into a general purpose image format, there is now also a tool called STIFF that you might want to check out.
But f2n has some extra features that go beyond file conversion. Some of them are illustrated by the png images below, all made directly by f2n :
f2n.py is fast, actually much faster then the older f2n in C. There is no problem to run it on your 4k x 4k images in about one second, also the rebinning is very speedy.
Here is some sample code that generated the topmost of the above images (with lots of comments) :
import f2n myimage = f2n.fromfits("example.fits") # We crop the FITS image (xmin, xmax, ymin, ymax). myimage.crop(70, 170, 60, 160) # The image is now 100 x 100 pixels. myimage.setzscale(4500, "ex") # z2 = "ex" means extrema -> maximum in this case. myimage.makepilimage("lin", negative = False) # We can choose to make a negative image. myimage.showcutoffs(redblue = True) # Pixels below z1 will be blue, above z2 will be red. # We upsample (= "zoom" without interpolation) the pixels. myimage.upsample(5) # The image is now 500 x 500 # We draw some labels and circles. myimage.drawstarsfile("stars.cat", r=3, colour=(255, 0,0)) # Note that all the coordinates are those from the original image ! # Of course, the function drawstars allows you to the same # without any catalog file. # Same for drawing a rectangle... myimage.drawrectangle(118, 149, 137, 155, colour=(0,255,0), label="Empty region") myimage.writetitle("Hello World !", colour=(200, 200, 0)) myimage.writeinfo(["This is a demo", "of some possibilities", "of f2n.py"], colour=(255,100,0)) myimage.tonet("2_single.png")
- python (something > 2.5 ... not tested on earlier versions)
- PIL (version 1.1.6, that is the latest version, with the fromarray() function)
Yes, we do need this latest version, 1.1.5 will not work. But PIL is very easy to update. To find out your current version :
>>> import Image >>> print Image.VERSION 1.1.6
svn export https://svn.epfl.ch/svn/mtewes-public/trunk/f2n ./f2nYou will get the module, sample code, and documentation. Of course you could replace export by checkout.
cd f2n python setup.py installor maybe
python setup.py install --userif you don't have write access to the global site-packages directory of your machine. See the help on Installing Python Modules to learn more.
Proceed as in the demo scripts provided in the distribution directory ! Note that they can be executed even before installing f2n.
- 1.2 : June 2012, nothing really new, but better packaging.
- 1.1 : December 2009, improved auto cutoffs (now also for flat fields), "rainbow" scale, rectangle drawings, CLI options...
- 0.3 : June 2009, first "release".
- Before this, there was f2n.c ...
And don't hesitate to ask me if you have questions or trouble running/installing it.