首页 > 解决方案 > Converting multipage PDF to TIFF does not work with Python library Wand

问题描述

Given the short, 5 page PDF file (attached at the bottom), and the following python code to convert to a multi-page TIFF:

from wand.image import Image


with Image(filename='5-page-pdf.pdf', resolution=200) as img:
    img.type = "grayscale"
    img.format = "tiff"
    img.compression = "lzw"
    img.save(filename="test.tiff")

results in a TIFF file that has pages 2-4 as what appears to be black text on a dark-grey (or maybe transparent) background. Other image processing libraries cannot open the file or render it.

Converting the same PDF with ImageMagick, which Wand uses, works just fine

convert -density 200 5-page-pdf.pdf -type grayscale -compress lzw 5-page-pdf.tiff

this produces a file that does work with other imaging libraries and looks correct in a TIFF viewer.

I've tried removing the alpha channel, I've tried setting the background color to 'White', and a few other things, to no avail. The TIFF that comes out of Wand is always garbled. If it's doable in ImageMagick it should be doable in Wand, right? What parameter or setting am I missing?

Original PDF

Wand Produced TIFF

标签: pythonpdfimagemagicktiffwand

解决方案


看起来设置img.alpha_channel属性不会跨页面传播。

试试这个解决方法

from wand.api import library
from wand.image import Image

with Image(filename="5-page-pdf.pdf", resolution=200) as img:
    img.type = 'grayscale'
    img.compression = "lzw"
    # Manually iterate over all page, and turn off alpha channel.
    library.MagickResetIterator(img.wand)
    for idx in range(library.MagickGetNumberImages(img.wand)):
        library.MagickSetIteratorIndex(img.wand, idx)
        img.alpha_channel = 'off'
    img.save(filename="test.tiff")

推荐阅读