【问题标题】:saveGIF() returns empty GIFsaveGIF() 返回空的 GIF
【发布时间】:2019-10-12 18:47:22
【问题描述】:

在 R 中,我试图获得基于多张图像的动画 GIF。我编写了一个返回图像的函数,并使用动画包的 saveGIF() 函数来创建 GIF。返回图像的功能有效(我看到图像在查看器中弹出)。使用 saveGIF 创建 GIF 时,没有错误消息但 GIF 为空。

 library(leaflet)
 library(mapview)
 library(animation)

  latitude = c(seq(48.13608, 52.48608, 0.00145))
  longitude = c(seq(11.57278, 13.40278, 0.00061))

  sampledf <- as.data.frame(cbind(longitude, latitude))


  plot.1 <- function(df)
  {
  for (i in seq(1,nrow(df),300)){
  m<- leaflet() %>%
  addTiles() %>%
  setView( lng = 12.48778
           , lat = 50.31108
           , zoom = 4 )    %>%

  addPolylines(data = df[1:i,],
               lng = ~longitude,
               lat = ~latitude,
               color = ~"red")

  print(m)
 }
 }

  saveGIF(plot.1(sampledf),movie.name="test.gif", interval=0.5, ani.width=1980/2, ani.height=1080/2)

【问题讨论】:

  • 我不知道leaflet,但我会尝试m 而不是print(m)
  • @RonakShah,我添加了一个示例 df 和使用的包
  • @StéphaneLaurent 我试过了,没用

标签: r animation imagemagick gif


【解决方案1】:

这可能是一种复杂的方法,但我可以通过首先为sampledf 数据创建png 文件然后使用magick 库来生成gif 来做到这一点。

library(leaflet)
library(mapview)
library(magick)

counter <- 1 
for (i in seq(1,nrow(sampledf),300)){

    m <- leaflet() %>%
          addTiles() %>%
          setView( lng = 12.48778
                 , lat = 50.31108
                 , zoom = 4 )    %>%
           addPolylines(data = sampledf[1:i,],
                        lng = ~longitude,
                        lat = ~latitude,
                        color = ~"red")

     mapshot(m, file = paste0("plot_", counter, ".png"))
     counter = counter + 1
}
file_names <- list.files(pattern = "plot_\\d+.png$", full.names = TRUE)

image_read(file_names) %>%
   image_animate(fps = 1) %>%
   image_write("output.gif")

【讨论】:

  • 尽管它可能是一个复杂的解决方案,但它可以完美运行,也集成到函数中。谢谢!
猜你喜欢
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-08-06
  • 2018-12-23
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
相关资源
最近更新 更多