-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpdftoimg.py
More file actions
35 lines (25 loc) · 861 Bytes
/
pdftoimg.py
File metadata and controls
35 lines (25 loc) · 861 Bytes
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
import fitz
import os
from PIL import Image
def pdf2image1(pdf,path1, path2):
pdfDoc = fitz.open(path1)
pg=0
page = pdfDoc.load_page(pg)
pix = page.get_pixmap(matrix=fitz.Matrix(4,4))
print(pix.width, pix.height)
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
if not os.path.exists(path2):
os.makedirs(path2)
img.save(path2 + '/' + f'{pdf}.jpg', )
# img.save(f'output_{page_number}.png',)
# pix.save(path2 + '/' + 'images_%s.png' % pg) # 将图片写入指定的文件夹内
pdfs=[]
pdfs_path="./paper_pdfs"
for filename in os.listdir(pdfs_path):
if filename.endswith('.pdf'):
new_filename,_ = os.path.splitext(filename)
pdfs.append(new_filename)
for pdf in pdfs:
path1 = f"./paper_pdfs/{pdf}.pdf"
path2 = "./images"
pdf2image1(pdf,path1, path2)