2015-02-23 71 views
2

我有大約100張圖像(png)。我不想手動完成這些工作,而是要按照定義的順序(基於文件名)將它們放在一起(每行12個圖像)放在一個pdf中。如何將圖像按照定義的順序合併到一個文件中

有沒有人有任何建議?

我按照托馬斯告訴我的下面試了一下,把它們貼在黑邊邊上,我怎麼去掉那個?

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") 
+0

說實話不知道該怎麼做。我認爲使用「par」定義了一行中有多少圖像... – EpiMan 2015-02-23 20:27:05

回答

5

可以使用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

+0

Greaaaatttttttt !!!感謝十億! – EpiMan 2015-02-23 20:42:15

+0

我試着你告訴我,但我得到一個錯誤。我更新了我的帖子。你能看看嗎? – EpiMan 2015-02-23 22:31:03

+0

@EpiMan當調用'plot'時,指定'bty ​​=「n」'(表示boxtype = none)。 – Thomas 2015-02-24 07:13:46

2

注意,由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