Cowplot中的draw_image()函数导致pdf模糊 [英] The draw_image() function from cowplot results in blurred pdfs
问题描述
读取矢量化的pdf有时会导致来自Cowplot的 draw_image()
函数严重模糊的图像:
Reading a vectorized pdf sometimes result in a badly blurred image with the draw_image()
function from cowplot:
library(ggplot2)
library(cowplot)
library(magick)
# make pdf input as example
p <- ggplot(iris, aes(Sepal.Length, Sepal.Width, shape = Species)) +
geom_point() + scale_shape_manual(values = 21:23) + theme_classic()
ggsave("input.pdf", p, width = 6, height = 4.2)
# now draw with draw_image() and then write as png
fig <- ggdraw() + draw_image("input.pdf")
ggsave("output.png", fig, width = 1, height = .7, dpi = 1200) # blurred image
但是,阅读SVG可以正常工作:
However, reading SVGs works fine:
fig <- ggdraw() +
draw_image("http://jeroen.github.io/images/tiger.svg")
ggsave("output.png", fig, width = 1, height = .7, dpi = 1200)
此外,使用:
magick::image_read_pdf("input.pdf")
导致非模糊输出.
推荐答案
我不完全确定为什么SVG和pdf的处理方式不同,或者用 magick :: image_read()
(这是 draw_image()
在内部使用的方法),但是一种解决方案是在 draw_image()内使用
magick :: image_read_pdf()
代码>.函数 magick :: image_read_pdf()
将pdf转换为光栅图像,我们可以使用 density
参数指定所需的分辨率:
I'm not entirely sure why SVGs and pdfs are handled differently, or what exactly happens when you read pdfs with magick::image_read()
(which is what draw_image()
uses internally), but one solution is to use magick::image_read_pdf()
inside of draw_image()
. The function magick::image_read_pdf()
converts the pdf into a raster image, and we can specify the resolution we want with the density
argument:
fig <- ggdraw() + draw_image(magick::image_read_pdf("input.pdf", density = 600))
ggsave("output.png", fig, width = 1, height = .7, dpi = 1200)
这篇关于Cowplot中的draw_image()函数导致pdf模糊的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!