'Saving multiple images into different pdf names

I have a collection of images and their corresponding names:

image_data = {
    'images': ['image1.jpg', 'image2.jpg', 'img1.jpg', 'img2.jpg', 'img_1.jpg', 'img_2.jpg'],
    'names': ['first_image.pdf','first_image.pdf','second_image.pdf','second_image.pdf','third_image.pdf','third_image.pdf']
}

I want to save the series of images into their corresponding .pdf file names, as a single pdf for each name. So, the result should produce:

first_image.pdf
second_image.pdf
third_image.pdf

Each having two images. Here's what I have tried:

from fpdf import FPDF
pdf = FPDF()
# imagelist is the list with all image filenames
for pdf_,image in zip(image_data['names'], image_data['images']):
    pdf.add_page()
    pdf.image(image,w=190,h=280)
    pdf.output(f"{pdf_}", "F")

However, I get the following error:

RuntimeError: FPDF error: Not a PNG file:

Not sure how to proceed as when I converted the image to .jpg, I got the same issue. How can I properly convert these images to .pdf with their associative file names ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source