Question

I have a project that i'm working on using Python and GhostScript. Currently I have a PDF file with several hundred pages, I need to take this PDF and seperate the pages into TIF Files, I have the code up and running correctly, however as this program can take some time to complete, I would like to have an output to the user showing how far in the process it is.

gs_appdir = 'C:\\Program Files\\gs\\gs9.10\\bin\\'
os.chdir(gs_appdir)
args = "-o \""+wkgdir+"Images\\Image_%03d.tif\" -sDEVICE=tiffg4 -q -NODISPLAY -r200x200 -s \""+inPDFFile+"\""
os.system("gswin64c.exe %s" % args)

When I take the -q parameter out of the arguments it will show me, however it will go line by line by line:

Page 1 of xxxx
Page 2 of xxxx
Page 3 of xxxx
...et c.

I would like to do something similar to this:

print "\r[ Preparing Images ] %s%%" % ((img+1)*100/imgcount),

Where it will print the percentage on the same line, however i'm not quite sure how to accomplish this using GhostScript. Any advice would be greatly appriciated.

Was it helpful?

Solution

Hmm I think basically you can't do this. With a chunk of PostScript programming you could get a message of the form you want passed out, but it will be on stdout, so if you run with -q then it will be suppressed.

You would have to start by using the4 Ghostscript extensions to open the PDF file, begin the PDF interpretation, count the number of pages, then enter a loop rendering each page and emitting a message. Followed by closing everything down. Normally the convenience routines in Ghostscript take care of all this for you, but they won't emit the kind of message you want.

Even if it was all emitted on the same line, I don't think you would like it. You seem to be assuming that the later lines will simply erase (write on top of) the previous lines, and that isn't the case I don't think, though it does depend on the command shell (and therefore the operating system) you are using. I note you seem to be using Windows, you might be able to get this effect with a \r I'm not sure.

Fundamentally, if you want to do this you will have to write a new application which uses the API rather than executing Ghostscript as an external process, and process the stdout callback yourself.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top