【问题标题】:python ghostscript not closing output filepython ghostscript没有关闭输出文件
【发布时间】:2017-11-10 22:13:43
【问题描述】:

我正在尝试将包含一页或多页的 PDF 文件转换为每页的图像。这很像the question found here。事实上,我正在尝试使用该帖子中来自@Idan Yacobi 的代码来完成此操作。他的代码如下所示:

import ghostscript

def pdf2jpeg(pdf_input_path, jpeg_output_path):
    args = ["pdf2jpeg", # actual value doesn't matter
            "-dNOPAUSE",
            "-sDEVICE=jpeg",
            "-r144",
            "-sOutputFile=" + jpeg_output_path,
            pdf_input_path]
    ghostscript.Ghostscript(*args)

当我运行代码时,我从 python 得到以下输出: ##### 238647312 c_void_p(238647312L)

当我查看应该在其中创建新 .jpg 图像的文件夹时,那里有一个具有新名称的文件。但是,当我尝试打开文件时,图像预览显示“Windows 照片查看器无法打开此图片,因为该图片正在另一个程序中编辑。”

似乎由于某种原因,Ghostscript 打开了文件并对其进行了写入,但完成后并没有关闭它。有什么办法可以强迫这种情况发生吗?或者,我是否错过了其他东西?

我已经尝试将上面的最后一行更改为下面的代码,以便在完成后显式关闭 ghostscript。

GS = ghostscript.Ghostscript(*args)
GS.exit()

【问题讨论】:

  • 如果GS = ghostscript.Ghostscript(*args) 抛出异常,您的代码实际上永远不会到达GS.exit()(除非您使用finally

标签: python pdf jpeg ghostscript


【解决方案1】:

我在图像文件保持打开状态时遇到了同样的问题,但是当我查看 ghostscript init.py 文件(在以下目录中找到:PythonDirectory\Lib\site-packages\ghostscript__init__ .py),exit 方法有一行注释。

gs.exit(self._instance) 行默认注释,但当您取消注释该行时,图像文件将被关闭。

def exit(self):
    global __instance__
    if self._initialized:
        print '#####', self._instance.value, __instance__
        if __instance__:
            gs.exit(self._instance) # uncomment this line
            self._instance = None
        self._initialized = False

【讨论】:

    【解决方案2】:

    我在批量处理大量 pdf 时遇到了同样的问题,我相信我已经将问题隔离为 Ghostscript 的 python 绑定问题,就像你说的那样,图像文件没有正确关闭。为了绕过这个,我不得不去使用一个 os 系统调用。所以给出你的例子,函数和调用将被替换为:

    os.system("gs -dNOPAUSE -sDEVICE=jpeg -r144 -sOutputFile=" + jpeg_output_path + ' ' + pdf_input_path)
    

    您可能需要将“gs”更改为“gswin32c”或“gswin64c”,具体取决于您的操作系统。这可能不是最优雅的解决方案,但它解决了我的问题。

    【讨论】:

      【解决方案3】:

      我的工作实际上只是安装图像打印机并让 Python 使用图像打印机打印 PDF,从而创建所需的 jpeg 图像。这是我使用的代码:

      import win32api
      def pdf_to_jpg(pdf_path):
          """
          Turn pdf into jpg image(s) using jpg printer
          :param pdf_path:  Path of the PDF file to be converted
          """
      
          # print pdf to jpg using jpg printer
          tempprinter = "ImagePrinter Pro"
          printer = '"%s"' % tempprinter
          win32api.ShellExecute(0, "printto", pdf_path, printer, ".", 0)
      

      【讨论】:

        【解决方案4】:

        在运行受密码保护的 PDF 时,我遇到了同样的问题 - ghostscript 会崩溃并且不会关闭 PDF,从而阻止我删除 PDF。

        Kishan 的解决方案已经适用于我,因此对我的问题没有帮助。

        我通过导入 GhostscriptError 并在 try/finally 块之前实例化一个空的 Ghostscript 来修复它,如下所示:

        from ghostscript import GhostscriptError
        from ghostscript import Ghostscript
        
        ...
        # in my decryptPDF function
        GS = Ghostscript()
        try:
            GS = Ghostscript(*args)
        finally:
            GS.exit()
        
        ...
        # in my function that runs decryptPDF function
        try:
            if PDFencrypted(append_file_path):
                decryptPDF(append_file_path)
        except GhostscriptError:
            remove(append_file_path)
            # more code to log and handle the skipped file
            ... 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-28
          • 1970-01-01
          • 2012-09-13
          • 2020-01-16
          相关资源
          最近更新 更多