3.1 First steps

Display an image

The following script loads the data of one FITS image, and displays the image. It must be saved in a ”.py” file:

# -*- coding: iso_8859_1 -*-
from aspylib import astro

#------ inputs ------
Folder = u"C:\\Images\\Jérôme\\"
Imagelist = [Folder + "Image-calibrated-1.fit"]

#--- loads and displays image ---
data = astro.get_imagedata(Imagelist)
astro.display(data)

raw_input()

Except for the first line, all lines starting with “#” are comments and are ignored by the Python interpreter.

The first and last lines will be used in most Python scripts :

  • the first line is required to specify the text file encoding.
  • the last statement “raw_input()” is necessary to prevent the script to close immediately after having been executed

The statement “from aspylib import astro” is needed to import the “astro” module from AsPyLib. Each time a function of this module is called, its name must take the form: astro.function().

Two other interesting things specific to the script:

  • the string called Folder as a “u” in front of it to tell Python that it is a Unicode string. The Unicode encoding is required if the path contains non-ASCII characters (the é and ô) so that it is handled properly.
  • the function astro.get_imagedata() works with a list of images, so a list of image names must be provided to it. That’s the reason why we put the name of the single image into square brackets: we make a list with one element.

As you see, the image display is relatively simple. It is actually programmed with only 35 lignes of code, and makes use of the Matplotlib module. This is one aspect of AsPyLib : in more traditional softwares, a large part of the programming effort is spent to deal with Windows objects : windows, menus, buttons, etc. In AsPyLib the internal code is kept simple, and 100% of the efforts is spent on the image processing operations.

_images/display_1.jpeg

The navigation buttons are very powerful to zoom in, navigate back, and the sliders allow to ajust the thresholds for visualisation. In order to close automatically the image, the following statement must be inserted at the beginning: “import matplotlib.pyplot as plt”, and then the image is closed with the statement “plt.close()”.

Display the difference between two images

The code below illustrates the use of image lists:

# -*- coding: iso_8859_1 -*-
from aspylib import astro

#------ inputs ------
Folder = u"C:\\Images\\"
Imagelist = [Folder + "Image-calibrated-" + str(i+1) + ".fit" for i in range(2)]

#--- loads and displays image ---
data = astro.get_imagedata(Imagelist)
astro.display(data[1,:,:]-data[0,:,:])

raw_input()

The list Imagelist will contain: Imagelist = [“C:\Images\Image-calibrated-1.fit”, “C:\Images\Image-calibrated-2.fit”] The statement that is used to construct the list is called a “list comprehension”. See section 5.1.4 of this page for further details.

The data of both images are stored in a single Numpy array called “data”. The i-th image is contained in data[i-1,:,:]. We see that array indexing starts at 0, as in C langage.