如何使用 magick::image_read_pdf 获取 pdf 文件的总页数?
How to get total number of pages of pdf files using magick::image_read_pdf?
假设在一个文件夹下 main_path
,我们有多个页数不同的 pdf 文件,我使用下面的函数循环所有文件并截取每一页:
library(magick)
library(glue)
main_path <- './'
file_names <- list.files(path = main_path, pattern ='.pdf')
file_paths <- file.path(main_path, file_names)
file_names_no_ext <- tools::file_path_sans_ext(file_names)
max_page <- 10
pdf2plot <- function(file_path, file_names_no_ext){
pages <- magick::image_read_pdf(file_path)
print(pages)
num <- seq(1, max_page, 1)
# num <- seq(1, nrow(data.frame(pages)), 1)
for (i in num){
pages[i] %>% image_write(., path = paste0(glue(main_path, '/plot/', {file_names_no_ext},
sprintf('_%02d.', i)), format = "png"))
}
}
mapply(pdf2plot, file_paths, file_names_no_ext)
我遇到的问题是,如果我们在文件夹中有一个总页数少于 max_page
的文件,它会引发一个 Error in magick_image_subset(x, i) : subscript out of bounds
。例如,我有一个文件有 2 页,但我设置 max_page=10
,我会得到这个错误。
pages
内容:
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 PNG 2250 3000 sRGB TRUE 0 300x300
2 PNG 2250 3000 sRGB TRUE 0 300x300
3 PNG 2250 3000 sRGB TRUE 0 300x300
4 PNG 2250 3000 sRGB TRUE 0 300x300
5 PNG 2250 3000 sRGB TRUE 0 300x300
6 PNG 2250 3000 sRGB TRUE 0 300x300
7 PNG 2250 3000 sRGB TRUE 0 300x300
8 PNG 2250 3000 sRGB TRUE 0 300x300
9 PNG 2250 3000 sRGB TRUE 0 300x300
Error in magick_image_subset(x, i) : subscript out of bounds
Called from: magick_image_subset(x, i)
我认为有两种方法可以解决这个问题,但我还不知道怎么做:1.使用try-catch
,2.用获取总数替换max_page
使用 magick::image_read_pdf
.
的页面
感谢您的提前帮助。
如果您查看 ?image_read
的文档,我们可以看到:
All standard base vector methods such as [, [[, c(), as.list(), as.raster(), rev(), length(), and print() can be used to work with magick image objects. Use the standard img[i] syntax to extract a subset of the frames from an image.
因此您只需使用 length(pages)
即可获取该文档的页数。这是使用 lapply()
的函数的简单版本。我认为您可以大大简化您的路径,但不会深入研究。
library(magick)
library(glue)
pdf2plot <- function(file_path, file_names_no_ext){
pages <- magick::image_read_pdf(file_path)
lapply(
1:length(pages),
\(i) image_write(pages[i], path = paste0(glue(main_path, '/plot/', {file_names_no_ext},
sprintf('_%02d.', i)), format = "png"))
)
}
使用 R 4.1.0 生成的代码
假设在一个文件夹下 main_path
,我们有多个页数不同的 pdf 文件,我使用下面的函数循环所有文件并截取每一页:
library(magick)
library(glue)
main_path <- './'
file_names <- list.files(path = main_path, pattern ='.pdf')
file_paths <- file.path(main_path, file_names)
file_names_no_ext <- tools::file_path_sans_ext(file_names)
max_page <- 10
pdf2plot <- function(file_path, file_names_no_ext){
pages <- magick::image_read_pdf(file_path)
print(pages)
num <- seq(1, max_page, 1)
# num <- seq(1, nrow(data.frame(pages)), 1)
for (i in num){
pages[i] %>% image_write(., path = paste0(glue(main_path, '/plot/', {file_names_no_ext},
sprintf('_%02d.', i)), format = "png"))
}
}
mapply(pdf2plot, file_paths, file_names_no_ext)
我遇到的问题是,如果我们在文件夹中有一个总页数少于 max_page
的文件,它会引发一个 Error in magick_image_subset(x, i) : subscript out of bounds
。例如,我有一个文件有 2 页,但我设置 max_page=10
,我会得到这个错误。
pages
内容:
format width height colorspace matte filesize density
<chr> <int> <int> <chr> <lgl> <int> <chr>
1 PNG 2250 3000 sRGB TRUE 0 300x300
2 PNG 2250 3000 sRGB TRUE 0 300x300
3 PNG 2250 3000 sRGB TRUE 0 300x300
4 PNG 2250 3000 sRGB TRUE 0 300x300
5 PNG 2250 3000 sRGB TRUE 0 300x300
6 PNG 2250 3000 sRGB TRUE 0 300x300
7 PNG 2250 3000 sRGB TRUE 0 300x300
8 PNG 2250 3000 sRGB TRUE 0 300x300
9 PNG 2250 3000 sRGB TRUE 0 300x300
Error in magick_image_subset(x, i) : subscript out of bounds
Called from: magick_image_subset(x, i)
我认为有两种方法可以解决这个问题,但我还不知道怎么做:1.使用try-catch
,2.用获取总数替换max_page
使用 magick::image_read_pdf
.
感谢您的提前帮助。
如果您查看 ?image_read
的文档,我们可以看到:
All standard base vector methods such as [, [[, c(), as.list(), as.raster(), rev(), length(), and print() can be used to work with magick image objects. Use the standard img[i] syntax to extract a subset of the frames from an image.
因此您只需使用 length(pages)
即可获取该文档的页数。这是使用 lapply()
的函数的简单版本。我认为您可以大大简化您的路径,但不会深入研究。
library(magick)
library(glue)
pdf2plot <- function(file_path, file_names_no_ext){
pages <- magick::image_read_pdf(file_path)
lapply(
1:length(pages),
\(i) image_write(pages[i], path = paste0(glue(main_path, '/plot/', {file_names_no_ext},
sprintf('_%02d.', i)), format = "png"))
)
}
使用 R 4.1.0 生成的代码