20

I'm trying to convert a PDF to PNG - this all works fine, however, the output image is still transparent even when I believe I have disabled it:

with Image(filename='sample.pdf', resolution=300) as img:
    img.background_color = Color("white")
    img.alpha_channel = False
    img.save(filename='image.png')

The above produces the images but are transparent, I also tried the below:

with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
    img.alpha_channel = False
    img.save(filename='image.png')

which produces this error:

Traceback (most recent call last):
  File "file_convert.py", line 20, in <module>
    with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
  File "/Users/Frank/.virtualenvs/wand/lib/python2.7/site-packages/wand/image.py", line 1943, in __init__
    raise TypeError("blank image parameters can't be used with image "
TypeError: blank image parameters can't be used with image opening parameters
Munro
  • 249
  • 1
  • 3
  • 4
  • After spending half an hour on `wand` and ImageMagick, and [all the complexity needed on Windows](https://stackoverflow.com/questions/13984357/pythonmagick-cant-find-my-pdf-files), I finally used a [very short solution using `pdftoppm`](https://stackoverflow.com/a/50476765/1422096). Inspired by [How to convert PDF to Image?](https://askubuntu.com/a/50180/364627). – Basj May 22 '18 at 21:35

5 Answers5

15

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

Manuel Riel
  • 2,208
  • 12
  • 14
  • This is useful after installing wand and imagemagick and can save hours of debugging ;) https://stackoverflow.com/questions/13984357/pythonmagick-cant-find-my-pdf-files – Basj May 22 '18 at 20:46
  • very simple solution and just works perfectly! – Rajesh Jan 14 '21 at 14:55
9

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")
Community
  • 1
  • 1
emcconville
  • 20,969
  • 4
  • 43
  • 60
9

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)
Averner
  • 78
  • 2
  • 7
Thibaut Mattio
  • 732
  • 5
  • 17
2

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.

RecursivelyIronic
  • 7,443
  • 2
  • 28
  • 37
2

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:

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


with Image(filename='sample.pdf', resolution=300) as img:
img.background_color = Color("white")
img.alpha_channel = 'remove'
img.save(filename='image.png')
Community
  • 1
  • 1
ands
  • 1,463
  • 11
  • 21