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

Epi*_*Man 5 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") 
Run Code Online (Sandbox Code Playgroud)

Dan*_*ich 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")
Run Code Online (Sandbox Code Playgroud)

结果图像


Tho*_*mas 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")
Run Code Online (Sandbox Code Playgroud)

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

结果:

在此输入图像描述