Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Wand将PDF转换为PNG禁用透明(alpha_通道)_Python_Imagemagick_Wand - Fatal编程技术网

Python Wand将PDF转换为PNG禁用透明(alpha_通道)

Python Wand将PDF转换为PNG禁用透明(alpha_通道),python,imagemagick,wand,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

我正在尝试将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
回溯(最近一次呼叫最后一次):
文件“File_convert.py”,第20行,在
将图像(filename='sample.pdf',分辨率=300,背景=颜色('white')作为img:
文件“/Users/Frank/.virtualenvs/wand/lib/python2.7/site packages/wand/image.py”,第1943行,在__
raise TypeError(“空白图像参数不能与图像一起使用”)
TypeError:空白图像参数不能与图像打开参数一起使用
从中,尝试创建带有背景色的空图像,然后合成

从wand.image导入图像
从wand.color导入颜色
图像(filename=“sample.pdf”,分辨率=300)为img:
将图像(宽度=img.width,高度=img.height,背景=颜色(“白色”)作为背景:
bg.复合材料(img,0,0)
保存(filename=“image.png”)
另一个答案(与白色图像合成)有效,但仅在最后一页有效,直接设置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)

我认为这可能是wand中的一个bug。似乎为PDF设置alpha通道应该会影响所有页面,但不会。我还有一些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.

参考资料:

对于那些仍然对此有问题的人,我找到了解决方案(它在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')

在编译其他答案时,我使用以下函数将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)

这在安装wand和imagemagick后非常有用,可以节省调试时间;)非常简单的解决方案,工作非常完美!在
魔杖
和ImageMagick上花了半个小时之后,我终于用了一个。灵感来自。