Coyote's Guide to IDL Programming

Creating Color TIFF Images

QUESTION: How do I create a color TIFF image in IDL?

ANSWER: Creating color TIFF images in IDL is almost identical to creating color JPEG images. That is to say, you need to have a 24-bit image with the image color information built directly into the image.

If you have a 2D image to start with, you can construct the proper 24-bit image from the 2D image and the color table vectors. Suppose, for sake of argument, that you are running IDL on an 8-bit display (256 or fewer colors) and that you would like to make a color TIFF image of a filled contour plot that was on display in a graphics window. The code to do so might look like this:

First, create a 2D byte array of the window contents.

   scaled = TVRD()

Note that if you have a 2D image already, it is essential to scale the image into a byte array. Your code might look like this:

   scaled = BytScl(image2D, Top=!D.Table_Size-1)

Next, obtain the current color table vectors. These are the vectors that make up the current color table. Type this:

   TVLCT, r, g, b, /Get

Finally, construct the 24-bit image. Type this:

   s = Size(scaled, /Dimensions)
   image24 = BytArr(3, s[0], s[1])
   image24[0,*,*] = r[scaled]
   image24[1,*,*] = g[scaled]
   image24[2,*,*] = b[scaled]

Note that if you are running IDL on a 24-bit graphics display, that you can obtain a 24-bit image from the graphics window directly, like this:

   image24 = TVRD(True=1)

Now you are ready to write the TIFF file. But just one word of warning. IDL normally uses a bottom-to-top image display order (!Order=0). Most software that accepts TIFF images use a top-to-bottom display order. As a result, bringing an IDL-written TIFF image into a program such as Photoshop often results in an upside-down image.

To correct this problem, it is a good idea to reverse the row order of the 24-bit image before it is written into the file. This can be done with the Reverse command in IDL, like this:

   image24 = Reverse(image24, 3)

Finally, write the TIFF file using the Write_TIFF command, like this:

   Write_Tiff, 'test_image.tif', image24, 1

Beginning in IDL 5.2, TIFF images can be compressed, too. For example, to write the file above with LZW compression, the command would be this:

   Write_Tiff, 'test_image.tif', image24, 1, Compression=1

This is not the only way to create a color TIFF image. Be sure you read the on-line documentation for the Write_TIFF command for more details:

   IDL> ? Write_Tiff

UPDATE: Note that today most people run IDL on machines with 24-bit graphics display cards. This makes creating a 24-bit image considerably easier. All you have to do it read it from your display device. For example, you can create a TIFF file like this.

   image24 = TVRD(TRUE=1)
   Write_Tiff, 'test_image.tif', image24, 1

The cgSnapshot function from the Coyote Library takes all of the quess work out of this for you. It can recognize the depth of your current graphics device and make a color output file in TIFF, JPEG, PNG, GIF, and other file formats. Just prepare the current graphics window the way you want the image to look, and capture the output to a file of whatever type you like. For example, to make a TIFF file from the window contents, type this.

   void = cgSnapshot(Filename='test_image.tif')

You can do the same thing with cgImage (or any other Coyote Graphics program). To write an image directly to a PNG file, for example, you could type this.

   cgImage, image, Stretch=2, /Axes, Output='test_image.png'

If you have ImageMagick installed on your computer, the resulting raster file will be created from PostScript intermediate files, which results in extraordinarily high-quality font output. These files are of the same quality as the output from the very latest IDL graphics system.

Web Coyote's Guide to IDL Programming