admin管理员组

文章数量:1205175

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?

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?

Share Improve this question edited Jan 20 at 12:58 KenS 31.1k3 gold badges37 silver badges56 bronze badges asked Jan 20 at 12:22 Amy HAmy H 215 bronze badges 9
  • You should share the PS file(s) in error to allow us to test. – Gilles Quénot Commented Jan 20 at 12:53
  • 1 try just dbl-quoting both refereences to $i in the for loop cmd execution . Good luck. – shellter Commented Jan 20 at 14:28
  • 1 Amy H Sorry, no, I can help with Ghostscript and PostScript programming, but not shell scripting. shellter's comment above looks reasonable to me (so you'd need gs -dSAFER .... -sOutputFile="${i/_rss.ps/_rss.png}" "$i"). Presumably you could replace 'gs' with 'echo' so you could see what is actually being escaped by the shell, and see if it looks like what you expect, but again, I'm not an expert on this. – KenS Commented Jan 20 at 14:47
  • 1 How can your code generate string -dGraphicsAlphaBits=4 with a d? – Cyrus Commented Jan 20 at 22:30
  • 1 Just tried both commands (with and without -d before Graphics) and both work for me! Weirdly, my bash script started working earlier too, without me changing anything (I literally copied and pasted what I did before!) so no clue what is going on there, but at least it works now! Thank you everyone for your suggestions – Amy H Commented Jan 21 at 10:14
 |  Show 4 more comments

1 Answer 1

Reset to default 1

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!

本文标签: bashHow to convert multiple postscript files into PNGs using GhostscriptStack Overflow