Fanning Software Consulting

IDL Output for Web Display

QUESTION: I often want to display IDL output on my web page or in a Powerpoint presentation for my colleagues, but I have trouble making the display look as professional as I would like it to look. The fonts, in particular, are often lousy. Do you have any suggestions for how I might be able to create my IDL output so that it looks better in these presentation settings?

ANSWER: Yes, I have one highly recommended suggestion. Get a copy of ImageMagick and install it on your computer. It is available for all kinds of computers, including Macs and Windows computers, and it is free. As far as I am concerned, it is essential for producing professional looking IDL output that is suitable for display on the Internet or in a Powerpoint slide presentation.

Note: Producing high-quality PostScript and raster file output can now be done automatically from resizeable graphics windows, using Coyote Graphics routines. This is described in detail in my new book, Coyote's Guide to Traditional IDL Graphics (see the book image just to the right). It is also possible to create high-quality output via the techniques described below with any Coyote Graphics command directly via the Output keyword.

To see how ImageMagick can be used, let's first write a graphics display program in IDL. Here is one that produces a contour plot. (Numerous Coyote Library programs are required to run this code.) Note that we take care in writing the program to make sure it works both on our display, and in PostScript. Mostly this is a matter of protecting certain commands that cannot be executed in the PostScript device. In this case, we are protecting a Device command with a Decomposed keyword, but it is often a Window or WSet command that needs protecting.

   PRO Graphic_Display

      ; Create some data.
      peak = cgDemoData(2)
      lon = Scale_Vector(Findgen(41), -138.5, -135.2)
      lat = Scale_Vector(Findgen(41), 38.5, 40.6)
        
      ; Set number of levels.
      levels = 12
    
      ; Load display colors and set up window.
      cgLoadCT, 4, NColors=levels, Bottom=1, /Brewer
    
      ; Draw the contour plot.
      step = (Max(peak) - Min(peak)) / levels
      userLevels = IndGen(levels) * step + Min(peak)
      cgContour, peak, lon, lat, /Fill, C_Colors=Indgen(levels)+1, $
         Levels=userLevels, Position=[0.15, 0.15, 0.95, 0.80], $
         XTitle='Longitude', YTitle='Latitude', XStyle=1, YStyle=1
      cgContour, peak, lon, lat, /Overplot, Levels=userLevels, /Follow
       
      ; Draw a colorbar. 
      cgColorbar, NColors=12, Bottom=1, Divisions=levels, $
         Range=[Min(peak), Max(peak)], Format='(I4)', $
         Position = [0.15, 0.9, 0.95, 0.95], Color=FSC_Color('black'), $
         Title='Elevation', XTickLen=1, CharSize=(!D.Name EQ 'PS') ? 1.25 : 0.8
   END

We can run the program like this.

   IDL> cgDisplay, 500, 400
   IDL> Graphic_Display

The standard way to create a PNG file from this graphics window is to simply use cgSnapshot, like this.

   IDL> void = cgSnapshot(/PNG, FILENAME='test_1')

You can see what the test_1.png output file looks like in the figure below.

The standard way of getting PNG output.
This is a normal screen capture of IDL graphics output with the output
sent to a PNG file.
 

Certainly this is nothing to write home about. And the fonts look particularly hopeless. But, rest assured, there is a better way.

Using ImageMagick to Produce IDL Output

A better idea is to take advantage of IDL's ability to produce PostScript output, with stylish true-type fonts, smooth lines, etc. This is where ImageMagick comes in. With ImageMagick, you can convert IDL PostScript output into all kinds of image output using its convert command. The PostScript set-up commands I use (PS_Start and PS_End) are designed to work with ImageMagick directly (if it is installed properly on your machine) to produce both PostScript files, and the corresponding JPEG, PNG, or TIFF files. Here is how I would go about producing the same PNG output as above, but this time using ImageMagick to do the conversion from PostScript for me. Note that PS_Start will set up your PostScript device in such a way that the "window" on the PostScript page will have the same aspect ratio as the currently open window on your display device.

   IDL> PS_Start, FILENAME='test_2.ps'
   IDL> Graphic_Display
   IDL> PS_End, /PNG

The resulting test_2.png output file can be seen in the figure below. I think you will agree that this result looks considerably more professional.

