1 min read

magick 使用技巧

本文于2020-10-10更新。 如发现问题或者有建议,欢迎提交 Issue

可以对图片进行操作。 (Ooms 2018)

1 基本功能

library(magick)
## Warning: 程辑包'magick'是用R版本3.6.3 来建造的
## Linking to ImageMagick 6.9.9.14
## Enabled features: cairo, freetype, fftw, ghostscript, lcms, pango, rsvg, webp
## Disabled features: fontconfig, x11
frink <- image_read("https://jeroen.github.io/images/frink.png")
frink

image_trim(frink) # 裁剪空白地方

image_scale(frink, "200x200") # resize

image_flip(frink) # 旋转

image_rotate(frink, 45) # 旋转

image_rotate(frink, 180) # 旋转

image_negate(frink)

frink %>% 
    image_background("green") %>% # 修改背景颜色 
    image_flatten() %>%
    image_border("red", "10x10") # 加边框

# image_rotate(frink, 45) %>% 
#     image_write("frink-rotated.png")
# 导出图片
image_oilpaint(frink)

image_implode(frink)

image_charcoal(frink) ## <-- result of this is shown

image_blur(frink)

image_edge(frink)

2 pipeline

library(magick)
file.path('../','picbackup') %>% 
    list.files(full.names = T) %>% 
    str_subset('reprex') %>% 
    data.table(x=.) %>%
    mutate(x = map(x,image_read)) %>% 
    mutate(x = map(x,~image_scale(.,"1000x1000"))) %>% 
    # resize to fit better
    .$x %>% 
    image_join %>% 
    image_animate(fps=1) %>% 
    image_write(
                file.path(
                    '../','picbackup'
                    ,'reprex201811061210.gif'
                )
                )
  1. image_join可以转换listc()格式,让image_animate调用。 github

Ooms, Jeroen. 2018. Magick. https://github.com/ropensci/magick.