Created
November 14, 2019 19:17
-
-
Save Ignimbrit/aecc3703e1534407d7547a17c71737aa to your computer and use it in GitHub Desktop.
Extract PDF pages as PNG images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pdf_decompose <- function(input_file, output_folder, pages = NA){ | |
pdf_length <- as.integer(pdftools::pdf_info(input_file)$pages) | |
if(is.na(pages[1])){pages <- seq(1, pdf_length, 1)} | |
if(!all(purrr::map_lgl(pages, is.numeric))){ | |
stop("pages must be a numeric vector") | |
} | |
pages <- pages[pages <= pdf_length] | |
for (i in seq_along(pages)){ | |
img <- magick::image_read_pdf( | |
path = input_file, | |
pages = pages[i] | |
) | |
magick::image_write( | |
img, | |
path = paste0(output_folder, "decomposed_", pages[i], ".png"), | |
format = "png", | |
quality = 100 | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment