使用磁盘上的图像文件与R动画包

3
我使用R语言创建了一系列图像,运行时间很长。我想使用animation包来制作视频,而无需重新运行分析过程。
我找不到使用现有文件中的图像的示例。最接近的是Yihui Xie的演示('flowers'),用于创建HTML动画。我更改了他的代码,成功地制作了花朵图像的mp4,但我不确定如何访问已经存在于文件中的图像。
根据他的代码,应该是这样的:
library(animation)
oopts = if (.Platform$OS.type == "windows") {
ani.options(ffmpeg = "C:/Software/ffmpeg/ffmpeg-20151015-git-0418541-win64-static/bin/ffmpeg.exe")}

#My list of images from disk
extList = list.files(myDir, pattern='.jpg', full.names=T)
saveVideo({
for (i in 1:length(extList)) {

#Yihui Xie's example downloads jpegs from web
#This code works to make an mp4 but I want to use images from disk
#extList = c('http://i.imgur.com/rJ7xF.jpg',
#         'http://i.imgur.com/Lyr9o.jpg',
#          'http://i.imgur.com/18Qrb.jpg')
#download.file(url = extList[i], destfile = sprintf(ani.options('img.fmt'), i), mode = 'wb')

someFunctionToAccessImage(extList[i])

  }
}, video.name='notFlowers.mp4',
use.dev = FALSE, 
ani.type = 'jpg',
interval = 2, 
single.opts = "'dwellMultiplier': 1")

额外问题 - 我能使用PNG或其他图像类型实现这一点吗?


您发布的删除答案不是解决方案吗? - Roman Luštrik
不是,Roman,它不是。我以为是,但当我注意到代码运行两次时出现非常奇怪的行为,所以我删除了那个解决方案。 - Ryan
1个回答

4
我发现这种方法存在几个问题。`saveVideo`使用临时目录来处理文件并制作视频。此外,它添加到图像名称的后缀没有正确工作。因此,这里有一种方法可以将您存储的文件夹中的图像复制到`saveVideo`使用的临时目录中。棘手的部分是找到该目录的路径,这是通过从表达式中定义的函数使用`sys.frame`完成的。
注意:另一个可能的选项是手动将图像复制到临时文件夹中,您知道`saveVideo`将使用它(它将调用`tempdir()`),或重新定义`tempdir()`返回当前图像的路径,但我没有测试过。
library(animation)
oopts = if (.Platform$OS.type == "windows")
    ani.options(ffmpeg = "C:\\home\\src\\ffmpeg-20151017-git-e9299df-win64-static\\bin\\ffmpeg.exe")

## Some variables
dirPath <- normalizePath("images/")  # path to folder containing images
postfix <- "%03d"                    # I created my files with "Rplot%03d"

## Make the animation
saveVideo({
    ## Need to retrieve some variables from environments up the call stack
    env.info <- (function() { list(wd = sys.frame(-1)$owd, fmt=sys.frame(-2)$img.fmt,
                                   e=sys.frame(-2)) })()
    postfix <- postfix
    img.fmt <- gsub("%d", postfix, env.info$fmt, fixed=TRUE)
    assign('img.fmt', img.fmt, envir=env.info$e)
    file.copy(list.files(dirPath, full.names = TRUE), to=env.info$wd, overwrite = TRUE)
}, video.name='heatBalls.mp4', img.name='Rplot', interval = .05, use.dev=FALSE)

完整示例

## Random function to save some images to disk
circ <- function(x,y,r) { s <- seq(-pi,pi,len=30); data.frame(x=x+r*cos(s), y=y+r*sin(s)) }
imgFun <- function(n, ncircs, dirPath) {
    if (!require(scales)) stop("install scales package")
    rads <- runif(ncircs, 0.5, 3)
    xs <- runif(ncircs, 0.1+rads, 19.9-rads)
    ys <- runif(ncircs, 0.1+rads, 19.9-rads)
    vs <- matrix(runif(ncircs*2), 2)
    cols <- colorRampPalette(c('lightblue','darkblue'), alpha=0.3)(ncircs)
    png(file.path(dirPath, 'Rplot%03d.png'))
    for (i in seq_len(n)) {
        image(x=seq(0, 20, length=20), y=seq(0, 20, length=20),
              z=matrix(rnorm(400),20), col=heat.colors(20, alpha=0.6), xlab='', ylab='')
        for(j in 1:ncircs) polygon(x=circ(xs[j], ys[j], rads[j]), col=alpha(cols[j],0.7))
        condx <- (xs + rads) > 20 | (xs - rads) < 0
        condy <- (ys + rads) > 20 | (ys - rads) < 0
        vs[1,condx] <- -vs[1,condx]
        vs[2,condy] <- -vs[2,condy]
        xs <- xs + vs[1,]
        ys <- ys + vs[2,]
    }
    dev.off()
}

## Create some images on disk in a folder called "images"
dirPath <- normalizePath("images/")
dir.create(dirPath)
imgFun(50, 18, dirPath)

接下来运行以上代码,应该会生成以下类似的视频。

enter image description here


感谢您提供出色的答案。这看起来像是一个非常好的解决方案,适用于需要留在R生态系统中的人。当我等待我的问题得到回答时,我也尝试了Zulko的Python模块MoviePy 链接。总体而言,这是一款非常容易入门的工具,并且提供的帮助和示例非常完整。强烈推荐! - Ryan
我认为你在这件事上不要留在R生态系统里是绝对正确的;p,谢谢你提供的链接。最直接的做法就是直接使用ffmpeg,因为你已经有了图片。但是,这样也很有趣。 - Rorschach
是的,Zulko在MoviePy GitHub页面上也这样说了关于ffmpeg。然而,Zulko添加了一些惊人的控件来自定义电影,这使得它值得一试。关于留在R生态系统中,我花费了数小时在R中尝试解决这个问题,但使用Zulko的模块只需大约15分钟即可解决。 - Ryan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接