0

我正在尝试将一个巨大的 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,但没有找到解决方案。唯一的机会从标准获取图像的位图。

有人可以帮助我吗?顺便说一句,我想提高速度(-dNumRenderingThreads={cpu_number}),但这对我没有帮助。可能有人可以帮助我。

4

0 回答 0