Python Wand 将 PDF 转换为 PNG 禁用透明 (alpha_channel)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27826854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Wand convert PDF to PNG disable transparent (alpha_channel)
提问by Munro
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
回答by emcconville
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")
回答by RecursivelyIronic
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:
另一个答案(与白色图像合成)有效,但仅限于最后一页,就像直接设置 alpha 通道一样。以下适用于魔杖 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 shouldaffect all pages, but it doesn't.
我认为这可能是魔杖的错误。似乎为 PDF 设置 alpha 通道应该会影响所有页面,但事实并非如此。
回答by Manuel Riel
I also had some PDFs to convert to PNG. This worked for me and seems simpler than compositing images, as shown above.:
我还有一些 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.
Reference: wand.image
参考:wand.image
回答by ands
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:
对于那些仍然有问题的人,我找到了解决方案(它适用于 0.4.1 及更高版本,我不确定早期版本)。所以你应该使用这样的东西:
with Image(filename='sample.pdf', resolution=300) as img:
img.background_color = Color("white")
img.alpha_channel = 'remove'
img.save(filename='image.png')
回答by Thibaut Mattio
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)