EVOLUTION-MANAGER
Edit File: writePNG.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><title>R: Write a bitmap image in PNG format</title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="R.css" /> </head><body> <table width="100%" summary="page for writePNG {png}"><tr><td>writePNG {png}</td><td style="text-align: right;">R Documentation</td></tr></table> <h2> Write a bitmap image in PNG format </h2> <h3>Description</h3> <p>Create a PNG image from an array or matrix. </p> <h3>Usage</h3> <pre> writePNG(image, target = raw(), dpi = NULL, asp = NULL, text = NULL, metadata = NULL) </pre> <h3>Arguments</h3> <table summary="R argblock"> <tr valign="top"><td><code>image</code></td> <td> <p>image represented by a real matrix or array with values in the range of 0 to 1. Values outside this range will be clipped. The object must be either two-dimensional (grayscale matrix) or three dimensional array (third dimension specifying the plane) and must have either one (grayscale), two (grayscale + alpha), three (RGB) or four (RGB + alpha) planes. (For alternative image specifications see deatils)</p> </td></tr> <tr valign="top"><td><code>target</code></td> <td> <p>Either name of the file to write, a binary connection or a raw vector (<code>raw()</code> - the default - is good enough) indicating that the output should be a raw vector.</p> </td></tr> <tr valign="top"><td><code>dpi</code></td> <td> <p>optional, if set, must be a numeric vector of length 1 or 2 specifying the resolution of the image in DPI (dots per inch) for x and y (in that order) - it is recycled to length 2.</p> </td></tr> <tr valign="top"><td><code>asp</code></td> <td> <p>optional, if set, must be a numeric scalar specifying the aspect ratio (<code>x / y</code>). <code>dpi</code> and <code>asp</code> are mututally exclusive, speciyfing both is an error.</p> </td></tr> <tr valign="top"><td><code>text</code></td> <td> <p>optional, named character vector of entries that will be saved in the text chunk of the PNG. Names are used as keys. Note that the <code>"R.metadata"</code> key is reserved for internal use - see below</p> </td></tr> <tr valign="top"><td><code>metadata</code></td> <td> <p>optional, an R object that will be serialized into the <code>"R.metadata"</code> text key</p> </td></tr> </table> <h3>Details</h3> <p><code>writePNG</code> takes an image as input and compresses it into PNG format. The image input is usually a matrix (for grayscale images - dimensions are width, height) or an array (for color and alpha images - dimensions are width, height, planes) of reals. The planes are interpreted in the sequence red, green, blue, alpha. </p> <p>Alternative representation of an image is of <code>nativeRaster</code> class which is an integer matrix with each entry representing one pixel in binary encoded RGBA format (as used internally by R). It can be obtained from <code><a href="readPNG.html">readPNG</a></code> using <code>native = TRUE</code>. </p> <p>Finally, <code>writePNG</code> also supports raw array containing the RGBA image as bytes. The dimensions of the raw array have to be planes, width, height (because the storage is interleaved). Currently only 4 planes (RGBA) are supported and the processing is equivalent to that of a native raster. </p> <p>The result is either stored in a file (if <code>target</code> is a file name), in a raw vector (if <code>target</code> is a raw vector) or sent to a binary connection. </p> <p>If either <code>dpi</code> or <code>asp</code> is set, the <code>sPHy</code> chunk is generated based on that information. Note that not all image viewers interpret this setting, and even fewer support non-square pixels. </p> <h3>Value</h3> <p>Either <code>NULL</code> if the target is a file or a raw vector containing the compressed PNG image if the target was a raw vector. </p> <h3>Note</h3> <p>Currently <code>writePNG</code> only produces 8-bit, deflate-compressed, non-quantized, non-interlaced images. Note in particular that <code><a href="readPNG.html">readPNG</a></code> can read 16-bit channels but storing them back using <code>writePNG</code> will strip the 8 LSB (irrelevant for display purposes but possibly relevant for use of PNG in signal-processing if the input is truly 16-bit wide). </p> <h3>Author(s)</h3> <p>Simon Urbanek </p> <h3>See Also</h3> <p><code><a href="readPNG.html">readPNG</a></code> </p> <h3>Examples</h3> <pre> # read a sample file (R logo) img <- readPNG(system.file("img","Rlogo.png",package="png")) # write the image into a raw vector r <- writePNG(img) # read it back again img2 <- readPNG(r) # it better be the same identical(img, img2) # try to write a native raster img3 <- readPNG(system.file("img","Rlogo.png",package="png"), TRUE) r2 <- writePNG(img3) img4 <- readPNG(r2, TRUE) identical(img3, img4) ## text and metadata r <- writePNG(img, text=c(source=R.version.string), metadata=sessionInfo()) img5 <- readPNG(r, info=TRUE) attr(img5, "info") attr(img5, "metadata") </pre> <hr /><div style="text-align: center;">[Package <em>png</em> version 0.1-7 <a href="00Index.html">Index</a>]</div> </body></html>