PIL이라는 라이브러리와 natsort라는 라이브러를 사용하여,

 

 

아주 쉽게 PDF파일을 만들어내는 스크립트.

import os
from PIL import Image
import natsort


realpath = "/Users/1111964/Downloads/images"
originimages = natsort.natsorted(os.listdir(realpath))
images = []

for i in originimages:
    try:
       print (i)
       print (Image.open (realpath + '/' + i))
       images.append(Image.open (realpath + '/' + i))
    except:
       print ("error : " + i)


images[0].save(
    realpath +"/out.pdf", "PDF", resolution=100.0, save_all=True, append_images=images[1:]
)

 

ㅇ 당연한것이지만 image, narsort에 대한 PIP 라이브러리 설치가 선행되어야 한다.

ㅇ 이미지 파일들이 모아놓은 폴더 주소를 딴다. 여기서는 맥북 기준으로 "/Users/1111964/Downloads/images"를 사용했다. 윈도우면 "C:\\Users\\USER\\Downloads" 대략 이런식으로 폴더 지정 하면 된다"

 

 

ㅇ이름이나 저장경로를 바꾸고 싶으면 realpath +"/out.pdf" 이부분을 손봐주시면 되겠다. 여기는

이미지가 모여있는 폴더에서 out.pdf라는 파일로 저장하겠다는 것이다.

 

 

 

 

+ Recent posts