Add script with autoscaling
This commit is contained in:
commit
8c9c6736d1
3 changed files with 61 additions and 0 deletions
59
script.py
Executable file
59
script.py
Executable file
|
|
@ -0,0 +1,59 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
from pathlib import Path
|
||||
|
||||
import img2pdf
|
||||
|
||||
|
||||
SUPPORTED_EXTENSIONS = {".jpg", ".jpeg", ".png"}
|
||||
|
||||
|
||||
def images_to_pdf(images: list[Path], output: Path) -> None:
|
||||
# A4 page dimensions in PDF points (1 point = 1/72 inch).
|
||||
a4_size = (img2pdf.mm_to_pt(210), img2pdf.mm_to_pt(297))
|
||||
|
||||
# Scale images proportionally to fit within A4 pages.
|
||||
layout = img2pdf.get_layout_fun(
|
||||
pagesize=a4_size,
|
||||
fit=img2pdf.FitMode.into,
|
||||
auto_orient=True,
|
||||
)
|
||||
|
||||
with output.open("wb") as pdf_file:
|
||||
pdf_file.write(
|
||||
img2pdf.convert(
|
||||
[str(image) for image in images], #type: ignore
|
||||
layout_fun=layout,
|
||||
)
|
||||
)
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Convert JPEG/PNG images into a PDF, one image per page."
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"images",
|
||||
nargs="+",
|
||||
type=Path,
|
||||
help="Input JPEG/PNG images in page order",
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
type=Path,
|
||||
default=Path("output.pdf"),
|
||||
help="Output PDF file (default: output.pdf)",
|
||||
)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
images_to_pdf(args.images, args.output)
|
||||
|
||||
print(f"Created: {args.output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue