2

I currently have a Python script that runs a Ghostscript command to convert a PDF's first page to a PNG. However, right now the image just goes into one of my directories. Is it possible to have the Ghostscript output binary data so I can save that save to a database?

args = [
    "pdf2img", # actual value doesn't matter
    "-sOutputFile=testing.png",
    "-dNOPAUSE",
    "-dBATCH",
    "-sDEVICE=png256",
    "-sPAPERSIZE=a1",
    "-dPDFFitPage=true",
    "-dFirstPage=1",
    "-dLastPage=1",
    "testPDF.pdf"
    ]

ghostscript.Ghostscript(*args)
rachiebytes
  • 542
  • 1
  • 5
  • 20
  • Mongo db stores textual information, so the image would have to be a binary string. Take a look here http://stackoverflow.com/questions/4796914/store-images-in-a-mongo-database Also, you can take a look at the PIL pillow module (http://pillow.readthedocs.org/en/3.0.x/). It works great with images. You can convert your image to a binary string, store that on mongo, retrieve the data later and recreate the image with the PIL mod – reticentroot Oct 08 '15 at 14:46
  • Do you want to know how to get Ghostscript to give you the binary data instead of writing it to disk right away or do you need to know how to store a big blob of binary data in MongoDB? – Philipp Oct 08 '15 at 14:47
  • I'd prefer Ghostscript give me the binary data @Philipp – rachiebytes Oct 08 '15 at 14:51
  • you can also use MongoDB GridFS, for storing files: http://docs.mongodb.org/manual/core/gridfs/ – sergiuz Oct 08 '15 at 14:53
  • @RachelAnnJohnson Then I would recommend you to remove the references to mongodb - as you see the question attracts more MongoDB-centric attention than Ghostscript-centric attention. – Philipp Oct 08 '15 at 14:55
  • @Philipp Unless of course the intent is to store the binary data "in MongoDB". Which actually seems to be be point of the question. Perfectly reasonable ask, and despite "misinformation" already given, MongoDB does not care the least about the data type and the driver will generally work it out. GridFS is only an option needed when the total size is expected to exceed 16MB. Which is quite a lot really. – Blakes Seven Oct 08 '15 at 15:08

1 Answers1

1

You can have Ghostscript pipe the output to stdout by saying -sOutputFile=%stdout. Other than that I can't think what you mean by 'give you the binary data'.

KenS
  • 30,202
  • 3
  • 34
  • 51
  • Thank you! Any idea on how to store that output in a variable in Python? – rachiebytes Oct 08 '15 at 16:39
  • I'd be very surprised if you can assign it to a **variable**, the output of rendering can be multiple megabytes, possibly even gigabytes, of data. But I know nothing about python. – KenS Oct 09 '15 at 07:33