-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdftoimage.py
More file actions
43 lines (35 loc) · 1.05 KB
/
pdftoimage.py
File metadata and controls
43 lines (35 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import argparse
from os import listdir, mkdir
from os.path import join, isdir
from subprocess import call
import sys
import datasets
from shutil import which
COLOR_IMAGE_DPI = 300
from pathlib import Path
"""
Script to use pdftoppm to turn the pdfs into single images per page
"""
def get_images(pdfname, output_dir, dpi):
if which("pdftoppm") is None:
raise ValueError("Requires executable pdftopmm to be on the PATH")
if not isdir(output_dir):
print("Making %s to store rasterized PDF pages" % output_dir)
Path(output_dir).mkdir(parents=True)
if not pdfname.endswith(".pdf"):
raise ValueError()
doc_id = pdfname.split("/")[-1][:-4]
args = [
"pdftoppm",
"-jpeg",
"-r",
str(dpi),
"-cropbox",
pdfname,
join(output_dir, doc_id + "-page"),
]
retcode = call(args)
if retcode != 0:
raise ValueError("Bad return code for <%s> (%d)", " ".join(args), retcode)
if __name__ == "__main__":
get_images("result/pdfs/", "result/pdfs", COLOR_IMAGE_DPI)