我的应用程序运行了几次,然后在每个 pdf 上出错。这是我收到的错误:
Exception TypeError: TypeError("object of type 'NoneType' has no len()",) in <bound method Image.__del__ of <wand.image.Image: (empty)>> ignored
Run Code Online (Sandbox Code Playgroud)
这是我使用的功能:
def read_pdf(file):
pre, ext = os.path.splitext(file)
filename = pre + '.png'
with Image(filename=file, resolution=200) as pdf:
amount_of_pages = len(pdf.sequence)
image = Image(
width=pdf.width,
height=pdf.height * amount_of_pages
)
for i in range(0, amount_of_pages):
image.composite(
pdf.sequence[i],
top=pdf.height * i,
left=0
)
image.compression_quality = 100
image.save(filename=filename)
logging.info('Opened and saved pdf to image: \'' + file + '\'.')
return filename
Run Code Online (Sandbox Code Playgroud)
此函数将正确地将 pdf 转换为图像,但两次或三次后,每次都会崩溃并抛出该异常。如果我重新启动 python 脚本,它会再次运行几次。
该错误是由系统资源不足引起的。Wand 调用 ImageMagick 库;反过来,将解码工作传递给 Ghostscript 委托。Ghostscript 非常稳定,但确实使用了大量资源,并且在并行运行时不愉快(我的意见)。
有什么帮助吗?
wand.image.Image.sequance. 报告了一些已知的内存泄漏问题。尽管许多已得到修复,但 PDF 任务似乎仍然存在问题。从发布的代码来看,您似乎只是用给定 PDF 的所有页面创建了一个高大的图像。我建议MagickAppendImages直接移植。
import ctypes
from wand.image import Image
from wand.api import library
# Map C-API to python
library.MagickAppendImages.argtypes = (ctypes.c_void_p, ctypes.c_bool)
library.MagickAppendImages.restype = ctypes.c_void_p
with Image(filename='source.pdf') as pdf:
# Reset image stack
library.MagickResetIterator(pdf.wand)
# Append all pages into one new image
new_ptr = library.MagickAppendImages(pdf.wand, True)
library.MagickWriteImage(new_ptr, b'output.png')
library.DestroyMagickWand(new_ptr)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1230 次 |
| 最近记录: |