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 :

You can crop, rebin, mask, choose zscales, conveniently annotate ...

... and even compose multiple images into a single png.

Of course you can also use it on much larger files.

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.
# 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))



Download & Installation

Type :

svn export https://svn.epfl.ch/svn/mtewes-public/trunk/f2n ./f2n
You will get the module, sample code, and documentation. Of course you could replace export by checkout.
cd f2n
python setup.py install
or maybe
python setup.py install --user
if 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.

Version history

Enjoy !

And don't hesitate to ask me if you have questions or trouble running/installing it.