Learn R Programming

magick (version 1.1)

transformations: Image Transformations

Description

Vectorized functions for transforming images. These functions apply the same transformation to each frame in the image. The Magick++ documentation explains meaning of each function and parameter. See editing for functions to read or combine image sequences.

Usage

image_trim(image)

image_background(image, color, flatten = TRUE)

image_crop(image, geometry = "")

image_scale(image, geometry = "")

image_sample(image, geometry = "")

image_border(image, color = "", geometry = "")

image_despeckle(image, times = 1L)

image_median(image, radius = 1)

image_reducenoise(image, radius = 1L)

image_noise(image, noisetype = "gaussian")

image_blur(image, radius = 1, sigma = 0.5)

image_charcoal(image, radius = 1, sigma = 0.5)

image_edge(image, radius = 1)

image_oilpaint(image, radius = 1)

image_emboss(image, radius = 1, sigma = 0.5)

image_enhance(image)

image_equalize(image)

image_flip(image)

image_flop(image)

image_frame(image, geometry = "25x25+6+6")

image_implode(image, factor = 0.5)

image_negate(image)

image_normalize(image)

image_rotate(image, degrees)

image_fill(image, color, point = "1x1", fuzz = 0)

image_transparent(image, color, fuzz = 0)

image_chop(image, geometry)

image_colorize(image, opacity, color)

image_page(image, pagesize = NULL, density = NULL)

image_contrast(image, sharpen = 1)

image_annotate(image, text, gravity = "northwest", location = "+0+0", degrees = 0, size = 10, font = NULL, color = NULL, strokecolor = NULL, boxcolor = NULL)

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

image_compare(image, reference_image, metric = "")

image_deskew(image, treshold = 40)

Arguments

image

object returned by image_read

color

a valid color string

flatten

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

geometry

a string with geometry syntax for example "10x10+5-5".

times

number of times to repeat the despeckle operation

radius

radius, in pixels, for various transformations

noisetype

integer betwee 0 and 5 with noisetype.

sigma

the standard deviation of the Laplacian, in pixels.

factor

image implode factor (special effect)

degrees

how many degrees

point

string indicating the flood-fill starting point

fuzz

Colors within this distance are considered equal. Use this option to match colors that are close to the target color in RGB space. I think max distance (from #000000 to #FFFFFF) is 256^3.

opacity

percentage of transparency

pagesize

geometry string with preferred size and location of an image canvas.

density

geometry string with vertical and horizontal resolution in pixels of the image. Specifies an image density when decoding a Postscript or PDF.

sharpen

enhance intensity differences in image

text

annotation text

gravity

string with gravity type

location

geometry string with location relative to gravity

size

font-size in pixels

font

rendering font. To use a TrueType font, precede the TrueType filename with an @.

strokecolor

adds a stroke (border around the text)

boxcolor

background color that annotation text is rendered on.

format

output format such as png, jpeg, gif or pdf. Can also be a bitmap type such as rgba or rgb.

depth

color depth, must be 8 or 16

antialias

(TRUE/FALSE) enable anti-aliasing for text and strokes

reference_image

another image to compare to

metric

string with a metric type

treshold

straightens an image. A threshold of 40 works for most images.

Details

Each function returns a copy of the manipulated image; the input image will be unaffected. Therefore operations can be piped with magrittr if you're into that kind of stuff.

See Also

Other image: editing

Examples

Run this code
# NOT RUN {
logo <- image_read("https://www.r-project.org/logo/Rlogo.png")
logo <- image_scale(logo, "400")
image_trim(logo)
image_background(logo, "pink", flatten = TRUE)
image_crop(logo, "400x400+200+200")
image_scale(logo, "200x200")
image_sample(logo, "200x200")
image_border(logo, "red", "10x10")
image_despeckle(logo)
image_median(logo)
image_reducenoise(logo)
image_noise(logo)
image_blur(logo, 10, 10)
image_charcoal(logo)
image_edge(logo)
image_oilpaint(logo)
image_emboss(logo)
image_enhance(logo)
image_equalize(logo)
image_flip(logo)
image_flop(logo)
image_frame(logo)
image_implode(logo)
image_negate(logo)
image_normalize(logo)
image_rotate(logo, 45)
image_fill(image_flatten(logo), "red")
image_fill(image_flatten(logo), "red", fuzz = 25600)
image_chop(logo, "100x20")
image_colorize(logo, 50, "red")
# Lights up the R logo
frames <- image_scale(logo, "400x400")
for(i in 1:7) frames <- c(frames, image_contrast(frames[i]))
(blink <- image_animate(c(frames, rev(frames)), fps = 20, loop = 1))

# Add some text to an image
image_annotate(logo, "This is a test")
image_annotate(logo, "CONFIDENTIAL", size = 50, color = "red", boxcolor = "pink",
 degrees = 30, location = "+100+100")

# Setting fonts requires fontconfig support (and that you have the font)
myfont <- ifelse(identical("windows", .Platform$OS.type), "courier-new", "courier")
try(image_annotate(logo, "The quick brown fox", font = myfont, size = 50))
logo2 <- image_blur(logo)
if(magick_config()$version >= "6.8.7")
 image_compare(logo, logo2, metric = "phash")
# }

Run the code above in your browser using DataLab