Write PNG
write_png(
image,
file = NULL,
palette = NULL,
use_filter = TRUE,
compression_level = -1L,
avoid_transpose = FALSE,
bits = 8,
trns = NULL,
raw_spec = NULL
)
If file
argument provided, function writes to file and returns
nothing, otherwise it returns a raw vector holding the PNG
encoded data.
image. Supported image types:
Numeric arrays with values in the range [0, 1], with 1, 2, 3 or 4 colour planes to represent gray, gray+alpha, rgb and rgba pixels, respectively
Rasters with a mixture of named colours (e.g. 'red'), and hex colours of the form #RGB, #RGBA, #RRGGBB and #RRGGBBAA
Integer arrays with values in [0,255] treated as 8-bit image data. Integer arrays with values in [0, 65535] treated as 16-bit image data
Integer matrices containing colurs in native format i.e. 8-bit RGBA values packed into a single integer
Can be saved as an indexed PNG
Vectors of raw bytes must be accompanied by a
raw_spec
which details how the bytes are to be interpreted
e.g. colour depth, width and height
If NULL (the default) then return PNG data as raw vector, otherwise write to the given file path.
character vector of up to 256 colors. If specified, and the image is a 2D matrix of integer or numeric values, then an indexed PNG is written where the matrix values indicate the colour palette value to use. The values in the matrix must range from 0 (for the first colour)
Use PNG filtering to help reduce size? Default: TRUE. If FALSE, then filtering will be disabled which can make image writing faster.
compression level for PNG. Default: -1 means
to use the default compression level. Other valid
values are in range [0, 9]. In general, lower compression levels
result in faster compression, but larger image sizes. For fastest
image writing, set compression_level
to 0 to completely disable compression.
Should transposition be avoided if possible so as to
maximise the speed of writing the PNG? Default: FALSE.
PNG is a row-major image format, but R stores data in column-major
ordering. When writing data to PNG, it is often necessary to transpose
the R data to match what PNG requires. If this option is set
to TRUE
then the image is written without this transposition and
should speed up PNG creation. This option only has an effect
for 2D integer and numeric matrix formats.
bit depth. default 8. Valid values are 8 and 16. This option only has an effect when image to output is a numeric array.
color to be treated as transparent
in RGB and Greyscale images - without specifying a full alpha channel.
Only a single color can be specified and it will be treated as a
fully transparent color in the image. This setting is only used
when writing RGB and Greyscale images. For 8-bit RGB images, the value
may be a hex color value i.e. "#RRGGBB"
or a vector of 3 numeric
values in the range [0, 255]. For 8-bit greyscale images,
must be a single integer value in the range [0, 255].
For 16-bit RGB images, the value
may be a vector of 3 numeric
values in the range [0, 65535]. For 16-bit greyscale images,
must be a single integer value in the range [0, 65535].
Default: NULL - means to not add a transparency color.
named list of image specifications for encoding a raw vector
to PNG. Use raw_spec()
to create such a list in the correct format.
This argument is only required if the image
argument is a
raw vector.
# create a small greyscale integer matrix, and write it to a PNG file
mat <- matrix(c(0L, 255L), 3, 4)
pngfile <- tempfile()
write_png(mat, file = pngfile)
im <- read_png(pngfile, type = 'raster')
plot(im, interpolate = FALSE)
Run the code above in your browser using DataLab