Learn R Programming

magick (version 1.2)

editing: Image Editing

Description

Read, write and join or combine images. All image functions are vectorized, meaning they operate either on a single frame or a series of frames (e.g. a collage, video, or animation). Besides paths and URLs, the image_read() function supports all commonly used bitmap and raster types.

Usage

image_read(path, density = NULL, depth = NULL)

image_write(image, path = NULL, format = NULL, quality = NULL, depth = NULL, density = NULL, comment = NULL, flatten = FALSE)

image_convert(image, format = NULL, type = NULL, colorspace = NULL, depth = NULL, antialias = NULL)

image_display(image, animate = TRUE)

image_browse(image, browser = getOption("browser"))

image_join(...)

Arguments

path

a file, url, or raster object or bitmap array

density

resolution to render pdf or svg

depth

color depth (either 8 or 16)

image

magick image object returned by image_read() or image_graph()

format

output format such as "png", "jpeg", "gif", "rgb" or "rgba".

quality

number between 0 and 100 for jpeg quality. Defaults to 75.

comment

text string added to the image metadata for supported formats

flatten

should image be flattened before writing? This also replaces transparency with background color.

type

a magick ImageType classification for example grayscale to convert image to black/white

colorspace

string with a magick ColorspaceType for example "gray", "rgb" or "cmyk"

antialias

enable anti-aliasing for text and strokes

animate

support animations in the X11 display

browser

argument passed to browseURL

...

several images or lists of images to be combined

Details

Besides functions above, all standard base vector methods such as [, [[, c(), as.list(), as.raster(), rev(), length(), and print() can be used with magick images.

Use the standard img[i] syntax to extract a subset of the frames from an image. The img[[i]] method is used to extract a single frame as a bitmap object, i.e. a raw matrix with pixel values.

X11 is required for image_display() which is only works on some platforms. A more portable method is image_browse() which opens the image in a browser. RStudio has an embedded viewer that does this automatically which is quite nice.

See Also

Other image: analysis, animation, attributes, color, composite, device, effects, index, ocr, painting, transform

Examples

Run this code
# NOT RUN {
# Download image from the web
frink <- image_read("https://jeroen.github.io/images/frink.png")
worldcup_frink <- image_fill(frink, "orange", "+100+200", 30000)
image_write(worldcup_frink, "output.png")

# extract raw bitmap array
bitmap <- frink[[1]]

# replace pixels with #FF69B4 ('hot pink') and convert back to image
bitmap[,50:100, 50:100] <- as.raw(c(0xff, 0x69, 0xb4, 0xff))
image_read(bitmap)

# Plot to graphics device via legacy raster format
raster <- as.raster(frink)
par(ask=FALSE)
plot(raster)

# Read bitmap arrays
curl::curl_download("https://www.r-project.org/logo/Rlogo.png", "Rlogo.png")
image_read(png::readPNG("Rlogo.png"))

curl::curl_download("https://jeroen.github.io/images/example.webp", "example.webp")
image_read(webp::read_webp("example.webp"))

curl::curl_download("http://jeroen.github.io/images/tiger.svg", "tiger.svg")
image_read(rsvg::rsvg("tiger.svg"))
# }

Run the code above in your browser using DataLab