library(magick)
## Linking to ImageMagick 6.9.12.3
## Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
## Disabled features: fontconfig, x11
frink <- image_read("https://jeroen.github.io/images/frink.png")
print(frink)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72

TEKS ANNOTAION

# Add some text
image_annotate(frink, "FRINK HEHE", size = 70, gravity = "southwest", color = "brown")

# Customize text
image_annotate(frink, "CONFIDENTIAL", size = 30, color = "red", boxcolor = "pink",
  degrees = 60, location = "+50+100")

Combining with pipes

frink <- image_read("https://jeroen.github.io/images/frink.png")
frink2 <- image_scale(frink, "100")
image_info(frink)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE    73494   72x72
image_info(frink2)
##   format width height colorspace matte filesize density
## 1    PNG   100    202       sRGB  TRUE        0   72x72
test <- image_rotate(frink, 90)
test <- image_background(test, "blue", flatten = TRUE)
test <- image_border(test, "red", "10x10")
test <- image_annotate(test, "This is how we combine transformations", color = "white", size = 30)
print(test)
##   format width height colorspace matte filesize density
## 1    PNG   465    240       sRGB  TRUE        0   72x72

image_read("https://jeroen.github.io/images/frink.png") %>%
  image_rotate(270) %>%
  image_background("blue", flatten = TRUE) %>%
  image_border("red", "10x10") %>%
  image_annotate("The same thing with pipes", color = "white", size = 30)

Image Vectors

# Download earth gif and make it a bit smaller for vignette
earth <- image_read("https://jeroen.github.io/images/earth.gif") %>%
  image_scale("200x") %>%
  image_quantize(128)

length(earth)
## [1] 44
head(image_info(earth))
##   format width height colorspace matte filesize density
## 1    GIF   200    200        RGB FALSE        0   72x72
## 2    GIF   200    200        RGB  TRUE        0   72x72
## 3    GIF   200    200        RGB  TRUE        0   72x72
## 4    GIF   200    200        RGB  TRUE        0   72x72
## 5    GIF   200    200        RGB  TRUE        0   72x72
## 6    GIF   200    200        RGB  TRUE        0   72x72
rev(earth) %>% 
  image_flip() %>% 
  image_annotate("meanwhile in Australia", size = 20, color = "white")

LAYERS

bigdata <- image_read('https://jeroen.github.io/images/bigdata.jpg')
frink <- image_read("https://jeroen.github.io/images/frink.png")
logo <- image_read("https://jeroen.github.io/images/Rlogo.png")
img <- c(bigdata, logo, frink)
img <- image_scale(img, "300x300")
image_info(img)
##   format width height colorspace matte filesize density
## 1   JPEG   300    225       sRGB FALSE        0   72x72
## 2    PNG   300    232       sRGB  TRUE        0   72x72
## 3    PNG   148    300       sRGB  TRUE        0   72x72
image_mosaic(img)

image_flatten(img)

image_flatten(img, 'Add')

image_flatten(img, 'Modulate')

image_flatten(img, 'Minus')

COMBINING

image_append(image_scale(img, "x200"))

image_append(image_scale(img, "100"), stack = TRUE)

bigdatafrink <- image_scale(image_rotate(image_background(frink, "none"), 300), "x200")
image_composite(image_scale(bigdata, "x400"), bigdatafrink, offset = "+180+100")

Pages

manual <- image_read_pdf('https://cloud.r-project.org/web/packages/magick/magick.pdf', density = 72)
image_info(manual)
##    format width height colorspace matte filesize density
## 1     PNG   612    792       sRGB  TRUE        0   72x72
## 2     PNG   612    792       sRGB  TRUE        0   72x72
## 3     PNG   612    792       sRGB  TRUE        0   72x72
## 4     PNG   612    792       sRGB  TRUE        0   72x72
## 5     PNG   612    792       sRGB  TRUE        0   72x72
## 6     PNG   612    792       sRGB  TRUE        0   72x72
## 7     PNG   612    792       sRGB  TRUE        0   72x72
## 8     PNG   612    792       sRGB  TRUE        0   72x72
## 9     PNG   612    792       sRGB  TRUE        0   72x72
## 10    PNG   612    792       sRGB  TRUE        0   72x72
## 11    PNG   612    792       sRGB  TRUE        0   72x72
## 12    PNG   612    792       sRGB  TRUE        0   72x72
## 13    PNG   612    792       sRGB  TRUE        0   72x72
## 14    PNG   612    792       sRGB  TRUE        0   72x72
## 15    PNG   612    792       sRGB  TRUE        0   72x72
## 16    PNG   612    792       sRGB  TRUE        0   72x72
## 17    PNG   612    792       sRGB  TRUE        0   72x72
## 18    PNG   612    792       sRGB  TRUE        0   72x72
## 19    PNG   612    792       sRGB  TRUE        0   72x72
## 20    PNG   612    792       sRGB  TRUE        0   72x72
## 21    PNG   612    792       sRGB  TRUE        0   72x72
## 22    PNG   612    792       sRGB  TRUE        0   72x72
## 23    PNG   612    792       sRGB  TRUE        0   72x72
## 24    PNG   612    792       sRGB  TRUE        0   72x72
## 25    PNG   612    792       sRGB  TRUE        0   72x72
## 26    PNG   612    792       sRGB  TRUE        0   72x72
## 27    PNG   612    792       sRGB  TRUE        0   72x72
## 28    PNG   612    792       sRGB  TRUE        0   72x72
## 29    PNG   612    792       sRGB  TRUE        0   72x72
## 30    PNG   612    792       sRGB  TRUE        0   72x72
## 31    PNG   612    792       sRGB  TRUE        0   72x72
## 32    PNG   612    792       sRGB  TRUE        0   72x72
## 33    PNG   612    792       sRGB  TRUE        0   72x72
## 34    PNG   612    792       sRGB  TRUE        0   72x72
## 35    PNG   612    792       sRGB  TRUE        0   72x72
## 36    PNG   612    792       sRGB  TRUE        0   72x72
## 37    PNG   612    792       sRGB  TRUE        0   72x72
## 38    PNG   612    792       sRGB  TRUE        0   72x72
## 39    PNG   612    792       sRGB  TRUE        0   72x72
## 40    PNG   612    792       sRGB  TRUE        0   72x72
## 41    PNG   612    792       sRGB  TRUE        0   72x72
## 42    PNG   612    792       sRGB  TRUE        0   72x72
## 43    PNG   612    792       sRGB  TRUE        0   72x72
## 44    PNG   612    792       sRGB  TRUE        0   72x72

