Stacking FITS photos

Rafael Ferreira
Jan 27 · 5 min read

Whether you are an amateur astronomer taking images of the night sky for pure amusement, or an astrophysicist sieving over large amounts of data to research star formation from various nebulas or examining young stellar objects (YSO’s), you will use one technique that is key to creating an image that is usable. This technique is called stacking.

  1. Background
  2. Using it in python
  3. Future works


You maybe asking, what exactly is stacking. Stacking is what it sounds like. You take the amount of photos you have taken of your object, in this case it will be M31 or otherwise called the Horsehead Nebula, and overlay these images on top of one another. The reason we do this is to reduce as much noise as possible from the image. By doing so, the image of our object becomes crisper with every stack we overlay , allowing for the benefit of enhanced signal-to-noise ratio.

M31 also known as the HorseHead Nebula.

Astronomers, astrophysicists, amateur astronomers, and hobbyists alike all use one specific sensor that enables for present image takers to photograph the night so well. A CCD or charge-coupled device, takes in an electrical signal and outputs an image or video. This is due to various wavelengths of the spectrum given off by our electromagnetic spectrum. Astronomers and astrophysicists use the same device in most, if not all of the current satellites, depending on what part of the electromagnetic spectrum you are trying to capture. However, these beautifully designed machines are not without their flaws. The flaw, in this case, is the amount of noise present in each photo of the celestial object. That is where stacking becomes key.

A stacked image of Jupiter showing how noise is being cancelled out. Each image iteration showing the improvement of the image via stacking.

Utilization of Python and Astropy:

In today’s age, astronomers and astrophysicists alike use data science as a tool to improve models, interpret data, and analyze our universe with even greater confidence. Using a tutorial from AstroPython on viewing and manipulating FITS images. A FITS image, as defined by Wikipedia, is an open standard for defining a digital file format which is useful for storage, transmission, and processing of data formatted as multi-dimensional arrays. Astronomers use these files to take in the information taken from their object of interest. In this case, we will be looking at the Horsehead nebula.

FITS image of the sombrero galaxy.

First, task your necessary packages to process the image. To do this you need to import the packages numpy, astropy, plyplt, fits, and LogNorm as I have down below.

Then, we intake the image we want to process either from your local computer or, as in this case, an outside source. We call it to a variable to make it easier for calling this image. Once we have our image we need to open up our image being able to grab the array that is contained in the PRIMARY part of the file. Once we have an array of the image we can plot the array using imshow. Imshow takes a 2D array and outputs a 2D image. The image below shows the gradient of the Horsehead Nebula.

The HorseHead Nebula produced from a FITS file. d

This image gives us a lot of detail about the Horsehead nebula including how the transition of the cloud of dust, gas that is forming the stars, and where the dust cloud stops. This image is great for analyzing what is occurring within this specific nebula, but noise is still present within this image. Now, we can stack the image to further reduce noise. Next, by using the power of a for loop, we can begin to stack the image over and over again, overlaying them as many times as we have images ultimately producing one file. In this example, I used 5 images stacked over each other, before, once again, extracting the array data.

Once I have this concatenated data, I can sum the list of all the arrays from each image and use another for loop to combine all the arrays into one final image.

Before we plot this image, we need to find out the best stretch, that is how best to fit the image on the page, by plotting a histogram of the final image that has been stacked. I can see that our range is from 2000 to 3000 for our color scaling and will use this data to plot the final image. And there you have it, our final image which denotes many of the characteristics of this nebula in a clear and concise manner.

Now we plot our final image with the parameters from the color scaling of 2000 to 3000.

Future Works:

In an upcoming project I will be using this method to stack images of the Small Magellanic Cloud (SMC) where the several YSO’s live, that I have classified in my thesis. This will assist in significantly reducing the amount of noise to get the best FIT image of the area of interest in the SMC. This will enable me to then extract the necessary information to plot the near infrared spectrum and properly determine the stellar characteristics of each of the YSO’s. To be continued….

Data Scientists must think like an artist when finding a solution, when creating a piece of code.Artists enjoy working on interesting problems, even if there is no obvious answer.

Rafael Ferreira

Written by

Undergraduate in Physics concentration in Astrophysics. Thesis in classification of Eight Young Stellar Objects in the SMC. Data Science Bootcamp Flatiron

Data Scientists must think like an artist when finding a solution, when creating a piece of code.Artists enjoy working on interesting problems, even if there is no obvious answer.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store