Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'])
like image 747
Robin Avatar asked Sep 05 '26 06:09

Robin


1 Answers

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'])
like image 103
Chris Morgan Avatar answered Sep 07 '26 20:09

Chris Morgan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!