21

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

5 回答 5

17

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

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.

参考:wand.image

于 2016-03-29T08:36:03.460 回答
9

上一个答案中,尝试创建一个具有背景颜色的空图像,然后进行合成。

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")
于 2015-01-07T19:40:54.650 回答
9

编译其他答案,这是我用来将 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)
于 2017-03-01T06:52:43.587 回答
2

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

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')
于 2016-11-08T18:48:11.773 回答
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 通道应该会影响所有页面,但事实并非如此。

于 2016-02-16T02:54:11.907 回答