The WRITE_PNG procedure writes a 2-D or 3-D IDL variable into a Portable Network Graphics (PNG) file or to a buffer. The data is stored using lossless compression with either 8 or 16 data bits per channel, based on the input IDL variable type. True color (3D) variables must have the number of channels as their leading dimension (pixel interleaved). For BYTE format indexed-color (2D) variables, an optional palette may be stored in the image file along with a list of pixel values which are to be considered transparent by a reading program.

Examples


The following lines create an image in an IDL graphics window, read it from the window and write it as a .png file in the temporary directory, then read the .png file and display it in the same graphics window:

; Save device settings and tell IDL to use a color table
DEVICE, GET_DECOMPOSED=old_decomposed
DEVICE, DECOMPOSED=0
LOADCT, 14
; Create an image and display it
IMAGE1 = DIST(300)
WINDOW, 1, XSIZE=300, YSIZE=300
TV, IMAGE1
; Write a PNG file to the temporary directory
; Note the use of the TRUE keyword to TVRD
filename = FILEPATH('test.png', /TMP)
WRITE_PNG, filename, TVRD(/TRUE)
PRINT, 'File written to ', filename
; Read in the file
IMAGE2 = READ_PNG(filename)
; Display the IMAGE1 and IMAGE2 side by side
; Note the use of the TRUE keyword to TV
WINDOW, 1, XSIZE=600, YSIZE=300, $
   TITLE='Original (left) and Image read from file (right)'
TV, IMAGE1, 0
TV, IMAGE2, 1, /TRUE
; Restore device settings.
DEVICE, DECOMPOSED=old_decomposed

Syntax


WRITE_PNG, Filename, Image[, R, G, B] [, BUFFER=variable] [, COMPRESSION=value] [, /ORDER] [, TRANSPARENT=array] [, /VERBOSE] [, XRESOLUTION=value] [, YRESOLUTION=value]

Arguments


Filename

A scalar string containing the full pathname of the PNG file to write.

Image

The array to write into the new PNG file. If Image is one of the integer data types, it is converted to type unsigned integer (UINT) and written out at 16 data bits per channel. All other data types are converted to bytes and written out at 8-bits per channel.

Note: If Image is two-dimensional (single-channel) and R, G, and B are provided, all input data types (including integer) are converted to bytes and written out as 8-bit data.

R, G, B

For single-channel images, R, G, and B should contain the red, green, and blue color vectors, respectively. For multi-channel images, these arguments are ignored.

Keywords


BUFFER

Set this keyword to a named variable in which to return the PNG stream instead of writing the stream to a file. If this keyword is set then the Filename is ignored. This keyword is useful when you want to convert the image to a Base-64 encoded image (using IDL_BASE64) and embed it in a web page.

COMPRESSION

Set this keyword to the compression level for the PNG data. The value must be in the range 0 to 9. A value of 0 is no compression. Higher values will give smaller files but will be slower to write and read. The default is 6, which gives a good balance between compression and speed.

ORDER

Set this keyword to indicate that the rows of the image should be written from bottom to top. The rows are written from top to bottom by default. ORDER provides compatibility with PNG files written using versions of IDL prior to IDL 5.4, which wrote PNG files from bottom to top.

TRANSPARENT

Set this keyword to an array of opacity values (an opacity table) in the range of 0 to 255. A value of 0 (zero) equals full transparency (no opacity). If you specify fewer opacity values than exist in the color table, the missing values are replaced by 255.

This keyword is valid only if Image is a single-channel (color indexed) image and the R, G, B palette is provided.

VERBOSE

Produces additional diagnostic output during the write.

XRESOLUTION

Set this keyword to the horizontal resolution (in dots per inch) of the image. This keyword does not affect the actual image data. Internally, the resolution is converted to pixels per meter and stored in the PNG file header for use by other applications. The default behavior is to not write out this attribute.

YRESOLUTION

Set this keyword to the vertical resolution (in dots per inch) of the image. This keyword does not affect the actual image data. Internally, the resolution is converted to pixels per meter and stored in the PNG file header for use by other applications. The default behavior is to not write out this attribute.

Additional Examples


The following example demonstrates the use of the TRANSPARENT keyword:

; Create ramp image
img = BINDGEN(256,256)
 
; R, G, B color table - make everything red
r = REPLICATE(255b,256)
g = REPLICATE(0b,256)
b = g
 
; Set "Transparency" to a ramp
transp = BINDGEN(256)
 
; This image is more transparent on the left
WRITE_PNG, 'test_transp.png', img, r,g,b, TRANSPARENT=transp
 
; This returns the transparency/opacity table again
img2 = READ_PNG('test_transp.png', r2,g2,b2, TRANSPARENT=t2)
 
; Convert from indexed color to an RGBA image
imgRGBA = BYTARR(4,256,256)
imgRGBA[0,*,*] = r2[img2]
imgRGBA[1,*,*] = g2[img2]
imgRGBA[2,*,*] = b2[img2]
imgRGBA[3,*,*] = t2[img2]
 
i = IMAGE(imgRGBA)

Here, we demonstrate how to use the BUFFER keyword:

img = BYTSCL(RANDOMU(seed, 3, 20, 20))
; Write to a buffer, then convert to Base-64 encoding
WRITE_PNG, "", img, BUFFER=b
imgdata = IDL_BASE64(b)
print,'<img src="data:image/png;base64,'+imgdata+'"/>'

IDL prints:

<img src="data:image/png;base64,iVBORw0KGgoAA..."/>

Version History


5.2

Introduced

8.5.1

Added BUFFER keyword

9.1

Added COMPRESSION keyword

See Also


READ_PNG, QUERY_* Routines, IDL_BASE64