-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg2pdf.py
48 lines (39 loc) · 1.36 KB
/
img2pdf.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
import os
import fitz # pymupdf
from PyQt5.QtCore import QThread, pyqtSignal
class Img2pdf(QThread):
signal = pyqtSignal(int, int)
def __init__(self):
super(Img2pdf, self).__init__()
self.img_path = None
self.out_path = None
self.pdf_name = None
def __del__(self):
self.wait()
def setData(self, img_path, out_path, pdf_name):
self.img_path = img_path
self.out_path = out_path
self.pdf_name = pdf_name
def run(self):
self.convert()
def convert(self):
doc = fitz.open()
img_dir = os.listdir(self.img_path)
total = len(img_dir)
count = 0
for file_name in img_dir:
count += 1
self.signal.emit(count, total)
if not (file_name.endswith('.jpg') or file_name.endswith('.png')):
continue
img_ab_path = self.img_path + os.sep + file_name
imgdoc = fitz.open(img_ab_path)
imgbytes = imgdoc.convert_to_pdf()
imgpdf = fitz.open('pdf', imgbytes)
doc.insert_pdf(imgpdf)
doc.save(os.path.join(self.out_path, self.pdf_name))
doc.close()
# img = Img2pdf()
# img.convert('E:\\图片\\NieR Art 幸田和磨アート集',
# 'E:\\图片\\NieR Art 幸田和磨アート集',
# 'NieR Art 幸田和磨アート集.pdf')