Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python重影脚本未关闭输出文件_Python_Pdf_Jpeg_Ghostscript - Fatal编程技术网

python重影脚本未关闭输出文件

python重影脚本未关闭输出文件,python,pdf,jpeg,ghostscript,Python,Pdf,Jpeg,Ghostscript,我正在尝试将包含一个或多个页面的PDF文件转换为每个页面的图像。这很像。事实上,我正试图用那篇文章中@Idan Yacobi的代码来实现这一点。他的代码如下所示: import ghostscript def pdf2jpeg(pdf_input_path, jpeg_output_path): args = ["pdf2jpeg", # actual value doesn't matter "-dNOPAUSE", "-sDEVICE

我正在尝试将包含一个或多个页面的PDF文件转换为每个页面的图像。这很像。事实上,我正试图用那篇文章中@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获得以下输出:
(238647312L)

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

似乎出于某种原因,Ghostscript打开了文件并写入其中,但在完成后没有关闭它。我有没有办法强迫你这么做?或者,我错过了什么

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

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

我在批处理大量PDF时遇到了同样的问题,我相信我已经将问题隔离为Ghostscript的python绑定问题,正如您所说,映像文件没有正确关闭。为了绕过这个问题,我不得不使用操作系统调用。因此,根据您的示例,函数和调用将替换为:

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

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

我的工作实际上只是安装一台图像打印机,让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)

在图像文件保持打开的情况下,我遇到了同样的问题,但当我查看ghostscriptinit.py文件(在以下目录中找到:PythonDirectory\Lib\site packages\ghostscript\uuuu init\uuuuuu.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

当我遇到密码保护的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
    ... 

如果
GS=ghostscript.ghostscript(*args)
抛出一个异常,那么您的代码实际上永远不会到达
GS.exit()
(除非您最后使用