'How do I convert a transparent PNG to PDF using Pillow python

I am trying to convert a png image to pdf using Pillow. Most of the PNG images have a transparent background and upon converting, the background defaults to black and some of the image (anything in black) is blended out .

This is what I have attempted:

def convert_single_file_to_pdf(self, filepath):
    filename = filepath.stem
    parent_dir = filepath.parent
    save_as = str(Path(parent_dir, filename).with_suffix(".pdf").resolve())

    image_1 = Image.open(str(filepath))
    im_1 = image_1.convert('RGB')
    im_1.save(save_as)

I tried converting to RGBA by using image_1.convert('RGBA') instead and it caused the error: ValueError: cannot save mode RGBA



Sources

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

Source: Stack Overflow

Solution Source