-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathimg2pdf.py
63 lines (52 loc) · 1.53 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import os
import string
from PIL import Image
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import portrait
import sys
from tqdm import trange
def file_name(file_dir, suffix=".jpg"):
L = []
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == suffix:
L.append(os.path.join(root, file))
L = sorted(L, key=lambda x: int(x.split('\\')[-1].split('/')[-1][:-4]))
return L
def conpdf(f_pdf, filedir, suffix=".jpg", scale=False):
fileList = file_name(filedir, suffix)
img = Image.open(fileList[0])
if scale and img.size[0] < img.size[1]:
# (w, h) = (940, 529)
(w, h) = (848, 1168)
else:
(w, h) = img.size
c = canvas.Canvas(f_pdf, pagesize=(w, h))
for i in trange(len(fileList)):
f = fileList[i]
(xsize, ysize) = Image.open(f).size
ratx = xsize / w
raty = ysize / h
ratxy = xsize / (1.0 * ysize)
if ratx > 1:
ratx = 0.99
if raty > 1:
raty = 0.99
rat = ratx
if ratx < raty:
rat = raty
widthx = w * rat
widthy = h * rat
widthx = widthy * ratxy
posx = (w - widthx) / 2
if posx < 0:
posx = 0
posy = (h - widthy) / 2
if posy < 0:
posy = 0
c.drawImage(f, posx, posy, widthx, widthy)
c.showPage()
c.save()
print("转码完毕")
if __name__ == "__main__":
conpdf('output.pdf', 'temp', '.png')