I will be processing hundreds of postscript files and converting them into PNG files with Ghostscript, so I am trying to find a way to batch process them in one command.
When doing this manually I simply do
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -GraphicsAlphaBits=4 -sOutputFile=output.png input.ps
And this works fine. But it's not practical to be doing this for hundreds of files, so I am trying to find a way to input all files within a folder, ending in _rss.ps
, into Ghostscript to convert to PNGs.
When I use
#!/bin/bash
for i in *_rss.ps ; do echo "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -GraphicsAlphaBits=4 -sOutputFile=${i/_rss.ps/_rss.png} $i" ; done
It gives the exact result I need
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=32532q_rss.png 32532q_rss.ps
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=4323_rss.png 4323_rss.ps
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=54735_rss.png 54735_rss.ps
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=JF290489_rss.png JF290489_rss.ps
but when I remove echo
and the quotation marks it gives the following error
GPL Ghostscript 9.25 (2018-09-13)
Copyright (C) 2018 Artifex Software, Inc. All rights reserved.
This software comes with NO WARRANTY: see the file PUBLIC for details.
Error: /syntaxerror in (binary token, type=137)
Operand stack:
Execution stack:
%interp_exit .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push --nostringval-- --nostringval-- --nostringval-- false 1 %stopped_push 2047 1 3 %oparray_pop 2046 1 3 %oparray_pop 2026 1 3 %oparray_pop 1884 1 3 %oparray_pop --nostringval-- %errorexec_pop .runexec2 --nostringval-- --nostringval-- --nostringval-- 2 %stopped_push
Dictionary stack:
--dict:953/1684(ro)(G)-- --dict:0/20(G)-- --dict:77/200(L)--
Current allocation mode is local
GPL Ghostscript 9.25: Unrecoverable error, exit code 1
Can anyone advise me on what I am doing wrong, or an alternative way to do this?
Thanks for all the suggestions, I really appreciate it.
The command I posted does now work even though nothing has been changed - I literally copied and pasted it to test and it worked that time, and still does! Perhaps because I made sure it used Ghostscript 10.04.0
rather than an older version, which was pointed out to me, but as far as I can tell they don't differ in these commands.
Some people pointed out the -d
before GraphicsAlphaBits=4
- I ran commands both with and without this and both work
If anyone is interested, the working script is:
#!/bin/bash
for i in *_rss.ps ; do gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=${i/_rss.ps/_rss.png} $i ; done
And gives output to stdout:
GPL Ghostscript 10.04.0 (2024-09-18)
Copyright (C) 2024 Artifex Software, Inc. All rights reserved.
This software is supplied under the GNU AGPLv3 and comes with NO WARRANTY:
see the file COPYING for details.
Loading NimbusSans-Regular font from %rom%Resource/Font/NimbusSans-Regular... 3502688 2144812 2783288 1486048 2 done.
As well as producing the PNG files.
To test:
#!/bin/bash
for i in *_rss.ps ; do echo "gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=${i/_rss.ps/_rss.png} $i" ; done
and this gives a series of:
gs -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r900 -dGraphicsAlphaBits=4 -sOutputFile=prefix_rss.png prefix_rss.ps
Thanks everyone!