简体   繁体   English

从R中的多个JPEG生成PDF

[英]Generating PDF from multiple JPEGs in R

I have a folder with multiple JPEG files. 我有一个包含多个JPEG文件的文件夹。 How do I generate a PDF file from these JPEGs in R? 如何从R中的这些JPEG生成PDF文件?

One JPEG = 1 PDF page. 1个JPEG = 1个PDF页面。 Images are of the same size. 图像大小相同。

Thanks a lot in advance. 非常感谢。

You can do this easily using Latex. 您可以使用Latex轻松完成此操作。 Which is nice, because then you can just use Sweave to do the whole thing. 很好,因为这样您就可以使用Sweave来完成整个操作。

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. 好的,您必须设置Sweave管道,但是通过一些调整,您可以轻松地自动化整个过程。

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). 如果您坚持使用R来执行此操作,则可以打开pdf绘图设备,使用par设置边距(默认值可能是大的而不是居中),然后在循环中使用plot.new来开始新的页面并进行plot.window以设置坐标等,而无需绘制轴等,请使用read.jpeg包中的read.jpeg函数(或其他要读取的工具/包,也可以使用EBImage),然后使用rasterImage将jpeg绘制到pdf设备(或将这些步骤中的某些步骤替换为其他图像绘制功能,例如ReadImages中的plot方法)。

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. 我想到了ImageMagick程序套件,也提到过LaTeX,可能还有其他工具。

if you insist on using R (other tools are more suitable), try something like this (slow, untested): 如果您坚持使用R(其他工具更合适),请尝试以下操作(缓慢,未经测试):

   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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM