ImageMagick is a suite of tools used to manipulate images of various formats (including FITS files!). I use ImageMagick's convert utility to convert images from one format to another. For example

$ convert rose.jpg rose.png

or

$ convert rose.jpg -resize 50% -quality 50 rose_thumb.jpg

Image arithmetic

But besides simple image conversions, ImageMagick can also perform arithmetic on images, for example average, subtract or divide images (which can be used for dark- or flat-fielding). In the case you have several flatfield images you want to average, simply use the -average flag

$ convert flat001.png flat00n.png flat003.png -average flat_avg.png

Alternatively, you can specify the output format to be different from the input as well.

To use this flatfield to process images (divide raw images by the flatfield), use the -compose and -composite flags.

$ convert raw001.png flat_avg.png -compose Divide_Src \
  -composite -auto-level raw001-flat.png

Here we use raw001.png as input and divide it by flat_avg.png (-compose Divide_Src -composite). After that we equalize the image such that all values fit within the file format (-auto-level) and store it to disk (raw001-flat.png).

Instead of using -auto-level, you could also use the -evaluate flag to scale the image in a more predictable way, for example if you want to convert the images to a movie later on. In this case the previous line would read something like

$ convert raw001.png flat_avg.png -compose Divide_Src \
  -composite -evaluate multiply 0.5 raw001-flat.png

to scale the composite image by a factor 0.5. -evaluate can also be used to add, subtract or do other things.

Colorize images

ImageMagick can also be used to colorize images with an arbitrary gradient (in fact it can perform any function on any image with the -fx flag).

First we generate a discrete gradient (which will automatically be interpolated later):

$ convert -size 1x1 xc:#FFFFE5 xc:#FFF7BC xc:#FEE391 xc:#FEC44F xc:#FB9A29 \
  xc:#EC7014 xc:#CC4C02 xc:#993404 xc:#662506 \
  +append -rotate -90 paul_tol_linear_clut.png

(we used Paul Tol's optimal linear color scheme here). In a nutshell, this command makes several images (xc:) of 1 by 1 pixel (-size 1x1) of one color. The results are appended and then rotated -90 degrees before being stored to disk as paul_tol_linear_clut.png. It is important that the color gradient is vertical.

Once we have a gradient, we can use it to colorize an image:

$ convert test_image.png paul_tol_linear_clut.png \
  -fx 'v.p{0,u*v.h}'  test_image-colorized.png

Unforunately the -fx flag is rather slow because it is very general in nature. The -clut option should be faster and easier, but I did not get it to work.

$ convert -clut -interpolate bicubic test_image.png  paul_tol_linear_clut.png \
  test_image-colorized.png

Paul Tol's color schemes

For reference, I include Paul Tol's optimal color schemes here as well, both as discrete look up table and as gradient. The LUTs can be used to colorize any image as described above. The gradients are for illustration only.

Linear color map

convert -size 1x1 xc:#FFFFE5 xc:#FFF7BC xc:#FEE391 xc:#FEC44F xc:#FB9A29 \
  xc:#EC7014 xc:#CC4C02 xc:#993404 xc:#662506 +append \
  -rotate -90 paul_tol_linear_clut.png
convert -size 1x1 xc:#FFFFE5 xc:#FFF7BC xc:#FEE391 xc:#FEC44F xc:#FB9A29 \
  xc:#EC7014 xc:#CC4C02 xc:#993404 xc:#662506 +append \
  -filter Cubic -resize 600x50! paul_tol_linear_grad.png

paul_tol_linear_grad.png

Plus-minus color map

convert -size 1x1 xc:#3D52A1 xc:#3A89C9 xc:#77B7E5 xc:#B4DDF7 xc:#E6F5FE \
  xc:#FFFAD2 xc:#FFE3AA xc:#F9BD7E xc:#ED875E xc:#D24D3E xc:#AE1C3E \
  +append -rotate -90 paul_tol_plusmin_clut.png
convert -size 1x1 xc:#3D52A1 xc:#3A89C9 xc:#77B7E5 xc:#B4DDF7 xc:#E6F5FE \
  xc:#FFFAD2 xc:#FFE3AA xc:#F9BD7E xc:#ED875E xc:#D24D3E xc:#AE1C3E \
  +append -filter Cubic -resize 600x50! paul_tol_plusmin_grad.png

paul_tol_plusmin_grad.png

Rainbow color map

convert -size 1x1 xc:#781C81 xc:#42368F xc:#3F5CAB xc:#4580C1 xc:#519CB8 \
  xc:#5DA8A4 xc:#6DB388 xc:#94BD5F xc:#C5BA45 xc:#DEA63A xc:#E78833 \
  xc:#E55F2B xc:#D92120 +append -rotate -90 paul_tol_rainbow_clut.png
convert -size 1x1 xc:#781C81 xc:#42368F xc:#3F5CAB xc:#4580C1 xc:#519CB8 \
  xc:#5DA8A4 xc:#6DB388 xc:#94BD5F xc:#C5BA45 xc:#DEA63A xc:#E78833 \
  xc:#E55F2B xc:#D92120 +append -filter Cubic -resize 600x50! paul_tol_rainbow_grad.png

paul_tol_rainbow_grad.png

References

More information on: