Python Wand将PDF转换为PNG禁用透明(alpha_channel)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python Wand将PDF转换为PNG禁用透明(alpha_channel)相关的知识,希望对你有一定的参考价值。
我正在尝试将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
答案
我也有一些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
另一答案
从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")
另一答案
编译其他答案,这是我用来将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)
另一答案
另一个答案(与白色图像合成)有效,但仅限于最后一页,直接设置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通道应该影响所有页面,但事实并非如此。
另一答案
对于那些仍然有问题的人,我找到了解决方案(它适用于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')
以上是关于Python Wand将PDF转换为PNG禁用透明(alpha_channel)的主要内容,如果未能解决你的问题,请参考以下文章
使用 GhostScript 将 PDF 转换为透明 PNG
imagemagick 将 RGB PNG 转换为 CMYK PDF
ImageMagick 将具有多页的 pdf 转换为高质量的 PNG