python - 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?
解决方案
看起来设置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")
推荐阅读
- python - 使用 splinter 如何单击此链接/按钮
- angular - mat-chip-list 中的 Angular Material ngFor - 如何防止换行?
- javascript - 在 HTML5 Canvas 游戏中更改精灵的黑色像素颜色
- python - 如何在不手动设置 FLASK_APP 的情况下通过烧瓶运行或自定义命令运行 Flask 应用程序
- django - 从父级保存相关的子模型字段 - Django
- laravel - Laravel 与表的关系
- php - Timber Twig:按类别查询自定义帖子类型并输出帖子的简码
- airflow - 如何使用 xcom 变量获取任务输出?
- python - 卷积神经网络 (CNN) 可以用数学公式表示吗?
- javascript - 从数组上传文件