3

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
mr. cooper
  • 810
  • 1
  • 9
  • 15
  • 1
    How are you using the images? Inkscape is a powerful and free program that can manage vector graphics, you could try that. – David Mar 02 '15 at 23:59
  • 1
    Perhaps something similar to [this technique](http://stackoverflow.com/questions/12682621/how-to-adjust-boundingbox-of-an-eps-file/12684585#12684585) can be used. AIUI you just need to insert a `x y scale` command and recalculate the bounding box. – luser droog Mar 03 '15 at 01:36
  • 2
    Using Ghostscript. Surround the EPS with a scale as suggested above, so that the size is correct (eg 2 2 scale doubles n each direction). Set the media size for the scaled up output. The use the eps2write device to manufacture a new EPS at the given size. – KenS Mar 03 '15 at 02:36

1 Answers1

4

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:

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.

Kurt Pfeifle
  • 86,724
  • 23
  • 248
  • 345
  • Thank you for the detailed answer, @Kurt Pfeifle. I usually install binaries with Homebrew. Do you advise using Homebrew for installing Ghostscript? – mr. cooper Mar 30 '15 at 19:25
  • Sorry, I have no personal experience with Homebrew. On OSX I've decided for MacPorts. However, if Homebrew gives you a recent version of Ghostscript, and if your other Free Software packages are all Homebrew, I see no reason to avoid a homebrew-ed Ghostscript... – Kurt Pfeifle Mar 30 '15 at 19:54
  • by upvote do you mean accept it as the answer? I might have accepted it after your comment, but thanks again. Let me know if 'upvoting' is a separate action. – mr. cooper Mar 31 '15 at 22:04
  • There are two possible actions: ***(1)*** 'Upvote' -- click on `^`-shaped button right left to the top line of the answer. Can be executed by anyone who has at least 15 reputation points (including the original poster (OP) of the question). -- ***(2)*** 'Accept' -- can only be executed by the OP of a question, but only for exactly 1 answer (if multiple answers are available). -- OP can do both for same answer. Everybody (including OP) can upvote multiple answers for same question (if they deserve it), but cannot upvote his own answer. – Kurt Pfeifle Mar 31 '15 at 22:16