-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg2pdf.py
More file actions
executable file
·66 lines (49 loc) · 1.88 KB
/
img2pdf.py
File metadata and controls
executable file
·66 lines (49 loc) · 1.88 KB
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
64
65
66
#!/usr/bin/python
from argparse import ArgumentParser, Namespace
from pathlib import Path
from PIL import Image
SUFFIXES = (
'.png',
'.jpg',
'.jpeg'
)
def get_images(folder: Path) -> list[Image.Image]:
return [Image.open(path) for path in folder.iterdir()
if path.is_file() and path.suffix in SUFFIXES]
def find_out_rotation(image: Image.Image) -> int:
if image.width > image.height: # TODO: detection of 0, 90, 180, 270
return Image.ROTATE_90
def convert2portrait(images: list[Image.Image]) -> list[Image.Image]:
result = []
for image in images:
if (rotation := find_out_rotation(image)):
image = image.transpose(rotation)
result.append(image)
return result
def save2pdf(images: list[Image.Image], path: Path, quality: int) -> None:
images[0].save(path, format='pdf', quality=quality,
save_all=True, append_images=images[1:])
def main(folder: Path, output: Path, quality: int, portrait: bool) -> None:
images = get_images(folder)
if portrait:
images = convert2portrait(images)
save2pdf(images, output, quality)
def parse_args() -> Namespace:
parser = ArgumentParser(description='Convert photos to pdf')
parser.add_argument('folder', type=Path, help='folder with photos')
parser.add_argument(
'-o', '--output', dest='output', type=Path, default='output.pdf',
help='output pdf path'
)
parser.add_argument(
'-q', '--quality', dest='quality', type=int, default=70,
help='quality of output images on a scale from 0 (worst) to 100 (best)'
)
parser.add_argument(
'-p', '--portrait', dest='portrait', action='store_true',
help='convert photos to portrait orientation'
)
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args.folder, args.output, args.quality, args.portrait)