Creating Color Images from IDL Graphics Windows
QUESTION: Can you show me how to create a color (JPEG, TIFF, BMP, PNG) image from an IDL graphics window?
ANSWER: Use cgSnapshot and your problem is solved. :-)
IDL> image = cgSnapshot(/JPEG) IDL> image = cgSnapshot(/TIFF) IDL> image = cgSnapshot(/BMP) IDL> image = cgSnapshot(/PNG)
Or, simply display your graphics routines in a resizeable graphics window, cgWindow, that can automatically create these kinds of files (as well as PostScript files) for you.
But here are the deep, hidden mysteries in the bowels of that program.
First, make sure the window you want to make an image of is the current graphics window. (You would be surprised how many people forget this most important step.)
IDL> WSet, windowContainingImage
Next, determine if your current graphics display is an 8-bit graphics device (Z graphics buffer, older X Term windows, etc.) or a 24-bit graphics device. If you are on a 24-bit graphics device, make sure color decomposition is on. (This applies only if you want your code to be cross-platform and you would be a fool if you didn't.) In a program, I usually have code like this:
IF (!D.Flags AND 256) NE 0 THEN BEGIN Device, Get_Decomposed=theDecomposedState, Get_Visual_Depth=theDepth IF theDepth GT 8 THEN BEGIN Device, Decomposed=1 color24 = 1 ENDIF ELSE truecolor = 0 ENDIF ELSE BEGIN color24 = 0 theDepth = 8 ENDELSE
Next, take a snapshot of the image in the window. Note that if you are on an 8-bit graphics display, this will be a 2D image. Whereas if you are on a 16-bit or 24-bit graphics display, it will be a 24-bit pixel interleaved image.
image = TVRD(True=color24)
Finally, suppose the variable fileType is the kind of image you wish to make, say "JPEG". Then, to create the color image, your code might look like this. Remember, the variable color24 is set to 0 if you took the snapshot on a 8-bit graphics device and to 1 otherwise. Note that if you have an 8-bit image, you will have to create a color 24-bit image, most of the time, by running the image value through the color table values, which you obtain with TVLCT and the Get keyword.
CASE StrUpCase(fileType) OF 'BMP': BEGIN IF color24 THEN BEGIN ; BMP files assume blue, green, red planes. Write_BMP, filename, Transpose(image[2,1,0]), _Extra=extra ENDIF ELSE BEGIN TVLCT, r, g, b, /Get Write_BMP, filename, image, r, g, b, _Extra=extra ENDELSE END 'JPEG': BEGIN IF color24 THEN BEGIN image3D = image ENDIF ELSE BEGIN TVLCT, r, g, b, /Get image3D = [ [[r[image]]], [[g[image]]], [[b[image]]] ] image3D = Transpose(image3d,[2,0,1]) ENDELSE Write_JPEG, filename, image3D, True=1, Quality=quality, _Extra=extra END 'PNG': BEGIN IF color24 THEN BEGIN Write_PNG, filename, image, _Extra=extra ENDIF ELSE BEGIN TVLCT, r, g, b, /Get image2D = image Write_PNG, filename, Reverse(image2D,2), r, g, b, _Extra=extra ENDELSE END 'TIFF': BEGIN IF color24 THEN BEGIN image3D = Reverse(image,3) ENDIF ELSE BEGIN TVLCT, r, g, b, /Get image3D = [ [[r[image]]], [[g[image]]], [[b[image]]] ] image3D = Transpose(image3d,[2,0,1]) image3D = Reverse(Temporary(image3D), 3) ENDELSE Write_TIFF, filename, image3D, 1 END ENDCASE
High Quality Raster Files
This method of taking a snapshot of the graphics window to create an output raster file is what I would call the "low-resolution" way of creating raster file output. It works, but the results aren't very good if you want high-quality graphics and fonts.
To achieve high-quality raster output, using these kind of direct graphics commands, you have to create PostScript intermediate files from your graphics routines, and then use ImageMagick to convert the PostScript files to raster format. It is trivially easy to write graphics program that can display themselves in a display window and in a PostScript file using the Coyote Graphics routines described on this web page and in my book, Coyote's Guide to Traditional IDL Graphics. This is how all the graphics output in the Coyote Plot Gallery is created. If you can create a PostScript file of your graphical output, you can convert it to a high-quality raster file, using ImageMagick, with the Coyote Library program cgPS2Raster.
Last Updated 28 May 2013