-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto_image.py
61 lines (47 loc) · 1.77 KB
/
to_image.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import sys
from pdf2image.pdf2image import convert_from_path
def to_jpg(pdf_filename, result_basename=None):
"""
Convert a pdf file to a jpg file.
params:
pdf_filename: the pdf file to be converted
result_filename: the new jpg file to be saved
"""
if result_basename is None:
result_basename = pdf_filename[:-4]
images = convert_from_path(pdf_filename)
for i, image in enumerate(images):
image.save(f"{result_basename}_{i + 1}.jpg", "JPEG")
def to_png(pdf_filename, result_basename=None):
"""
Convert a pdf file to a png file.
params:
pdf_filename: the pdf file to be converted
result_filename: the new png file to be saved
"""
if result_basename is None:
result_basename = pdf_filename[:-4]
images = convert_from_path(pdf_filename)
for i, image in enumerate(images):
image.save(f"{result_basename}_{i + 1}.png", "PNG")
def main():
# python3 to_image.py -j <pdf> [<result_basename>]
# python3 to_image.py -p <pdf> [<result_basename>]
if len(sys.argv) < 3:
print("Usage:")
print(" python3 to_image.py -j <pdf> [<result_basename>] Convert to jpg")
print(" python3 to_image.py -p <pdf> [<result_basename>] Convert to png")
return
pdf_filename = sys.argv[2]
result_basename = None if len(sys.argv) < 4 else sys.argv[3]
if sys.argv[1] == "-j":
to_jpg(pdf_filename, result_basename)
elif sys.argv[1] == "-p":
to_png(pdf_filename, result_basename)
else:
print("Usage:")
print(" python3 to_image.py -j <pdf> [<result_basename>] Convert to jpg")
print(" python3 to_image.py -p <pdf> [<result_basename>] Convert to png")
return
if __name__ == "__main__":
main()