I have around 100 images(png). Instead of doing that manually, I want to put them beside each other (12 images per line) in one single pdf in a defined order (based on file names).
Does anybody have any suggestion?
I tried according to what Thomas told me below, it paste them beside with a black margin, how can I remove that?
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")
Note that the solution outlined by Thomas introduces some whitespace into the multipane image not present in the source image. Adding the arguments xaxs = 'i'
and yaxs='i'
into plot()
will remove it.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With