【发布时间】:2019-06-06 06:08:22
【问题描述】:
我正在使用 Python PDF2Image 库运行一个简单的 PDF 到图像的转换。我当然可以理解,这个库正在跨越最大内存阈值来达到这个错误。但是,the PDF 是 6.6 MB(大约),那么为什么会占用 GBs 的内存来引发内存错误呢?
Python 3.7.0 (v3.7.0:1bf9cc5093, Jun 27 2018, 04:06:47) [MSC v.1914 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pdf2image import convert_from_path
>>> pages = convert_from_path(r'C:\Users\aakashba598\Documents\pwc-annual-report-2017-2018.pdf', 200)
Exception in thread Thread-3:
Traceback (most recent call last):
File "C:\Users\aakashba598\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 917, in _bootstrap_inner
self.run()
File "C:\Users\aakashba598\AppData\Local\Programs\Python\Python37-32\lib\threading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\aakashba598\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1215, in _readerthread
buffer.append(fh.read())
MemoryError
另外,有什么可能的解决方案?
更新:当我从 convert_from_path 函数中减少 dpi 参数时,它就像一个魅力。但是制作的图片质量很差(原因很明显)。有没有办法解决这个问题?就像每次批量创建图像和清除内存一样。如果有办法,该怎么做?
【问题讨论】:
-
一定要用Python,还是可以用imagemagick?
-
我想通过编码来实现,Python是一种非常方便的编程语言。
标签: python python-3.x out-of-memory data-conversion