0

我想将多页 PDF 转换为单个 PNG,这可以通过 CLI 与convert in.pdf -append out%d.pngper Convert 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")
4

1 回答 1

1

我不记得是否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")
于 2017-03-08T14:13:35.447 回答