简体   繁体   English

Python Wand 将 PDF 转换为 PNG 禁用透明 (alpha_channel)

[英]Python Wand convert PDF to PNG disable transparent (alpha_channel)

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:我正在尝试将 PDF 转换为 PNG - 这一切正常,但是,即使我认为我已禁用它,输出图像仍然是透明的:

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

I also had some PDFs to convert to PNG.我还有一些 PDF 可以转换为 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参考: 魔杖.image

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")

Compiling the other answers, here is the function I use to convert a PDF into pages:编译其他答案,这是我用来将 PDF 转换为页面的函数:

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.另一个答案(与白色图像合成)有效,但仅限于最后一页,就像直接设置 alpha 通道一样。 The following works on wand 0.4.2:以下适用于魔杖 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.似乎为 PDF 设置 alpha 通道应该会影响所有页面,但事实并非如此。

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).对于那些仍然有问题的人,我找到了解决方案(它适用于 0.4.1 及更高版本,我不确定早期版本)。 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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM