Python Wand 将 PDF 转换为 PNG 禁用透明(alpha_channel)

标签 python imagemagick wand

我正在尝试将 PDF 转换为 PNG - 这一切正常,但是,即使我相信我已禁用它,输出图像仍然是透明的:

with Image(filename='sample.pdf', resolution=300) as img:
    img.background_color = Color("white")
    img.alpha_channel = False
    img.save(filename='image.png')

上面生成的图像是透明的,我也尝试了下面的:

with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
    img.alpha_channel = False
    img.save(filename='image.png')

产生此错误:

Traceback (most recent call last):
  File "file_convert.py", line 20, in <module>
    with Image(filename='sample.pdf', resolution=300, background=Color('white')) as img:
  File "/Users/Frank/.virtualenvs/wand/lib/python2.7/site-packages/wand/image.py", line 1943, in __init__
    raise TypeError("blank image parameters can't be used with image "
TypeError: blank image parameters can't be used with image opening parameters

最佳答案

我还有一些 PDF 可以转换为 PNG。这对我有用,而且看起来比合成图像更简单,如上所示。:

from wand.image import Image
from wand.color import Color

all_pages = Image(blob=self.pdf)        # PDF will have several pages.
single_image = all_pages.sequence[0]    # Just work on first page
with Image(single_image) as i:
    i.format = 'png'
    i.background_color = Color('white') # Set white background.
    i.alpha_channel = 'remove'          # Remove transparency and replace with bg.

引用:wand.image

关于Python Wand 将 PDF 转换为 PNG 禁用透明(alpha_channel),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27826854/

相关文章:

python - 切换到 selenium webdriver 中的 web 对话框 : Python

python - 在 MongoAlchemy 中运行 Author.save() 时出错

PHP 未捕获异常 'ImagickException',消息为 'FailedToExecuteCommand'

python - 如何在 Python 中使用 imagemagick?

python - 将使用 wand 生成的图像保存到 django ImageField

python - 如何为数据框中的特定单元格设置值

python - 如何使CV2在python3.6上运行?

php - shell whoami 不等于 php shell_exec whoami?

python - 如何提取彩色边框内的图像区域?

python - 将多页 PDF 转换为 TIFF 不适用于 Python 库 Wand