r - 如何在 R 中创建缩略图?

标签 r thumbnails plot

我正在尝试生成带有 R 图的 .png 文件。这很好用:

png(file="hello.png", width=1000, height=800)

# Lots and lots of plotting. Long code. Very slow.
plot(x=c(1, 2, 3), y=c(4, 6, 5))

dev.off()

现在如何直接从 R 获取 hello.png 的缩略图?

最佳答案

我不确定这将如何扩展到您的实际应用程序,并且对 png 库没有太多经验,但这在我的测试用例中运行得相当快。您需要指定要制作缩略图的 PNG 文件的名称以及该缩略图的高度/宽度。默认情况下,它将在同一目录中保存缩略图,并在文件名前添加 thumb_

makeThumb <- function(file, height, width) {
  require(png)
  img <- readPNG(file)

  png(file = paste("thumb", file, sep = "_"), height = height, width = width)
    par(mar=c(0,0,0,0), xaxs="i", yaxs="i", ann=FALSE)
    plot(1:2, type='n', xaxt = "n", yaxt = "n", xlab = "", ylab = "")
    lim <- par()
    rasterImage(img, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
  dev.off()
}

请告诉我这是否适合您。该函数与lapply配合良好,也可以同时操作目录中的大量文件:lapply(listOfFiles, makeThumb, height = 200, width = 200) .

关于r - 如何在 R 中创建缩略图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5504057/

相关文章:

r - 创建连续整数,然后使用 dplyr 创建存储在 sqlserver 中的表的索引

php - 从 quicktime(电影)文件中获取缩略图

php - 如何在Laravel5.2中显示图像时生成缩略图

r - 在R中如何绘制时间与日历日期的关系

python - matplotlib - pandas - 子图中的 twinx 轴没有 xlabel 和 xticks

Java内存不足: increase heap space?

r - 索引向量中的连续重复项

python - 使用 R 或 python 在坐标系中绘制向量

r - 将数据帧列拆分为 R 中的向量

ruby-on-rails - 在 linux 环境中创建一个 dwg 的缩略图