Using magick to work with images

magick tidyverse tesseract gifski

Examples of functions in the R magick package

Load packages

pacman::p_load(tidyverse, here, magick, gifski, tesseract)

Download images

Read images

path_in  <- paste0(here(), "/_posts/using-imagick/imgs/", 1:12, ".jpeg")
path_in[1:2]
[1] "/Users/elizabethstanny/Documents/estanny-distill/_posts/using-imagick/imgs/1.jpeg"
[2] "/Users/elizabethstanny/Documents/estanny-distill/_posts/using-imagick/imgs/2.jpeg"

Create the function, read_image_in to read in the images

read_image_in  <- function(path_in){
  image_read(path = path_in, 
             strip = TRUE  # drop image comments and meta data
             )
}

Read the images into imgs_in, a list with 12 elements

imgs_in  <- map(path_in, read_image_in)
imgs_in[1:2]
[[1]]
# A tibble: 1 × 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 JPEG     320    240 sRGB       FALSE    32147 72x72  

[[2]]
# A tibble: 1 × 7
  format width height colorspace matte filesize density
  <chr>  <int>  <int> <chr>      <lgl>    <int> <chr>  
1 JPEG     320    240 sRGB       FALSE    22214 72x72  

Concatenate the images into a tibble

imgs_tog  <- reduce(imgs_in, c)  
imgs_tog

Functions for image frames and animation

imgs_tog[1:2] %>% 
  image_append(stack = FALSE)  

imgs_tog  %>% 
  image_animate(fps = 1,
                loop = 1 # repeat animation 1 time.  0 = infinite
                )

imgs_tog  %>% 
  image_write_gif(path = here("_posts/using-imagick/imgs/example.gif"), delay = 1)
[1] "/Users/elizabethstanny/Documents/estanny-distill/_posts/using-imagick/imgs/example.gif"
![example gif](imgs/example.gif){width="50%"}
example gif
imgs_tog[1:2]  %>% 
  image_average()

imgs_tog  %>% 
  image_montage(tile = "3x4" ,
                bg = "darkred")

Functions for brightness, colors, saturation, hue

imgs_tog[2]
imgs_tog[2]  %>% 
  image_colorize(opacity = 60, color = "purple")
imgs_tog[2]  %>% 
  image_quantize(
  colorspace = "gray"
)
imgs_tog[2]  %>% 
  image_convert(type = "grayscale")
imgs_tog[2]  %>% 
  image_modulate(brightness = 50)
imgs_tog[2]  %>% 
  image_modulate(saturation = 50)
imgs_tog[2]  %>% 
  image_modulate(hue = 50)

Add a border and text

imgs_tog[1]  %>% 
  image_border(color = "darkred", "5x5")  %>% 
  # image_rotate(degrees = 10)  %>% 
  image_annotate(text = "Mt Tam", 
                 size = 30,
                 #find font in `Font Book.app`
                 font = "Bradley Hand", 
                 color = "darkred", 
                 degrees = 0,
                 # start with gravity
                 gravity = "northwest",
                 location = "+30+0" #30 pixels right 0 pixels up/down relative to gravity 
                 )  

Functions for effects

Blur sigma = 0 to 1 - 0 no blur

imgs_tog[1]  %>% 
  image_blur(sigma = 1)
imgs_tog[1]  %>% 
  image_charcoal()
imgs_tog[1]  %>% 
  image_oilpaint()
imgs_tog[1]  %>% 
  image_implode()

imgs_tog[1]  %>% 
  image_negate()

imgs_tog[1]  %>% 
  image_emboss()

imgs_tog[1]  %>% 
  image_reducenoise()

Image Transform

imgs_tog[1]
imgs_tog[1]  %>% 
  image_flip()

imgs_tog[1]  %>% 
  image_flop()

geometry helper functions

imgs_tog[1]  %>% 
  image_crop(geometry = geometry_area(width = 100, height =100, x_off = 120, y_off = 50))
imgs_tog[1]  %>% 
  image_crop(geometry = geometry_point(120, 50))
imgs_tog[1]  %>% 
  image_resize(geometry = geometry_size_pixels(width = 100, height=100, preserve_aspect = FALSE))
imgs_tog[1]  %>% 
  image_resize(geometry = geometry_size_pixels(width = 100, height=100, preserve_aspect = TRUE))
imgs_tog[1]  %>% 
  image_resize(geometry = geometry_size_percent(width = 30, height=10))
imgs_tog[1]  %>% 
  image_chop(geometry = "x150") 

Extract text from an image

img_test  <- image_read(here("_posts/using-imagick/imgs/screen_shot_1.png"))

image_ocr_data(img_test)
# A tibble: 39 × 3
   word     confidence bbox          
   <chr>         <dbl> <chr>         
 1 Photos         96.4 29,24,64,32   
 2 Photo          94.2 81,37,123,66  
 3 Kind:          94.2 124,37,158,66 
 4 JPEG           63.6 176,46,207,56 
 5 oon            35.0 454,47,492,57 
 6 JPEG           80.5 72,74,106,103 
 7 Quality:       88.9 108,74,157,103
 8 _              48.9 163,74,174,103
 9 High           55.0 176,83,202,95 
10 °              27.7 454,84,461,94 
# … with 29 more rows