U12: The magick package: Advanced Image-Processing in R

https://cran.r-project.org/web/packages/magick/vignettes/intro.html

# install.packages("magick")
# install.packages("rsvg")
library(magick)
## Warning: package 'magick' was built under R version 4.0.3
## Linking to ImageMagick 6.9.11.34
## Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
## Disabled features: fontconfig, x11
library(rsvg)
## Warning: package 'rsvg' was built under R version 4.0.3

windows()

tiger <- image_read_svg('http://jeroen.github.io/images/tiger.svg', width = 350)

print(tiger)
##   format width height colorspace matte filesize density
## 1    PNG   350    350       sRGB  TRUE        0   72x72

# Render svg to png bitmap
image_write(tiger, path = "tiger.png", format = "png")
# Converting formats

tiger_png <- image_convert(tiger, "png")

image_info(tiger_png)
##   format width height colorspace matte filesize density
## 1    PNG   350    350       sRGB  TRUE        0   72x72
tiger

#########################################################

# Example image

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

# Add 20px left/right and 10px top/bottom

image_border(image_background(frink, "hotpink"), "#000080", "20x10")

# Trim margins

image_trim(frink)

# Passport pica

image_crop(frink, "100x150+50")

# Resize

image_scale(frink, "300") # width: 300px

image_scale(frink, "x300") # height: 300px

# Rotate or mirror

image_rotate(frink, 45)

image_flip(frink)

image_flop(frink)

# Brightness, Saturation, Hue

image_modulate(frink, brightness = 80, saturation = 120, hue = 90)

# Paint the shirt orange

image_fill(frink, "orange", point = "+100+200", fuzz = 20)

# Add randomness

image_blur(frink, 10, 5)

image_noise(frink)

# Silly filters

image_charcoal(frink)

image_oilpaint(frink)

image_negate(frink)

# Kernel convolution

kern <- matrix(0, ncol = 3, nrow = 3)
kern[1, 2] <- 0.25
kern[2, c(1, 3)] <- 0.25
kern[3, 2] <- 0.25
kern
##      [,1] [,2] [,3]
## [1,] 0.00 0.25 0.00
## [2,] 0.25 0.00 0.25
## [3,] 0.00 0.25 0.00
img <- image_resize(logo, "300x300")
img_blurred <- image_convolve(img, kern)
image_append(c(img, img_blurred))

img %>% image_convolve('Sobel') %>% image_negate()

img %>% image_convolve('DoG:0,0,2') %>% image_negate()

#########################################################
 
# Add some text

image_annotate(frink, "I like R!", size = 70, gravity = "southwest", color = "green")

# Customize text

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

# Fonts may require ImageMagick has fontconfig

image_annotate(frink, "The quick brown fox", font = 'Times', size = 30)

# 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)

# 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
earth

head(image_info(earth))
##   format width height colorspace matte filesize density
## 1    GIF   200    200        RGB FALSE        0   72x72
## 2    GIF   200    200        RGB FALSE        0   72x72
## 3    GIF   200    200        RGB FALSE        0   72x72
## 4    GIF   200    200        RGB FALSE        0   72x72
## 5    GIF   200    200        RGB FALSE        0   72x72
## 6    GIF   200    200        RGB FALSE        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")

# 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

# Animations can be saved as GIF of MPEG files:
    
image_write(animation, "Rlogo-banana.gif")
# Graphics device
    
# install.packages("ggplot2")
    
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.0.3
# Drawing and Graphics
    
# Produce image using graphics device

fig <- image_graph(width = 400, height = 400, res = 96)
    
ggplot2::qplot(mpg, wt, data = mtcars, colour = cyl)
    
dev.off()
## png 
##   2
# Combine
    
out <- image_composite(fig, frink, offset = "+70+30")
    
print(out)
## # A tibble: 1 x 7
##   format width height colorspace matte filesize density
##   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
## 1 PNG      400    400 sRGB       TRUE         0 96x96

# 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
# Animated Graphics
    
# install.packages("gapminder")
# install.packages("ggplot2")
library(gapminder)
## Warning: package 'gapminder' was built under R version 4.0.3
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 x 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")
# Raster Images

plot(as.raster(frink))

# Print over another graphic

plot(cars)

rasterImage(frink, 21, 0, 25, 80)

# The grid package

library(grid)
    
qplot(speed, dist, data = cars, geom = c("point", "smooth"))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
grid.raster(frink)

# OCR text extraction

# install.packages("tesseract")
    
img <- image_read("http://jeroen.github.io/images/testocr.png")

print(img)
## # A tibble: 1 x 7
##   format width height colorspace matte filesize density
##   <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
## 1 PNG      640    480 sRGB       TRUE     23359 72x72

# Extract text

cat(image_ocr(img))
## This is a lot of 12 point text to test the
## ocr code and see if it works on all types
## of file format.
## 
## The quick brown dog jumped over the
## lazy fox. The quick brown dog jumped
## over the lazy fox. The quick brown dog
## jumped over the lazy fox. The quick
## brown dog jumped over the lazy fox.