Search code examples
pythonpdfrgbaimg2pdf

img2pdf AlphaChannelError: what is the best way to remove alphachannel


I have set of images from which I create pdf by the following code

with io.BytesIO() as tmp_io:
    tmp_io.write(img2pdf.convert(img_file_paths))
    result_bytes = tmp_io.getvalue()

One of files contains alpha channel and I got

raise AlphaChannelError("Refusing to work on images with alpha channel")

What is the simplest way to remove alpha channel and save to pdf rgb channels?


Solution

  • Here is bit ugly solution from myself

    def remove_alpha_from_image(image_path):
        im = Image.open(image_path)
        im.load()
        try:
            background = Image.new("RGB", im.size, (255, 255, 255))
            background.paste(im, mask=im.split()[3])  # 3 is the alpha channel
            im = background
        except IndexError:  # img is not RGBA
            pass
    
        name_hash_md5 = md5(bytes(image_path, encoding="utf-8"))  # noqa: S303
        name = name_hash_md5.hexdigest()
        if not os.path.exists(TMP_DIR):
            os.makedirs(TMP_DIR)
        path = f"{TMP_DIR}{name}.pdf"
        im.save(path, "PNG", resolution=100.0)
        return path
    
    with io.BytesIO() as tmp_io:
        try:
            tmp_io.write(img2pdf.convert(file_paths))
        except img2pdf.AlphaChannelError:
            tmp_io.write(img2pdf.convert([remove_alpha_from_image(path) for path in file_paths]))
    
        result_bytes = tmp_io.getvalue()