r - 使用 R 动画包在磁盘上使用图像文件

标签 r animation

我使用 R 创建了一系列需要很长时间才能运行的图像。我想使用动画包从中制作视频而无需重新运行分析。

我找不到使用文件中现有图像的示例。最接近的是 Yihui Xie 的 demo('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 或其他图像类型做这个吗?

最佳答案

我发现这有几个问题。 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 - 使用 R 动画包在磁盘上使用图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33182020/

相关文章:

ios - 什么时候是在 UIView 子类中设置动画和添加子层的合适时间

css - 使用 css3 动画旋转 Div

javascript - 如何为这个峰值图表制作动画?

r - survfit.coxph ;使用新数据和 ID 选项预测生存率

r - Shiny 应用程序中titlePanel中的斜体字体

r - rgl 包 plot3d 上的图例

html - svg 旋转动画(变换原点)

Java如何使图形对象朝其面向的方向移动

r - 数列识别

r - 如何在ggplot2中使用纬度和箭头绘制风向