from PIL import Image
import os
# Assuming '/mnt/data/my_images/' is the directory with PNG images
folder_path = '/mnt/data/my_images/'
pdfs_path = '/mnt/data/my_pdf/'
# Create a PDFs directory if it doesn't exist
os.makedirs(pdfs_path, exist_ok=True)
# List all files in the directory
files = os.listdir(folder_path)
# Filter out files that are not PNG
png_files = [f for f in files if f.lower().endswith('.png')]
# Convert each PNG image to PDF
for image_file in png_files:
image_path = os.path.join(folder_path, image_file)
pdf_path = os.path.join(pdfs_path, image_file.lower().replace('.png', '.pdf'))
# Open the image and convert it to PDF
image = Image.open(image_path)
# Ensure the image is in RGB mode (required for saving as PDF)
if image.mode != 'RGB':
image = image.convert('RGB')
image.save(pdf_path, 'PDF')
# Return the path to the directory containing the new PDFs
pdfs_path