PostScript file converted to PNG with ImageMagick
The IDL PostScript file has been converted to PNG output with ImageMagick.
 

(Note that if you are unfamilar with the PS_Start/PS_End routines from the Coyote Library you will want to download them )

If you don't want to use PS_END to automatically do the conversion for you, you can always convert your PostScript files yourself, by spawning the ImageMagick command from within an IDL session. For example, the Spawn command I use in PS_End looks like this.

   IDL> Spawn 'convert test_2.ps test_2.png'

User Reaction

Brian Larson points out that if you use one of the newer Macintosh computers, that PS Converter does a better job of creating nice looking output than ImageMagick. The only downside, he reports, is that you can't call PS Converter from within IDL, but instead you have to double-click on the PostScript file.

Paolo Grigis points out that if one wants a high resolution PNG image from the PostScript file, you should use this ImageMagick command to produce the file.

   convert -density 300 file.ps -resize 25% file.png

He adds that to see why this is needed, convert any PostScript file with, say, six plots on it with and without the density option. Note that if I try this with the file above, it does produce a higher quality file (see below), but at the expense of a larger file. The first PNG file is about 15K in size, the second is about 79K, and the third is about 162K. (Editor's Note: I've changed the default ImageMagick command in PS_END to this now.)

PostScript file converted to PNG with ImageMagick
The IDL PostScript file has been converted to PNG output with ImageMagick with higher density.
 

Reimar Bauer reports that on his LINUX machine he prefers the pstoimg command instead of convert. He particularly likes the -aaliastext option, which uses GhostScript's anti-aliasing feature for smoother font characters. He notes that GhostScript 4.03 or higher is needed for this. The command he uses is this:

   pstoimage -type png -crop tblr -aaliastext -flip r90 -density 150 myfile.ps

It is possible to do multiple files at the same time with this command.

Paul Krummel writes:

G’day David. I have just got back from overseas and have been catching up on the IDL newsgroup posts. The ‘plotting graphics for web pages’ posts caught my eye and I thought I would send you a quick message on this topic and mention what I use. Like many people, I plot all my IDL graphics to PostScript and then convert to images (similar to what many people said on the IDL newsgroup), however I do not use ImageMagick, but instead us GhostScript. I have been using GhostScript on a windows PC for many years to do this and a few years ago wrote an IDL routine to interface with it (see gs_convert.pro). This routine could be easily extended to run under LINUX as well. GhostScript has some nice conversion options (e.g., anti-aliasing of text and graphics), support for multiple output images from multi-page PostScript files, etc. The output has always looked great. I usually convert to PNG images.

Another routine I use along with gs_convert.pro is crop_image.pro (see attached) for automatically cropping an image to remove “white space,” which might also be of interest. I have attached a zip file containing these two files along with four other wrapper routines to these two. These are probably not the best written routines but they do the job for me and I thought that you may find them useful or interesting. Please make them available to the IDL community if you like.

Ken Bowmann writes that on his Macintosh machine he finds he has to add the -flatten option to ImageMagick to get it to do what he wants:

   convert -density 300 file.ps -resize 25% -flatten file.png

He also reports that if you want to sort out IDL's problem of using Seascape instead of Landscape output, that you should use the -rotate 270 option. This will rotate the output properly for inclusion into other software.

   convert -density 300 file.ps -resize 25% -flatten -rotate 270 file.png

PS_END has been configured so you can add your own convert parameters to the call. Here is how I would create a PNG output of a histogram plot with the current default options.

   IDL> PS_Start, FILENAME='test.ps'
      PostScript output will be created here: C:\IDL\test.ps
   IDL> cgHistoplot, Loaddata(7), /FILL
   IDL> PS_END, /PNG
      convert -alpha off -density 300 "C:\IDL\test.ps" -resize 25% -flatten  -rotate 270 "PNG24:C:\IDL\test.png"

Note that these commands can be invoked more simply like this. This is a short-hand way of producing file output from any Coyote Graphics routine.

   IDL> cgHistoplot, Loaddata(7), /FILL, OUTPUT='test.png'

The output can be seen in the figure below.

PostScript file converted to PNG with ImageMagick
The IDL PostScript file has been converted directly to PNG output with the ImageMagick convert command and default settings.
 

Version of IDL used to prepare this article: IDL 7.0.1.

Last Updated: 11 March 2011