2017-05-11 5 views
0

FPDF를 사용하여 이미지 폴더 (jpg)를 PDF로 변환하려고합니다. 이 코드는 세로 형식의 이미지에 적합합니다. 가로 형식의 이미지의 경우 세로 형식으로 표시됩니다. 오리엔테이션을 감지하고이를 PDF에 할당하는 방법이 있습니까?페이지 FPDF (파이썬)를 사용하여 이미지를 PDF로 변환 할 때의 방향

enter image description here

수정에 대한 업데이트 된 코드

from fpdf import FPDF 
from PIL import Image 
import glob 
import os 

image_directory = r'C:\coolbro\test\yay\test' 

extensions = ('*.jpg','*.png','*.gif') 

imagelist=[] 
for ext in extensions: 
    imagelist.extend(glob.glob(os.path.join(image_directory,ext))) 

for imagePath in imagelist: 


    cover = Image.open(imagePath) 
    width, height = cover.size 

    if height > width: 

     pdf = FPDF(unit = "pt", format = "legal") 

     pdf.add_page() 

     pdf.image(imagePath, 0, 0, 600) 

     destination = os.path.splitext(imagePath)[0] 
     pdf.output(destination + ".pdf", "F") 

    if width > height: 

     pdf = FPDF("L", unit = "pt", format = "legal") 

     pdf.add_page() 

     pdf.image(imagePath, 0, 0, 0, 600) 

     destination = os.path.splitext(imagePath)[0] 
     pdf.output(destination + ".pdf", "F") 
+0

을 당신이 원하는 모든 이미지 인 경우에는 L의 방향이 지금 내 세로의 모든 이미지를 차단 추가 img2pdf.py –

답변

1

코멘트 : 나는 방향

if Y > X: 
    # portrait 
else: 
    # landscape 
을 읽을 수있는 방법을 찾기 위해 노력했다

예를 들어, orientation = 'L' 추가 :

fpdf = FPDF(orientation = 'L', unit = 'mm', format='A4') 
+0

봐야한다. 세로 및 가로 이미지가있는 폴더가 있습니다. 수동으로 정렬 할 항목이 너무 많습니다. 당신이 선택해야하는 코드 또는 다른 방향으로 보인다. 오리엔테이션을 읽는 방법을 찾으려고했습니다. – ajd018

+0

"if"문을 두 번 사용하여 코드를 업데이트했습니다. 이 문제가 발생하거나 "else"를 사용해야합니까? – ajd018

+1

@ ajd018 : 동일하지만 'X <> Y'를 두 번 계산하십시오. – stovfl