1

I want to run this command from python:
gs.exe -sDEVICE=jpeg -dTextAlphaBits=4 -r300 -o a.jpg a.pdf
Using ghostscript, to convert pdf to series of images. How do I use the RAM for the input and output files? Is there something like StringIO that gives you a file path?

I noticed there's a python ghostscript library, but it does not seem to give much more over the command line

user3599803
  • 6,435
  • 17
  • 69
  • 130
  • Why don't you simply use `os.system()` if you want to run a command from Python? – John Coleman Aug 25 '16 at 12:03
  • I can do that, but I want the input and ouput file to be stored in python, not on disk. I'm using in a django web app – user3599803 Aug 25 '16 at 12:06
  • Maybe this will help: http://stackoverflow.com/q/18550127/4996248 . One of the answers even discusses `StringIO` in Python. – John Coleman Aug 25 '16 at 12:13
  • I've already tried it, it does not work. I can't give `SpooledTemporaryFile` to the ghostscript command line, unless it exceeds max_size, and then the file object will have 'name' attribute which is a path on the disk, but then it will not be in memory, which miss my point.. – user3599803 Aug 25 '16 at 15:03

1 Answers1

1

You can't use RAM for the input and output file using the Ghostscript demo code, it doesn't support it. You can pipe input from stdin and out to stdout but that's it for the standard code.

You can use the Ghostscript API to feed data from any source, and you can write your own device (or co-opt the display device) to have the page buffer (which is what the input is rendered to) made available elsewhere. Provided you have enough memory to hold the entire page of course.

Doing that will require you to write code to interface with the Ghostscript shared object or DLL of course. Possibly the Python library does this, I wouldn't know not being a Python developer.

I suspect that the pointer from John Coleman is sufficient for your needs though.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • I didn't understand a few things, what is "standard code" in your answer? And maybe that is a stupid question, but how can I pipe a something to ghostscript from python (feed as you said)? can I pass actual bytestring of the file? – user3599803 Aug 25 '16 at 15:11
  • I should add that I've tried ImageMagick library for doing the same thing (PDF-> images), worked. It uses ghostscript behind the scenes, and I did everything in python using `wand` library. So I guess it is somehow possible to get ghostscript results into memory. The reason why I want to try without ImageMagick is that it somehow slow in loading the file. So I want to try with ghostscript alone – user3599803 Aug 25 '16 at 15:13
  • By "standard code" I mean the Ghostscript binary that we ship. Note that this is basically example code, albeit very large and complex example code, there is an API to allow developers to build applications, you don't **have** to use what we supply! When I mentioned pipes I was discussing shell operations, I have no clue how you would do that in Python. As regards passing data to Ghostscript, you can do so, using the API, not using the example code as supplied. I would bet that ImageMagick uses files behind the scenes, not memory. Of course, that would be slower..... – KenS Aug 26 '16 at 07:09