Advertisement
Guest User

img2pdf

a guest
Jan 14th, 2021
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. from PIL import Image
  2. from pathlib import Path
  3. from natsort import natsorted
  4.  
  5.  
  6. path = Path.cwd() if __name__ == "__main__" else Path.cwd() / "book"
  7. un_images_list = list(Path(path).glob("*.jpg"))
  8. images_list = natsorted([fr"{i.as_posix()}" for i in un_images_list])
  9.  
  10.  
  11. def create_pdf(filename: str):
  12.     images = []
  13.     for image in images_list:
  14.         img_opn = Image.open(image)
  15.         img_opn = img_opn.convert("RGB")
  16.         images.append(img_opn)
  17.         print(f"{image} добавлена")
  18.     img = Image.open(un_images_list[0])
  19.     img.save(f"{filename}.pdf", save_all=True, append_images=images)
  20.     print("ПДФ сохранен!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement