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

时间:2015-01-07 19:10:52

标签: python imagemagick wand

我正在尝试将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')

以上产生图像但透明,我也尝试过以下内容:

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

产生此错误:

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

5 个答案:

答案 0 :(得分:10)

我也有一些PDF可以转换为PNG。这对我有用,似乎比合成图像更简单,如上所示。:

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.

参考:wand.image

答案 1 :(得分:9)

previous answer开始,尝试使用背景颜色创建空图像,然后进行合成。

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

答案 2 :(得分:5)

编译其他答案,这是我用来将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)

答案 3 :(得分:2)

另一个答案(与白色图像合成)有效,但仅限于最后一页,直接设置Alpha通道也是如此。以下适用于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)

我认为这可能是魔杖中的一个错误。似乎为PDF 设置alpha通道应该影响所有页面,但它不会。

答案 4 :(得分:2)

对于那些仍然有这个问题的人,我找到了解决方案(它适用于版本0.4.1及更高版本,我不确定早期版本)。 所以你应该使用这样的东西:

IFRAME --> AMP-IFRAME
IMG --> AMP_IMG 
VIDEO --> AMP-VIDEO