ANIMATION

image_animate(image_scale(img, "200x200"), fps = 1, dispose = "previous")

newlogo <- image_scale(image_read("https://jeroen.github.io/images/Rlogo.png"))
oldlogo <- image_scale(image_read("https://jeroen.github.io/images/Rlogo-old.png"))
image_resize(c(oldlogo, newlogo), '200x150!') %>%
  image_background('white') %>%
  image_morph() %>%
  image_animate(optimize = TRUE)

# Foreground image
banana <- image_read("https://jeroen.github.io/images/banana.gif")
banana <- image_scale(banana, "150")
image_info(banana)
##   format width height colorspace matte filesize density
## 1    GIF   150    148       sRGB  TRUE        0   72x72
## 2    GIF   150    148       sRGB  TRUE        0   72x72
## 3    GIF   150    148       sRGB  TRUE        0   72x72
## 4    GIF   150    148       sRGB  TRUE        0   72x72
## 5    GIF   150    148       sRGB  TRUE        0   72x72
## 6    GIF   150    148       sRGB  TRUE        0   72x72
## 7    GIF   150    148       sRGB  TRUE        0   72x72
## 8    GIF   150    148       sRGB  TRUE        0   72x72
# Background image
background <- image_background(image_scale(logo, "200"), "white", flatten = TRUE)

# Combine and flatten frames
frames <- image_composite(background, banana, offset = "+70+30")

# Turn frames into animation
animation <- image_animate(frames, fps = 10, optimize = TRUE)
print(animation)
##   format width height colorspace matte filesize density
## 1    gif   200    155       sRGB  TRUE        0   72x72
## 2    gif    94    105       sRGB  TRUE        0   72x72
## 3    gif   125    122       sRGB  TRUE        0   72x72
## 4    gif   108    118       sRGB  TRUE        0   72x72
## 5    gif   108    105       sRGB  TRUE        0   72x72
## 6    gif    92    105       sRGB  TRUE        0   72x72
## 7    gif   113    123       sRGB  TRUE        0   72x72
## 8    gif   119    118       sRGB  TRUE        0   72x72

menggambar device

# Or paint over an existing image
img <- image_draw(frink)
rect(20, 20, 200, 100, border = "red", lty = "dashed", lwd = 5)
abline(h = 300, col = 'blue', lwd = '10', lty = "dotted")
text(30, 250, "Hoiven-Glaven", family = "monospace", cex = 4, srt = 90)
palette(rainbow(11, end = 0.9))
symbols(rep(200, 11), seq(0, 400, 40), circles = runif(11, 5, 35),
  bg = 1:11, inches = FALSE, add = TRUE)
dev.off()
## png 
##   2
print(img)
##   format width height colorspace matte filesize density
## 1    PNG   220    445       sRGB  TRUE        0   72x72

Animated Graphics

library(gapminder)
library(ggplot2)
img <- image_graph(600, 340, res = 96)
datalist <- split(gapminder, gapminder$year)
out <- lapply(datalist, function(data){
  p <- ggplot(data, aes(gdpPercap, lifeExp, size = pop, color = continent)) +
    scale_size("population", limits = range(gapminder$pop)) + geom_point() + ylim(20, 90) + 
    scale_x_log10(limits = range(gapminder$gdpPercap)) + ggtitle(data$year) + theme_classic()
  print(p)
})
dev.off()
## png 
##   2
animation <- image_animate(img, fps = 2, optimize = TRUE)
print(animation)
## # A tibble: 12 × 7
##    format width height colorspace matte filesize density
##    <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
##  1 gif      600    340 sRGB       TRUE         0 96x96  
##  2 gif      385    243 sRGB       TRUE         0 96x96  
##  3 gif      395    237 sRGB       TRUE         0 96x96  
##  4 gif      374    232 sRGB       TRUE         0 96x96  
##  5 gif      393    225 sRGB       TRUE         0 96x96  
##  6 gif      373    234 sRGB       TRUE         0 96x96  
##  7 gif      354    234 sRGB       TRUE         0 96x96  
##  8 gif      308    210 sRGB       TRUE         0 96x96  
##  9 gif      320    260 sRGB       TRUE         0 96x96  
## 10 gif      331    218 sRGB       TRUE         0 96x96  
## 11 gif      356    208 sRGB       TRUE         0 96x96  
## 12 gif      347    208 sRGB       TRUE         0 96x96

image_write(animation, "gapminder.gif")
plot(as.raster(frink))

# Print over another graphic
plot(cars)
rasterImage(frink, 21, 0, 25, 80)

library(ggplot2)
library(grid)
qplot(speed, dist, data = cars, geom = c("point", "smooth"))
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

grid.raster(frink)