如何以定义的顺序将图像合并到一个文件中

时间:2015-02-23 20:22:07

标签: r

我有大约100张图片(png)。我不想手动执行此操作,而是希望按照定义的顺序(基于文件名)将它们放在一个单独的pdf中(每行12个图像)。

有人有任何建议吗?

我根据托马斯在下面告诉我的内容,尝试将它们粘贴在黑色边缘旁边,我该如何删除它?

setwd(workingDir);
files <- list.files(path=".", pattern="*.png", all.files=T, full.names=T)
filelist <- lapply(files, readPNG)
names(filelist) <- paste0(basename((files)))
list2env(filelist, envir=.GlobalEnv)


par(mar=rep(0,4))
layout(matrix(1:length(names(filelist)), ncol=15, byrow=TRUE))

for(i in 1:length(names(filelist))) {
  img <- readPNG(names(filelist[i]))
  plot(NA,xlim=0:1,ylim=0:1,xaxt="n",yaxt="n")
  rasterImage(img,0,0,1,1)
}


dev.print(pdf, "output.pdf") 

3 个答案:

答案 0 :(得分:9)

请注意,Thomas概述的解决方案会在源图像中不存在的多窗格图像中引入一些空白。将参数xaxs = 'i'yaxs='i'添加到plot()将删除它。

library("png") # for reading in PNGs

# example image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

# setup plot
dev.off()
par(mai=rep(0,4)) # no margins

# layout the plots into a matrix w/ 12 columns, by row
layout(matrix(1:120, ncol=12, byrow=TRUE))

# do the plotting
for(i in 1:120) {
  plot(NA,xlim=0:1,ylim=0:1,bty="n",axes=0,xaxs = 'i',yaxs='i')
  rasterImage(img,0,0,1,1)
}

# write to PDF
dev.print(pdf, "output.pdf")

result image

答案 1 :(得分:7)

您可以使用rasterImage功能和 png 包将它们全部绘制在一起。这里简单介绍了如何读取PNG然后绘制它(很多次)。

library("png") # for reading in PNGs

# example image
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

# setup plot
par(mar=rep(0,4)) # no margins

# layout the plots into a matrix w/ 12 columns, by row
layout(matrix(1:120, ncol=12, byrow=TRUE))

# do the plotting
for(i in 1:120) {
    plot(NA,xlim=0:1,ylim=0:1,xaxt="n",yaxt="n",bty="n")
    rasterImage(img,0,0,1,1)
}

# write to PDF
dev.print(pdf, "output.pdf")

您需要稍微修改它以便调用每个图像对象,而不是一遍又一遍地绘制img

结果:

enter image description here

答案 2 :(得分:0)

有时,您只想将一堆PNG图像放入PDF文件中,每页一张图像,以便使用PDF阅读器快速轻松地查看它们。这是我的操作方式,基于上述解决方案,并在必要时缩小图像以适合输出PDF(但保留宽高比)。这使您可以轻松捆绑各种尺寸的PNG图像。

library(png)
# Define a function to get the xright and ytop arguments for rasterImage()
get.raster.image.area.scaling <- function(pngfile) {
  imga <- attr(readPNG(pngfile, info=TRUE), "info")
  img.width <- imga$dim[1] / imga$dpi[1]  # width of PNG image in inches
  img.height <- imga$dim[2] / imga$dpi[2]  # height of PNG image in inches
  img.aspect <- img.height/img.width  # aspect ratio of PNG image
  ri <- list()
  if(img.width > my.width || img.height > my.height) {
    # shrink to fit output device page
    diff.width <- img.width - my.width
    diff.height <- img.height - my.height
    if(diff.width >= diff.height) {
      # shrink to fit width
      ri$width <- 1.0  # xright for rasterImage (fraction of device area)
      new.height <- img.aspect * my.width # (in)
      ri$height <- new.height/my.height  # ytop for rasterImage (fraction of device area)
    } else if(diff.height > diff.width) {
      # shrink to fit height
      ri$height <- 1.0  # ytop for rasterImage (fraction of device area)
      new.width <- my.height / img.aspect # (in)
      ri$width <- new.width/my.width  # xright for rasterImage (fraction of device area)
    } else stop("need to debug unexpected situation\n")
  } else {
    # no shrinking of PNG image needed
    ri$width <- img.width / my.width
    ri$height <- img.height / my.height
  }
  return(ri)
}  # end of get.raster.image.area.scaling()

pngfiles <- c("yourfile1.png", "yourfile2.png", "yourfile3.png")
num.png <- length(pngfiles)
my.width <- 8  # dimensions of desired PDF device output (in)
my.height <- 10
pdf(file = "testplot.pdf", width=my.width, height=my.height, pointsize=12)
par(mai=rep(0,4)) # no margins
for(i in 1:num.png) {
  img <- readPNG(pngfiles[i], native=TRUE)
  ri <- get.raster.image.area.scaling(pngfiles[i])
  plot(NA, xlim=0:1, ylim=0:1, bty="n", axes=0, xaxs = 'i', yaxs='i')
  rasterImage(img, 0, 0, ri$width, ri$height)
}
dev.off()