gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:10:02 26 4
gpt4 key购买 nike

我正在尝试将 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com