Create a JPEG image from an array or matrix.
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)
Either name of the file to write to, or a binary
connection, or a raw vector (
JPEG quality - a real number between 0 (lowest) and 1 (highest) controlling the quality of the output. Lower quality produces smaller, but more lossy files.
background color - used only if the input contains alpha channel since JPEG does not support storage of the alpha channel and thus the image needs to be flattened as if it was placed over the background of this color.
color space in which the image data is to be
interpreted. Defaults to the
writeJPEG takes an image as input and compresses it into JPEG
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. For
writeJPEG allows the source to include alpha
channel, but JPEG does NOT support alpha channel so it will be
blended against the specified background.
Alternative representation of an image is of
which is an integer matrix with each entry representing one pixel in
binary encoded RGBA format (as used internally by R). It can be
native = TRUE.
writeJPEG also supports raw array containing the RGBA
(or CMYK) 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 and CMYK) are supported and the processing of RGBA
is equivalent to that of a native raster.
The result is either stored in a file (if
target is a file
name), send to a binary connection (if
target is a connection)
or stored in a raw vector (if
target is a raw vector).
NULL if the target is either a file or connection, or a raw
vector containing the compressed JPEG image if the target was a raw
writeJPEG only produces 8-bit, non-progressive JPEG
format with no additional tags.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# read a sample file (R logo) img <- readJPEG(system.file("img","Rlogo.jpg",package="jpeg")) # write the image into a raw vector - using a low quality r <- writeJPEG(img, raw(), quality=0.3) # read it back again img2 <- readJPEG(r) # it will be slightly different since JPEG is a lossy format # in particular at the low quality max(abs(img - img2)) stopifnot(max(abs(img - img2)) < 0.4) # try to write a native raster img3 <- readJPEG(system.file("img","Rlogo.jpg",package="jpeg"), TRUE) r2 <- writeJPEG(img3, raw()) img4 <- readJPEG(r2, TRUE) # comparing nativeRaster values is not easy, so let's do write/read again img5 <- readJPEG(writeJPEG(img4, raw())) max(abs(img - img5)) stopifnot(max(abs(img - img5)) < 0.3)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.