在R中将pdf转换为png [英] Convert pdf to png in R

查看:908
本文介绍了在R中将pdf转换为png的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将pdf图转换为png或jpeg文件. 原因是我想将图像用于演示,并且我需要这两种格式,它们具有完全相同的尺寸/缩放比例.

I'm trying to convert a pdf plot to a png or jpeg file. The reason is that I want to use the images for presentations and I need both formats, having exactly the same dimensions/scaling.

我尝试了动画包中的函数im.convert(),但是输出看起来真的很差,无论是png还是jpeg.

I tried the function im.convert() in the animation package, but the output looks really bad, in both png and jpeg.

要运行以下代码,您需要动画"程序包和ImageMagick软件( http://www.imagemagick.org/script/convert.php )

To be able to run the following code you need the "animation" package and the ImageMagick software (http://www.imagemagick.org/script/convert.php)

library("animation")
ani.options(outdir = getwd())

pdf("bm.pdf")
plot(1:10)
dev.off()

im.convert("bm.pdf", output = "bm.jpeg")
im.convert("bm.pdf", output = "bm.png")

推荐答案

im.convert的结果可能不令人满意,因为它使用默认分辨率,即74 dpi.您可以通过传递额外的参数来提高分辨率:

The result of im.convert is probably not satisfactory because it uses the default resolution, which is 74 dpi. You can increase the resolution by passing an extra parameter:

im.convert("bm.pdf", output = "bm.png", extra.opts="-density 150")

-density 150将使分辨率提高一倍,并且您的PNG和JPEG看起来会更好.

-density 150 will double the resolution and your PNGs and JPEGs will look nicer.

但是通常,最好使用png()jpeg()生成图并使用适当的参数来获得与pdf()相同的结果.例如:

But in general it is probably better to use png() and jpeg() to generate the plots and use appropiate parameters to get the same results as with pdf(). For example:

pdf(width=5, height=5)
plot(1:10)
dev.off() 

png(width=5, height=5, units="in", res=150) 
plot(1:10)
dev.off()

这篇关于在R中将pdf转换为png的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