python ghostscript不关闭输出文件

问题描述:

我正在尝试将一页或多页的PDF文件转换为每一页的图像.这非常类似于发现的问题这里.实际上,我正在尝试使用该帖子中@Idan Yacobi的代码来完成此任务.他的代码如下:

I'm trying to turn PDF files with one or many pages into images for each page. This is very much like the question found here. In fact, I'm trying to use the code from @Idan Yacobi in that post to accomplish this. His code looks like this:

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)

When I run the code I get the following output from python: ##### 238647312 c_void_p(238647312L)

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

When I look at the folder where the new .jpg image is supposed to be created, there is a file there with the new name. However, when I attempt to open the file, the image preview says "Windows Photo Viewer can't open this picture because the picture is being edited in another program."

由于某种原因,Ghostscript似乎打开了文件并写入了文件,但在完成后没有关闭文件.我有什么办法可以强迫这种情况发生?还是我想念其他东西?

It seems that for some reason Ghostscript opened the file and wrote to it, but didn't close it after it was done. Is there any way I can force that to happen? Or, am I missing something else?

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

I already tried changing the last line above to the code below to explicitly close ghostscript after it was done.

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

我遇到了同样的问题,即图像文件一直处于打开状态,但是当我查看ghostscript init .py文件时(发现在以下目录中:PythonDirectory \ Lib \ site-packages \ ghostscript__init __.py),exit方法的一行带有注释.

I was having the same problem where the image files were kept open but when I looked into the ghostscript init.py file (found in the following directory: PythonDirectory\Lib\site-packages\ghostscript__init__.py), the exit method has a line commented.

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

The gs.exit(self._instance) line is commented by default but when you uncomment the line, the image files are being closed.

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