IT기타

파이썬 - 특정폴더 안에 image 파일 PDF 한 파일에 붙여넣기.

emilyyoo 2024. 7. 9. 22:07
728x90

from PIL import Image
import os

def images_to_pdf(directory, output_pdf):
    # List all files in the directory
    files = os.listdir(directory)
    
    # Filter out files that are not images
    images = [file for file in files if file.lower().endswith(('png', 'jpg', 'jpeg', 'tiff', 'bmp', 'gif'))]
    
    # Sort images by name
    images.sort()
    
    # Load images
    image_list = []
    for image in images:
        image_path = os.path.join(directory, image)
        print(f"Processing file: {image_path}")  # Debug message
        img = Image.open(image_path)
        if img.mode == 'RGBA':
            img = img.convert('RGB')
        image_list.append(img)
    
    # Save images as a single PDF
    if image_list:
        image_list[0].save(output_pdf, save_all=True, append_images=image_list[1:])

# Usage
directory_path = 'C:\\Users\\찜\\Documnts\\ImageZip'  # 이미지가 있는 폴더 경로
output_pdf_path = 'C:\\Users\\찜\\Documents\\output.pdf'  # 출력 PDF 파일 경로
images_to_pdf(directory_path, output_pdf_path)

728x90