我的目标是从PDF文件中获得一个具有透明背景的PNG文件。
这个
convert
工具可以完成这项工作:
$ convert test.pdf test.png
$ file test.png
test.png: PNG image data, 595 x 842, 8-bit gray+alpha, non-interlaced
但我想在python中以编程方式完成它,而不依赖于
转换
或任何其他工具。我想出了
pdf2image
但我不知道如何获得透明度。以下是我的尝试:
import pdf2image
with open("test.pdf", "rb") as fd:
pdf = pdf2image.convert_from_bytes(fd.read(), transparent=True)
pdf[0].save("test.png")
不幸的是,我失去了透明度:
$ python test.py
$ file test.png
test.png: PNG image data, 1654 x 2339, 8-bit/color RGB, non-interlaced
是否有任何方法可以在不依赖外部工具的情况下使用
pdf2图像
还是其他包裹?