问题
I use Matlab to generate EPS (Encapsulated PostScript) files. I would like to resize these files so they render at a different size than they are generated. I would like to specify the resized dimensions in inches or centimeters, ideally. Is there an option for doing this using free software or a command line utility?
I am looking for something compatible with OSX, but would settle for Windows. I am aware of EPSViewer.org but it only works in pixels and constrains the dimensions to be proportional.
回答1:
You can use Ghostscript (a Free software + command line utility as you want).
It is available for Linux, Windows and Mac OS X.
Make sure to have the most recent Ghostscript version (v9.15 or later) which includes the eps2write
device. (Check with gs -h | grep --color=auto -E '(epswrite|eps2write)'
which one you have.)
Pre-compiled binaries are available for Linux and Windows here:
- http://downloads.ghostscript.com/public/binaries/
Then, to scale your input.eps
to 50% while preserving the aspect ratio, you can run this command:
gs \
-o scaled.eps \
-sDEVICE=eps2write \
-c "<</Install {0.5 0.5 scale}>> setpagedevice" \
-f input.eps
(For Windows, replace gs
by gswin32c.exe
or by gswin64c.exe
.)
The -c "..."
command line parameter allows you to add PostScript code snippets which will be executed when Ghostscript processes the input file. In the above example it scales the input page.
Here is proof that the scaling worked. By running Ghostscript with -sDEVICE=bbox
you get the computed BoundingBoxes of the EPS files, which define the rectangles where actual pixels will be painted:
gs -q -o -sDEVICE=bbox input.eps
%%BoundingBox: 41 19 585 831
%%HiResBoundingBox: 41.990975 19.997999 584.027982 830.009014
gs -q -o -sDEVICE=bbox scaled.eps
%%BoundingBox: 20 9 293 416
%%HiResBoundingBox: 20.991023 9.990000 292.013991 415.008972
You can also use it to scale in a "liquid" way, without preserving aspect ratios by giving to different parameters to the scale
operator. Unfortunately, I cannot spare you from calculating the respective numbers by yourself. (We could do it in PostScript too, and give a command line where you would insert your required dimensions as inch, but that would make the command line too long. Ask if you want it anyway, then I update this answer accordingly...)
I used this to scale an EPS from A4 portrait to A4 landscape:
gs \
-o A4-landscape.eps \
-sDEVICE=eps2write \
-c "<</Install {1.4151 0.7066 scale}>> setpagedevice" \
-f A4-portrait.eps
Here are screenshots of the input and output files I used:
A4-portrait.eps
(input) is right.A4-landscape.eps
(output) is left.
For OS X, you may be tempted to get Ghostscript through MacPorts. Once you have the base MacPorts installation, it would as easy as:
sudo port -pf install ghostscript
to install it (including all dependencies).
However, be warned: MacPorts currently gives you only Ghostscript v9.10. That version does not yet include the eps2write
device. If you use GS 9.10, you can only use epswrite
-- but this has the disadvantage to only emit PostScript Level 1, which may heavily pixelate your resulting scaled EPS.
来源:https://stackoverflow.com/questions/28821395/how-to-resize-an-eps-file-with-free-software-or-command-line-utility