如何使用imagemagick将pdf的页面附加到python中的png

时间:2017-03-08 00:51:30

标签: python imagemagick wand magickwand

我希望将多页PDF转换为单个PNG,这可以通过CLI按convert in.pdf -append out%d.pngConvert multipage PDF to a single image来实现。

我可以在Python中实现同样的功能吗?我目前有:

with Image(filename=pdf_file_path, resolution=150) as img:
    img.background_color = Color("white")
    img.alpha_channel = 'remove'
    img.save(filename=pdf_file_path[:-3] + "png")

1 个答案:

答案 0 :(得分:0)

我无法记住MagickAppendImage是否已被移植到,但您应该能够利用wand.image.Image.composite

from wand.image import Image

with Image(filename=pdf_file_path) as pdf:
    page_index = 0
    height = pdf.height
    with Image(width=pdf.width,
               height=len(pdf.sequence)*height) as png:
        for page in pdf.sequence:
            png.composite(page, 0, page_index * height)
            page_index += 1
        png.save(filename=pdf_file_path[:-3] + "png")