-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf2png.py
60 lines (47 loc) · 1.53 KB
/
pdf2png.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
import fitz
import os
from PIL import Image
def pdf2png(filename, page_num=None, size_factor=1, ):
print(filename)
png_filename = filename[:filename.rindex(".pdf")] + ".png"
zoom_x, zoom_y = size_factor, size_factor
png_list = []
sub_png_list = []
pdf = fitz.open(filename)
width, height = 0, 0
mode = None # as flag
if isinstance(page_num, int):
page_num = [page_num, ]
elif isinstance(page_num, (tuple, list)):
pass
else:
page_num = []
page_num = sorted(page_num)
for i in page_num:
if i >= pdf.page_count:
break
trans = fitz.Matrix(zoom_x, zoom_y)
pm = pdf.get_page_pixmap(i, matrix=trans, alpha=False)
sub_filename = png_filename + "-%d.png" % i
pm.save(sub_filename)
png = Image.open(sub_filename)
png_list.append(png)
if mode is None:
mode = png.mode
width = max(width, png.width)
height += png.height
sub_png_list.append(sub_filename)
if mode is None:
return False
result = Image.new(mode, (width, height))
count_height = 0
for i, png in enumerate(png_list):
result.paste(png, box=(0, count_height)) # 左对齐
count_height += png.height
result.save(png_filename)
for sub_f in sub_png_list:
os.remove(sub_f)
return True
if __name__ == '__main__':
filename = "./phycs2018.pdf"
pdf2png(filename, page_num=(0, 1, 2), size_factor=2) # 将上述pdf的第0,1,2页转成图片(拼在一起)