Creating a Transparent Image in IDL

QUESTION: How can I create a graphics output image with a transparent background for inclusion in a software program like PowerPoint?

ANSWER: The best way to do this is to create a PNG image with WRITE_PNG and the TRANSPARENT keyword. Unfortunately, the documentation (at least through IDL 5.5) might well lead you astray.

The TRANSPARENT keyword should be set equal to a 256-element array, which will represent the 256 possible values in a 2D byte image. If the element in the TRANSPARENT vector is zero, then all the pixels in the image having the value represented by the vector element will be zero. In other words, if t[100] = 0 then all pixels in the image having value 100 will be transparent. If the value in the element in the TRANSPARENT vector is 255, then the corresponding pixel value in the image will be totally opaque. In the absence of information, the transparent vector is assumed to have a value of 255.

Note that it is possible to create image pixels that are semi-transparent by setting the corresponding TRANSPARENT vector elements to some number between 0 and 255. A value of 128, for example, would result in image pixels that were half transparent and half opaque.

Here is a test program that demonstrates how to create a plot in IDL with a transparent background.

PRO Test

  ; Load display colors.
  
  LoadCT, 0
  TVLCT, 255, 255, 255, !D.Table_Size-2 ; White
  TVLCT,   0, 255,   0, !D.Table_Size-3 ; Green
  TVLCT, 255,   0,   0, !D.Table_Size-4 ; Red
   
  ; Create the plot in the Z-buffer to insure an 8-bit image
  ; for output to the PNG file.

  thisDev = !D.Name
  Set_Plot, 'Z', /Copy
  Erase
  Plot, findgen(11), Color=!D.Table_Size-4, $
     Background = !D.Table_Size-2, /NoData
  OPlot, findgen(11), Color=!D.Table_Size-3
  snapshot = TVRD()
  Set_Plot, thisDev

  ; Create the transparent vector. Set all values to 255 (opaque).

  t = BytArr(256) + 255B

  ; Set the background value (!D.Table_Size-2) to 0

  t[!D.Table_Size-2] = 0

  ; Create the PNG file.
  
  WRITE_PNG,'transparent_test.png', snapshot, r, g, b, TRANSPARENT = t
  
END

Google
 
Web Coyote's Guide to IDL Programming