Reputation: 118
I have a folder with multiple JPEG files. How do I generate a PDF file from these JPEGs in R?
One JPEG = 1 PDF page. Images are of the same size.
Thanks a lot in advance.
Upvotes: 3
Views: 1897
Reputation: 77116
if you insist on using R (other tools are more suitable), try something like this (slow, untested):
lf = list.files(pattern = "jpeg") # image filenames
library(jpeg)
jpegs = lapply(lf, readJPG)
library(grid)
pdf("output.pdf", width=8, height=4)
grid.raster(jpegs[[1]])
lapply(jpegs[-1], function(x) {grid.newpage() ; grid.raster(x)} ) -> bquiet
dev.off()
Upvotes: 1
Reputation: 49660
If you insist on using R to do this then you can open a pdf
plotting device, par
to set the margins (default will probably be to big and not centering), then in a loop use plot.new
to start a new page and plot.window
to set up the coordinates, etc. without plotting axes etc., use the read.jpeg
function from the ReadImages package (or other tool/package to read, EBImage is another possibility) then rasterImage
to plot the jpeg to the pdf device (or replace some of those steps with other image plotting functions, such as the plot method in ReadImages).
But overall it is probably easier/quicker/better/... to use a tool better designed for this type of thing. The ImageMagick suite of programs comes to mind, LaTeX has also been mentioned, and there are probably other tools as well.
Upvotes: 0
Reputation: 108593
You can do this easily using Latex. Which is nice, because then you can just use Sweave to do the whole thing.
You can do something along the lines of :
% This is some Sweave file
\documentclass{article}
\usepackage{graphicx}
\begin{document}
<<results=tex,echo=FALSE>>=
mypics <- dir('mypics')
for(i in mypics){
cat("\\includegraphics{", i, "}\n\n", sep = "")
}
@
\end{document}
OK, you'll have to set up your Sweave pipeline, but with a bit of tweaking you can automate the whole process easily.
Upvotes: 1