Python Wand convert PDF to PNG disable transparent (alpha_channel)

前端 未结 5 1898
孤街浪徒
孤街浪徒 2020-11-29 03:23

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(         


        
相关标签:
5条回答
  • 2020-11-29 03:26

    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

    0 讨论(0)
  • 2020-11-29 03:27

    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')
    
    0 讨论(0)
  • 2020-11-29 03:36

    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")
    
    0 讨论(0)
  • 2020-11-29 03:38

    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.

    0 讨论(0)
  • 2020-11-29 03:45

    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)
    
    0 讨论(0)
提交回复
热议问题