问题描述
我正在尝试将一个巨大的 PDF 文档解析为图像列表(每个图像都有一个 bmp 格式)。我使用 ghostscript 和 python 将 PDF 解析为 numpy 数组列表,但使用了非常无用的方法:
def get_imgs_gs(path_to_pdf):
cpu_number = os.cpu_count() # get number of cores
folderName = "bmp_imgs" # name of temporary folder to save images
Path(folderName).mkdir(parents=True,exist_ok=True) # create the folder
absPath = os.path.abspath(folderName) # get absolute path to the folder
args = [
'gs','-sDEVICE=bmpgray',f'-g{WIDTH}x{HEIGHT}',# f'-dNumRenderingThreads={cpu_number}','-r247x247','-dnopAUSE','-dBATCH',f'-sOutputFile="{absPath}/%04d.bmp"',path_to_pdf
]
ghostscript.Ghostscript(*args) # run ghostscript
content = os.listdir(absPath) # get the folder's content (list of images name)
content.sort() # sort names to iterate by true order
imgs = [None]*len(content) # read images
for i in range(len(content)):
imgs[i] = plt.imread(absPath + '/' + content[i])
shutil.rmtree(absPath) # remove images
return imgs
正如您从上面的代码中看到的,我保存了这些图像,然后将其删除。
那么,我怎样才能避免这一步。我尝试使用 gs 的 ANSI-c API,但没有找到解决方案。唯一从 std 中获取图像位图的机会。
有人可以帮我吗?顺便说一句,我想提高速度(-dNumRenderingThreads={cpu_number}),但对我没有帮助。也许有人可以帮助我。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)