Syntax for ghostscript in python
I have installed python-ghostscript on Linux. I can run gs from the command line and it will create a jpg开发者_开发知识库 from a pdf. Here is the code that works:
~$ gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg -sOutputFile=/home/user/output.jpg /home/user/downloads/test.pdf
I am trying to run that process in Python but I cannot get the syntax. I get no errors, but nothing happens. I've tried to read up on Popen/subprocess, but I'm not understanding why the gs process that I am calling doesn't run and create the file.
output = Popen(['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf'])
Your last two parameters are joined, so it can't work; it's effectively the same as running in your terminal gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=jpeg '-sOutputFile=/home/user/output.jpg /home/user/downloads/test.pdf'
, which you wouldn't expect to work.
Separate those last two and it should work:
output = Popen(['gs', '-dSAFER', '-dNOPAUSE', '-dBATCH', '-sDEVICE=jpeg', '-sOutputFile=/home/user/output2.jpg', '/home/user/downloads/test.pdf'])
To test what is going wrong, you could pipe the standard output like this:
import sys, subprocess
args = ['gs','-dSAFER','-dNOPAUSE','-dBATCH','-sDEVICE=jpeg','-sOutputFile=/home/user/output2.jpg /home/user/downloads/test.pdf']
output = Popen( args, stdout = sys.stdout, stderr = sys.stderr )
this worked (python):
os.system('gs -dSAFER -dNOPAUSE -dQUIET -dBATCH -sDEVICE=jpeg -sOUTPUTFILE=/home/user/output2.jpg /home/user/Downloads/test.pdf')
精彩评论