将多页pdf的每一页转换为R中单独的png文件
Convert each page of a multi-paged pdf into separate png files in R
我看到 few questions asked 涉及尝试将 pdf 转换为 png,但 none 的答案显示了如何将多页 pdf 的每一页保存为不同的 png 文件。
从 13 页 pdf 示例开始:
# exmaple pdf
example_pdf <- "https://arxiv.org/ftp/arxiv/papers/1312/1312.2789.pdf"
如何将pdf的每一页另存为不同的png文件?
我们可以使用 magick package
:
中的 image_read_pdf
函数为每个页面创建一个 png
#install magick package
install.packages("magick")
library("magick")
# creating magick-image class with a png for each page of the pdf
pages <- magick::image_read_pdf(example_pdf)
pages
# saving each page of the pdf as a png
j <- 1:13
for (i in j){
pages[i] %>% image_write(., path = paste0("image",i,".png"), format = "png")
}
这会在您的主目录文件中将每个页面保存为 "image(page number).png"。
我看到 few questions asked 涉及尝试将 pdf 转换为 png,但 none 的答案显示了如何将多页 pdf 的每一页保存为不同的 png 文件。
从 13 页 pdf 示例开始:
# exmaple pdf
example_pdf <- "https://arxiv.org/ftp/arxiv/papers/1312/1312.2789.pdf"
如何将pdf的每一页另存为不同的png文件?
我们可以使用 magick package
:
image_read_pdf
函数为每个页面创建一个 png
#install magick package
install.packages("magick")
library("magick")
# creating magick-image class with a png for each page of the pdf
pages <- magick::image_read_pdf(example_pdf)
pages
# saving each page of the pdf as a png
j <- 1:13
for (i in j){
pages[i] %>% image_write(., path = paste0("image",i,".png"), format = "png")
}
这会在您的主目录文件中将每个页面保存为 "image(page number).png"。