editing: Image Editing

Description Usage Arguments Details See Also Examples

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, image_read() supports commonly used bitmap and raster object types.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
image_read(path, density = NULL, depth = NULL, strip = FALSE)

image_read_svg(path, width = NULL, height = NULL)

image_read_pdf(path, pages = NULL, density = 300, password = "")

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_data(image, channels = NULL, frame = 1)

image_raster(image, frame = 1, tidy = TRUE)

image_display(image, animate = TRUE)

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

image_strip(image)

image_blank(width, height, color = "none", pseudo_image = "")

image_join(...)

image_attributes(image)

demo_image(path)

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)

strip

drop image comments and metadata

width

in pixels

height

in pixels

pages

integer vector with page numbers. Defaults to all pages.

password

user password to open protected pdf files

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

string with imagetype value from image_types for example grayscale to convert into black/white

colorspace

string with a colorspace from colorspace_types for example "gray", "rgb" or "cmyk"

antialias

enable anti-aliasing for text and strokes

channels

string with image channel(s) for example "rgb", "rgba", "cmyk","gray", or "ycbcr". Default is either "gray", "rgb" or "rgba" depending on the image

frame

integer setting which frame to extract from the image

tidy

converts raster data to long form for use with geom_raster. If FALSE output is the same as as.raster().

animate

support animations in the X11 display

browser

argument passed to browseURL

color

a valid color string such as "navyblue" or "#000080"

pseudo_image

string with pseudo image specification for example "radial-gradient:purple-yellow"

...

several images or lists of images to be combined

Details

All standard base vector methods such as [, [[, c(), as.list(), as.raster(), rev(), length(), and print() can be used to work with magick image objects. Use the standard img[i] syntax to extract a subset of the frames from an image. The img[[i]] method is an alias for image_data() which extracts a single frame as a raw bitmap matrix with pixel values.

For reading svg or pdf it is recommended to use image_read_svg() and image_read_pdf() if the rsvg and pdftools R packages are available. These functions provide more rendering options and better quality than built-in svg/pdf rendering delegates from imagemagick itself.

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: _index_, analysis, animation, attributes, color, composite, device, edges, effects, fx, geometry, morphology, ocr, options, painting, segmentation, transform, video

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Download image from the web
frink <- image_read("https://jeroen.github.io/images/frink.png")
worldcup_frink <- image_fill(frink, "orange", "+100+200", 20)
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 from from other image packages
curl::curl_download("https://jeroen.github.io/images/example.webp", "example.webp")
if(require(webp)) image_read(webp::read_webp("example.webp"))
if(require(rsvg))
tiger <- image_read_svg("http://jeroen.github.io/images/tiger.svg")
if(require(pdftools))
image_read_pdf(file.path(R.home('doc'), 'NEWS.pdf'), pages = 1, density = 100)
# create a solid canvas
image_blank(600, 400, "green")
image_blank(600, 400, pseudo_image = "radial-gradient:purple-yellow")

ropensci/magick documentation built on Jan. 15, 2019, 3:27 a.m.