You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

rgl无法保存快照与GIF文件的技术问题求助

解决rgl在macOS ARM架构下无法保存快照/GIF的问题

问题重现

运行以下代码时无法正常保存PNG或GIF,报错Chromote超时,且提示当前rgl构建不支持快照,生成的文件为纯黑/空白:

pacman::p_load("rgl", "scales", "magick","webshot2")
rgl::plot3d(1:20, 1:20, 1:20)
snapshot3d("~/snap.png", fmt = "png")
movie3d(spin3d(axis=c(0,0,1), rpm=), duration=2, movie="3D-SPIN", verbose=TRUE, type="gif",fps = 2)

运行输出:

> pacman::p_load("rgl", "scales", "magick","webshot2")
> rgl::plot3d(1:20, 1:20, 1:20)
> snapshot3d("~/snap.png", fmt = "png")
Error : Chromote: timed out waiting for event Page.loadEventFired
Warning: webshot2::webshot() failed; trying rgl.snapshot()Warning: this build of rgl does not support snapshots
> movie3d(spin3d(axis=c(0,0,1), rpm=), duration=2, movie="3D-SPIN", verbose=TRUE, type="gif",fps = 2)
Error : Chromote: timed out waiting for event Page.loadEventFired
Error : Chromote: timed out waiting for event Page.loadEventFired: this build of rgl does not support snapshotsWriting '3D-SPIN001.png'
Error : Chromote: timed out waiting for event Page.loadEventFired: this build of rgl does not support snapshotsWriting '3D-SPIN002.png'
Error : Chromote: timed out waiting for event Page.loadEventFired: this build of rgl does not support snapshotsWriting '3D-SPIN003.png'
Error : Chromote: timed out waiting for event Page.loadEventFired: this build of rgl does not support snapshotsWriting '3D-SPIN004.png'
Warning: webshot2::webshot() failed; trying rgl.snapshot()Warning: this build of rgl does not support snapshots

会话信息:

R version 4.3.2 (2023-10-31)
Platform: aarch64-apple-darwin20 (64-bit)
Running under: macOS Sonoma 14.2.1

Matrix products: default
BLAS:   /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib 
LAPACK: /Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/lib/libRlapack.dylib;  LAPACK version 3.11.0

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

time zone: Asia/Tokyo
tzcode source: internal

attached base packages:
[1] stats4    stats     graphics  grDevices datasets  utils     methods   base     

other attached packages:
 [1] plot3Drgl_1.0.4       plot3D_1.4            webshot2_0.1.1        magick_2.8.2          installr_0.23.4       scales_1.2.1         
 [7] rgl_1.2.8             plotly_4.10.3         SeuratDisk_0.0.0.9020 remotes_2.4.2         GenomicRanges_1.52.0  GenomeInfoDb_1.36.1  
[13] IRanges_2.34.1        S4Vectors_0.38.1      BiocGenerics_0.46.0   Signac_1.12.0         Seurat_5.0.1          SeuratObject_5.0.1   
[19] sp_2.0-0              lubridate_1.9.3       forcats_1.0.0         stringr_1.5.1         dplyr_1.1.2           purrr_1.0.2          
[25] readr_2.1.4           tidyr_1.3.0           tibble_3.2.1          ggplot2_3.4.2         tidyverse_2.0.0       devtools_2.4.5       
[31] usethis_2.2.2   

已知:已安装Google Chrome,rglWebGL可在HTML部件正常显示图表,但保存的PNG/GIF为纯黑/空白。


解决方案

1. 重新编译带快照支持的rgl

当前预编译的rgl 1.2.8在ARM架构macOS上未启用快照功能,需从源码编译:

  • 终端安装依赖:
    brew install xquartz mesa
    
  • R中卸载现有rgl:
    remove.packages("rgl")
    
  • 源码安装rgl:
    install.packages("rgl", type = "source")
    
    安装完成后运行rgl::capabilities(),检查snapshot项是否为TRUE。

2. 调整webshot2超时时间

若坚持使用webshot2,可延长超时时间尝试:

options(rgl.useNULL = TRUE) # 强制使用WebGL后端
library(rgl)
plot3d(1:20, 1:20, 1:20)
# 设置10秒超时(10000毫秒)
snapshot3d("~/snap.png", fmt = "png", webshot = list(timeout = 10000))

3. 直接捕获rgl窗口(替代方案)

上述方法无效时,用magick直接捕获窗口内容:

library(magick)
# 确保rgl窗口处于前台
rgl::plot3d(1:20, 1:20, 1:20)
# 捕获rgl窗口截图
img <- image_read("window:rgl")
image_write(img, "~/snap.png")

4. 生成GIF的替代方法

使用WebGL后端生成单帧截图,再用magick合并为GIF:

options(rgl.useNULL = TRUE)
library(rgl)
library(magick)

frames <- list()
# 生成10帧旋转画面
for (i in 1:10) {
  plot3d(1:20, 1:20, 1:20, aspect = c(1,1,1))
  rotate3d(matrix(1,4,4), angle = 2*pi*i/10, x=0, y=0, z=1)
  # 保存单帧截图
  frames[[i]] <- image_read(snapshot3d(NULL, fmt = "png", webshot = list(timeout = 10000)))
}

# 合并为GIF,帧率设为5
image_join(frames) %>% image_animate(fps = 5) %>% image_write("3D-SPIN.gif")

内容的提问来源于stack exchange,提问作者rdv

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 18:45:55