问题
What I am trying to do is convert a ps which is just a image to a jpg using ghostcript here is what I started with
gswin32c.exe -sDEVICE=jpeg -sOutputFile="test.png" example.ps
however when I run this the jpg comes out with a lot of white space to the right and the bottom

Also the picture comes out huge is there any way to crop out the whitespace in ghostscript?
I tried this code
gswin32c.exe -sDEVICE=jpeg -dBATCH -dNOPAUSE -dSAFER -r72x72 -g200x900 -sOutputFile="test.jpg" example.ps
which decrease the width but the height is still huge. Any solutions?
回答1:
Firstly you need to set the 'correct' media size. There are several methods, including the -g switch you have already discovered.
The white space at the bottom is caused (probably) by the way the file has been made, PostScript co-ordinates start at bottom left and increase as you go up the page and right. Many computer systems start at top left and increase as you go down. In order to mimic this the document probably 'flips' the vertical (so that values increase as you go down the page) and then offsets the initial point so that it starts at the top left of the media it was designed for.
You can add PostScript to the command line using the -c .... -f switches, you will want to add some kind of vertical translation to move the imaged portion. Depending how the translation was accomplished in the original PostScript you may need to add either a positive or a negative offset. I would try 0 -100 translate and then see which way the image moves on the output.
Alternatively, convert the file to a PDF (using the pdfwrite device) and apply a CropBox.
回答2:
gswin32c.exe -sDEVICE=jpeg -dUseCropBox -sOutputFile="test.png" example.ps
use -dUseCropBox to remove the extra white space
回答3:
use pstoeps to find a tight bounding box, then pbmtools to convert to jpg, something like:
pstoeps file.ps | pstopnm - | pnmtojpg - > file.jpg
see http://netpbm.sourceforge.net/ if you dont have the later two.
i dont recall right now if pstoeps needs a switch to force calculation of a bounding box, but the man page should be pretty clear.
Edit: apolgies for posting without actually trying it..
The converter that is packed with ghostscript is ps2epsi , and it does not pipe to stdout, so you do:
ps2epsi file.ps
this creates file.epsi. Then run:
pstopnm -portrait -xborder 0 -yborder 0 file.epsi -stdout | pnmtojpeg > file.jpeg
Another approach which on second thought is better, (skipping the eps conversion) is this:
pstopnm -portrait file.ps -stdout | pnmcrop | pnmtojpeg > file.jpeg
来源:https://stackoverflow.com/questions/13812998/ghostscript-ps-to-jpg-output-without-white-space