Python Wand convert PDF to PNG disable transparent (alpha_channel)

。_饼干妹妹 提交于 2019-11-27 03:51:49

I also had some PDFs to convert to PNG. This worked for me and seems simpler than compositing images, as shown above.:

all_pages = Image(blob=self.pdf)        # PDF will have several pages.
single_image = all_pages.sequence[0]    # Just work on first page
with Image(single_image) as i:
    i.format = 'png'
    i.background_color = Color('white') # Set white background.
    i.alpha_channel = 'remove'          # Remove transparency and replace with bg.

Reference: wand.image

emcconville

From a previous answer, try creating an empty image with a background color, then composite over.

from wand.image import Image
from wand.color import Color

with Image(filename="sample.pdf", resolution=300) as img:
  with Image(width=img.width, height=img.height, background=Color("white")) as bg:
    bg.composite(img,0,0)
    bg.save(filename="image.png")
Thibaut Mattio

Compiling the other answers, here is the function I use to convert a PDF into pages:

import os
from wand.image import Image
from wand.color import Color


def convert_pdf(filename, output_path, resolution=150):
    """ Convert a PDF into images.

        All the pages will give a single png file with format:
        {pdf_filename}-{page_number}.png

        The function removes the alpha channel from the image and
        replace it with a white background.
    """
    all_pages = Image(filename=filename, resolution=resolution)
    for i, page in enumerate(all_pages.sequence):
        with Image(page) as img:
            img.format = 'png'
            img.background_color = Color('white')
            img.alpha_channel = 'remove'

            image_filename = os.path.splitext(os.path.basename(filename))[0]
            image_filename = '{}-{}.png'.format(image_filename, i)
            image_filename = os.path.join(output_path, image_filename)

            img.save(filename=image_filename)

The other answer (compositing with a white image) works, but only on the last page, as does setting the alpha channel directly. The following works on wand 0.4.2:

im = wand_image(filename='/tmp/foo.pdf', resolution=200)
for i, page in enumerate(im.sequence):
    with wand_image(page) as page_image:
        page_image.alpha_channel = False
        page_image.save(filename='/tmp/foo.pdf.images/page-%s.png' % i)

I think this is probably a bug in wand. It seems like setting the alpha channel for a PDF should affect all pages, but it doesn't.

For those who are still having problem with this, I found solution (it works in version 0.4.1 and above, I am not sure about earlier versions). So you should just use something like this:

with Image(filename='sample.pdf', resolution=300) as img:
img.background_color = Color("white")
img.alpha_channel = 'remove'
img.save(filename='image.png')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!