Convert eps to png using Imagemagick

Viewed 1385

I realize there are similar questions already posted, but none of them seem to address my issue, and I'm not getting anywhere with the man page for Imagemagick's convert. I have a simple EPS image that I want to convert to PNG. Just for the sake of completeness, here are the first few lines of the EPS file— I'm calling it myfile.eps for this example.

%!PS-Adobe-3.0 EPSF-3.0
%%BoundingBox: 5 5 305 305
%%BeginProlog
%%EndProlog

% Particle 14
160.631 121.588 6.99204 0 360 arc closepath
0 0 1 setrgbcolor fill

And here is the image itself: Encapsulated Postscript Image Example

The image consists of a collection of blue circles. I want to convert the image from the command line to PNG with the same image dimension. When I use this command:

convert myfile.eps myfile.png

The image winds up in the lower-left corner of a PNG file that is 612x792 pixels, with a ton of white space everywhere else.

Example PNG converted from EPS

Next, I tried -crop, -resize, and various combinations of those, but this typically gives me a LOT of individual PNG files that display small portions of that overall 612x792 image. I'm pretty sure that -crop should work but I evidently have not figured out how to correctly specify the crop dimensions.

What is the trick for retaining the image's bounding box when it converts to PNG?

1 Answers

Trimming the image (which removes whitespace around the edges) seems to do what you want quite simply in this case:

magick image.eps -trim result.jpg

enter image description here

If that is trimmed too closely for your liking, you can add a 10 pixel wide border back on to give it room to breathe like this:

magick image.eps -trim -bordercolor white -border 10 result.jpg

If you find you need to use -crop for other cases, you most likely have a problem with getting lots of output images because you didn't specify an offset. So, if for example, you want to crop 150x120 out of the southwest corner, set the gravity and use an offset of +0+0 like this:

magick image.eps -gravity southwest -crop 150x120+0+0 result.jpg

enter image description here


I see also that exiftool reports a bounding box as follows:

exiftool image.eps
ExifTool Version Number         : 12.00
File Name                       : image.eps
Directory                       : .
File Size                       : 25 kB
File Modification Date/Time     : 2021:05:04 07:58:44+01:00
File Access Date/Time           : 2021:05:04 08:00:11+01:00
File Inode Change Date/Time     : 2021:05:04 07:58:44+01:00
File Permissions                : rw-r--r--
File Type                       : EPS
File Type Extension             : eps
MIME Type                       : application/postscript
Bounding Box                    : 5 5 305 305                 <--- HERE
Image Height                    : 300
Image Width                     : 300
Image Size                      : 300x300
Megapixels                      : 0.090

And that this gives a reasonable result too:

magick image.eps -gravity southwest -crop 305x305+0+0 result.jpg

I am not sure whether any of these approaches will be reliable for all your images, so you may need to experiment and report back any problematic files. Or there may be a better approach altogether.


You could extract the BoundingBox from the EPS with awk like this:

bbox=$(awk '/%%BoundingBox:/ {printf("%dx%d+%d+%d",$4,$5,$2,$3); exit }' image.eps)

And then use it like this:

magick image.eps -gravity southwest -crop "$bbox"  result.jpg
Related