Python Wand convert PDF to PNG disable transparent (alpha_channel)

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

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

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

Leave a Comment