I end up generating a lot of postscript plots in R and other programs. Unfortunately, a lot of not so technical people have trouble opening postscript files so I end up having to convert these images to other formats pretty often. A really handy program for converting eps files to png (or jpg although that’s not really an optimal format for plots) is ImageMagick (available for all OSs I believe). ImageMagick lets you quickly convert (and create thumbnails, make B&W,…) from the command line without having to open up Photoshop.
For example, to convert an image named myPlot.eps to png you just need to enter convert myPlot.eps myPlot.png
(convert is a program in the ImageMagick package) at the command prompt and you’ll get a png file in myPlot.png. If you want to adjust the resolution (the default resolution is 72 dpi) of the output image, you can add the -density
option (e.g. for 200 dpi convert -density 200 myPlot.eps myPlot.png
). Make sure you put the -density
part before the input image name.
Converting many files at once is where ImageMagick really shines. The mogrify
command is probably the quickest option. For example, to convert the files image01.eps, image02.eps and image03.eps to png, just use the command mogrify -format png image*.eps
. In one shot, it will create image01.png, image02.png and image03.png.
Unfortunately, recent version of Imagemagick seem to be treating eps to png conversions oddly (see below) so mogrify isn’t cutting it on my files. If you have similar trouble (and you’re on Unix or Mac or Cygwin), you can just use a bit of Bash combined with the convert
command to get around the problem like this:
Black Background Problem
As I mentioned above, I started having trouble converting from eps to png after upgrading to ImageMagick 6.4. The transparent/white backgrounds in my eps files were being converted to black backgrounds and making the figures unreadable. I guess it must be some change in how transparency is handled but I’m not totally sure what changed. It took me a bit of googling before I found the solution, so I’ll repost it here. Adding -flatten
to the command (e.g. convert myImage.eps -flatten myImage.png
) should change the background back to white. My mogrify
command doesn’t include the -flatten
option so convert
(like the example above) seems like the way to go.
Anne | 28-Sep-09 at 10:38 am | Permalink
that’s good to know! I was fighting with poor-resolution pngs, because I was giving -density option *after* eps filename…
many thanks for pointing that out, and all the best.
Mate Ory | 21-Apr-10 at 6:17 pm | Permalink
You might use make for this:
%.png: %.eps
convert -density 100 $< -flatten $@
nix | 12-Aug-10 at 3:49 pm | Permalink
Hello,
Just thought I would drop a line and mention that their may be a potential bug in your script.
When using `ls *.eps` the issues is that that the delimiter is a white space. So if you have a file like:
“file 1.vacation.eps”
The ls command will treat it as 2 separate files:
1) file
2) 1.vacation.eps
You can let the shell do the expansion and use *.eps in the for loop which would take care of the space. Just have to watch the quoting of the variable.
Couple of quick tips you can use is the:
1) read command in bash
2) load into an array
Both methods will preserve the white space; just in case you have them.
PS: Using parameter expansion seems to take care of that potential issue, IE: ${f%.*}. However, I have used it enough.
PSS: You might find something like this useful.
find $HOME/ -iname \*.eps -print0 | while read -d $” file
do
echo “$file”
done
nix | 12-Aug-10 at 3:53 pm | Permalink
ok.. the read command didn’t come out as expected.
it should be:
read -d DollarSign_SingQuote_BackSlash_0_SingleQuote
The 0 is a zero not a an upper case “o”.
prasanth | 20-Nov-10 at 12:19 pm | Permalink
Thank you very much for your kind information. I am now able to make good quality png from gnuplot
Damjan | 22-Feb-11 at 5:48 pm | Permalink
Has anyone got an Idea how to convert in windows? I tried with commands described before, but I cannot make it right. I would like batch convert *.eps into *.png in windows. please help
ScottS-M | 23-Feb-11 at 9:53 am | Permalink
@Damjan
I guess there’s other solutions but one way would be install Cygwin and install ImageMagick inside that. Also I haven’t really tried it but I believe there’s a Windows version of ImageMagick.
John Luciani | 30-Apr-11 at 2:05 pm | Permalink
Thanks for posting this hint (-flatten). I had exactly the same problem and now it’s fixed!
(* jcl *)
Max | 13-Jul-11 at 7:36 pm | Permalink
Great!
subhash singha | 17-Nov-11 at 2:17 am | Permalink
Great ….!!! I do not have this info that I could convert in mac by just a single command .. makes my life so simple ….
Thanks for the info …
Gab | 12-Jan-12 at 10:28 am | Permalink
Never parse ‘ls’ !
cf http://mywiki.wooledge.org/ParsingLs
Kai | 01-Mar-12 at 11:10 am | Permalink
If you want your PNG to have a transparent background instead of a white one, you can use this command: “convert -colorspace rgb foo.eps foo.png”.
John | 14-May-12 at 3:54 am | Permalink
To get the transparancy from the EPS, this works for me…
convert -alpha on -colorspace transparent input.eps output.png
You can add -geometry wxh to give the ouput a size.
jlb | 07-Sep-12 at 4:16 pm | Permalink
Just a quick thanks for this post. I was searching for how to do this, and came across this page. After realizing that I had to use the grave mark, not the apostrophe, all was good. Successfully used it to convert a batch of .pdf files to .png. Thanks!
Mehdi | 17-Mar-13 at 5:34 pm | Permalink
Thanks a lot!
It really helped me!
what is “-alpha on”?
linuxien | 30-Jun-14 at 8:37 am | Permalink
WARNING: converting your (vector) EPS images into (bitmap) PNG is a bad idea!
A better choice is to use the PDF format, which keep the mathematical definition of the content of your EPS.
Convert your EPS files via the ‘epstopdf’ command, since everybody can read PDF files:
$ epstopdf my.eps
it makes a really good job.
Pranav | 04-Sep-14 at 12:34 pm | Permalink
Thank you for the useful script!!