将多页PDF转换为TIFF不适用于Python库魔杖

问题描述 投票:0回答:1

给出了简短的5页PDF文件(位于底部),以及以下python代码将其转换为多页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")

导致TIFF文件的页面为2-4,在深灰色(或透明)的背景下显示为黑色文本。其他图像处理库无法打开或渲染文件。

使用Wand使用的imagemagick转换相同的PDF效果很好

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

这会产生一个与其他影像库一起使用的文件,并且在TIFF查看器中看起来正确。

我尝试删除Alpha通道,我尝试将背景色设置为'白色',但还有其他一些事情,无济于事。从魔杖出来的TIFF总是乱码。如果它在ImageMagick中可行,那么在Wand中也可行,对不对?我缺少什么参数或设置?

Original PDF

Wand Produced TIFF

python pdf imagemagick tiff wand
1个回答
0
投票

看起来像设置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")
© www.soinside.com 2019 - 2024. All rights reserved.