R magick:无需矢量化即可合成图像

R magick: compose images without vectorization

magick R 包中的 image_composite 函数说明:

The image_composite function is vectorized over both image arguments: if the first image has n frames and the second m frames, the output image will contain n * m frames.

这是一个丑陋的例子:

banana <- image_read("https://jeroen.github.io/images/banana.gif")
image_composite(banana, banana, offset = "+70+00", operator = "Add")

有没有办法避免矢量化,让香蕉一起跳舞?或者,是否有另一个功能(也来自其他包)允许这样做?

在相同图像的情况下,您可以修改每一帧,将矢量化由内而外,有点。

image_apply(
  banana, FUN = function(img) {
    image_composite(img, img, offset = "+70+00", operator = "Add")
  }
)

如果您有两张帧数相同的图像,仅使用 magick 中公开的函数会更难。一种更简单的方法是将 magick-image 对象分成列表,并使用像 purrr::map2 这样的函数式编程工具,然后再重新加入它们:

purrr::map2(
  as.list(banana), as.list(image_negate(banana)),
  ~image_composite(.x, .y, operator = "Add", offset = "+70+00")
  ) %>% 
  image_join()