参见:https://docs.ropensci.org/magick/articles/intro.html

关于颜色获取部分,可以参见:

12. 从图中提取颜色

1. 写入, 写出与转换数据

该函数支持导入多种类型图片,也可以不选择图片类型,其可以自动进行识别:

11. 用magick 成为你的R ps - 图1

我们可以直接输出该图片:

  1. > im3 <- image_read("https://cdn.jsdelivr.net/gh/mugpeng/3-source/2.img/alchemist.png")
  2. > im3
  3. # A tibble: 1 x 7
  4. format width height colorspace matte filesize density
  5. <chr> <int> <int> <chr> <lgl> <int> <chr>
  6. 1 PNG 1920 1200 sRGB FALSE 2544938 72x72

注意,这里的显示并不依赖plot 窗口:

11. 用magick 成为你的R ps - 图2

其实我们可以直接输出为其他类型的图片:

  1. if( !dir.exists("./img") ) dir.create("./img")
  2. image_write(im3, path = "./img/alchemist.pdf", format = "pdf")

11. 用magick 成为你的R ps - 图3

比如输出为pdf:

11. 用magick 成为你的R ps - 图4

我们也可以直接转换为pdf 类型元素:

  1. > # convert2pdf obj
  2. > im3_pdf <- image_convert(im3, "pdf")
  3. > image_info(im3_pdf)
  4. # A tibble: 1 x 7
  5. format width height colorspace matte filesize density
  6. <chr> <int> <int> <chr> <lgl> <int> <chr>
  7. 1 PDF 1920 1200 sRGB FALSE 0 72x72

2. 对图片进行转换处理

  • image_scale
  1. # resize img
  2. image_scale(im3, "200") # proportionally to width: 200px
  3. image_scale(im3, "x200") # proportionally to height: 200px

11. 用magick 成为你的R ps - 图5

  1. > image_scale(im3, "x200") # proportionally to height: 200px
  2. # A tibble: 1 x 7
  3. format width height colorspace matte filesize density
  4. <chr> <int> <int> <chr> <lgl> <int> <chr>
  5. 1 PNG 320 200 sRGB FALSE 0 72x72
  • img_crop
  1. # cut img
  2. image_crop(im3, "100x150+500+100") # cut img from left 500px and top 100px size is 100x150px

11. 用magick 成为你的R ps - 图6

其他功能包括:

11. 用magick 成为你的R ps - 图7

可以慢慢探究。

3. 用magick 做滤镜

  • image_blur

模糊处理,sigma 和radius 控制模糊等级与半径:

  1. image_blur(im3, sigma = 5, radius = 5)

11. 用magick 成为你的R ps - 图8

  • noise

加上噪点

11. 用magick 成为你的R ps - 图9

  • charcoal

阴间模式

11. 用magick 成为你的R ps - 图10

  • oilpaint

油画模式

11. 用magick 成为你的R ps - 图11