import os
from PIL import Image
from PyPDF2 import PdfFileMerger, PdfFileWriter

def convert_jpg_to_pdf(input_file, output_file):
    with open(output_file, 'wb') as pdf_file:
        pdf_writer = PdfFileWriter()

        if isinstance(input_file, str):
            input_file = [input_file]

        for file in input_file:
            if not file.endswith('.jpg'):
                print(f"Skipping {file}: Not a JPEG image")
                continue

            with Image.open(file) as img:
                img = img.convert('RGB')
                pdf_writer.addPage(img2pdf(img))

        pdf_writer.write(pdf_file)

def img2pdf(img):
    img_file = io.BytesIO()
    img.save(img_file, 'PDF', resolution=100.0)
    pdf_bytes = img_file.getvalue()
    img_file.close()
    return PdfFileMerger().append(PdfFileReader(io.BytesIO(pdf_bytes))).getPage(0)

if __name__ == '__main__':
    input_file = 'image.jpg'  # Replace with your input file path
    output_file = 'output.pdf'  # Replace with your output file path

    convert_jpg_to_pdf(input_file, output_file)

    print(f"Conversion complete: {os.path.abspath(output_file)}")