Astrophotography with ImageMagick

I am not a very dedicated (or experienced) astrophotographer, but every now and then I feel like taking a photo of the sky. On high ISO and long exposures, you’ll find plenty of noise. Also, if you live as close to a city as I do, you’ll want to subtract background and correct for field distortions, to improve the (contrast-enhanced) images. Here is how to do that from the command line, using ImageMagick.

ImageMagick is free and open-source software (FOSS) and can be found on The package contains several commands, from which we will use the composite and convert programs.

Apart from your normal image, the light frame, you’ll need (at least) three more images:

  • the dark frame, taken with e.g. the cap on lens, and using the same ISO, same exposure time as the light frame
  • the flat field, a uniform, white field (e.g. unfocussed white computer screen at ~75% of optimal exposure time), using the same ISO and focal length as the light frame
  • the bias frame, taken with e.g. the cap on lens, using the same ISO as for the light frame, but with the shortest exposure time available

If you’re an astraholic (i.e. less lazy than I), you can take many images for each of these and combine them. Either way, in the following I’ll refer to all four files as light.jpg, dark.jpg, flat.jpg and bias.jpg. In principle you should take these frames under the same circumstances as much as possible. The dark and bias frames can be taken between the lights to ensure that e.g. the temperature, battery voltage, and other conditions that can influence the CCD are roughly the same. However, if you forgot to take any of the additional frames, taking them later is probably better than doing nothing at all (dead pixels usually don’t move around very fast).

Before you start, ensure that all images have the same orientation. One way of doing this is by enforcing the default orientation (losing the original orientation of your light) for all of your frames:

  • convert -orient Top-left input.jpg output.jpg

The theory for post-processing the images is rather simple:

  1. subtract the dark from the light, to get rid of hot pixels and other noise in the long expposure
  2. subtract the bias from the flat, to do the same for the expposure
  3. divide the corrected light by the corrected flat to remove field distortions
  4. optionally, stretch the colour levels of the final image

(Actually, some people suggest to subtract the bias from the light and dark as well. Firstly, I’d think that (light – bias) – (dark – bias) = light – dark, so that the outcome should be identical. Secondly, at least ImageMagick wraps values around 255 (e.g. 1 – 3 = -2 + 256 = 254) so that this gives a weird outcome (as may be expected when rounding off intermediate results).)

In ImageMagick language, this comes down to:

  1. composite flat.jpg -compose subtract bias.jpg flat-bias.jpg
  2. composite light.jpg -compose subtract dark.jpg light-dark.jpg
  3. composite light-dark.jpg -compose divide flat-bias.jpg light-flat.jpg
  4. convert light-flat.jpg -auto-level flat-final.jpg

The above operations are coded up in the bash script, which can be found at the bottom of this page.

After calibrating your images this way, you will still want to play with the colour levels, but that should be done by eye, as your desires will change from picture to picture. I use GIMP for that, a FOSS “Photoshop” with a perfect price/quality ratio, which can be dowloaded from


##  Calibrate a photo, applying dark, bias and flat-field corrections
##  Copyright (c) 2013 AstroFloyd -

if [[ ${#} -ne 3 && ${#} -ne 4 ]]
    echo -e "\nCalibrate a photo, applying dark, bias and flat-field corrections"
    echo -e "  Syntax: ${0} <light frame> <dark frame> <flat field> [<bias frame>]"
    echo -e "  If a bias frame is not provided, the dark frame is used to calibrate the flat field\n"
    echo -e "  light frame:  actual photo with desired focal length, ISO and exposure"
    echo -e "  dark frame:   cap on lens, same ISO, same exposure as light frame"
    echo -e "  flat field:   same f as light frame, unfocussed on uniform, white background"
    echo -e "  bias frame:   cap on lens, same ISO as light frame, shortest exposure"
    echo ""
    exit 1

if [ ! -e .calibrate_photo_\*.jpg ]
    echo -e "\nSome existing calibration files were found.  I will use them."
    echo "To clean this directory, do:  rm -f .calibrate_photo_*.jpg"

# File names:

light_final=`echo ${1} | sed -e 's/\(\..*\)/_calibrated\1/'`

# Copy the source files using convert
# Ensure that the orientation is the same for all of them
echo -e "\nCopying and rotating originals..."
convert -orient Top-left ${1} ${light}
if [ ! -e ${dark} ]; then convert -orient Top-left ${2} ${dark}; fi
if [ ! -e ${flat} ]; then convert -orient Top-left ${3} ${flat}; fi
if [ ! -e ${flat} ]
    if [[ ${#} -eq 4 ]]
	convert -orient Top-left ${4} ${bias}
	cp -f ${dark} ${bias}

# Calibrate flat frame - subtract bias from the flat frame:
if [ ! -e ${flat_bias} ]
    echo "Correcting bias in flat field..."
    composite ${flat} -compose subtract ${bias} ${flat_bias}
# Apply calibration frames:
#   subtract the dark from the light frame:
echo "Applying dark calibration..."
composite ${light} -compose subtract ${dark} ${light_dark}

#   divide the result by the flat frame:
echo "Applying flat-field calibration..."
composite ${light_dark} -compose divide ${flat_bias} ${light_flat}

#   normalise the result:
echo "Normalising the result..."
convert ${light_flat} -auto-level ${light_final}

# Clean up
#echo "Cleaning up..."
#rm -f .calibrate_photo_*.jpg

echo -e "Calibration completed.\nCalibrated image saved as ${light_final}\n"

Syntax highlighted by Code2HTML
This entry was posted in Astronomy, Software and tagged , , , , , , , , . Bookmark the permalink.

4 Responses to Astrophotography with ImageMagick

  1. Fliker09 says:

    Thanks for this post! As rational idea – better use dark flat instead of bias for clearing up flat

  2. Fausto says:

    Very nice post. Any ideas on how to map and correct for field distortions using ImageMagick? I’m mostly interested on how to map it.

  3. AstroFloyd says:

    No, sorry can’t help you there. I’ve played with xmorph a long time ago though.

  4. paulbgdd says:

    Thanks a lot. It would be really nice to have a set of imagemagick commands for all common astronomy image processing. Another interessting command is combine all flats into a master flat via: convert Flats_001.cr2 -evaluate-sequence median masterFlat.tiff

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s