Convert PNG to EPS

If you are a LaTex user like me you will instantly recognize what I’m getting at here. LaTex does not work well with most of the popular image formats such as JPG, BMP, PNG or GIF. It does like encapsulated postscript images though (EPS). So if you want nice figures in your papers you can either use tools that can generate eps files, or convert.

I usually just go straight to EPS – most of Linux based graphing tools have support for either than or straight postscript which is also perfectly fine for LaTex. Between OpenOffice.org, Gnuplot, and the charting tools in KOffice I rarely need to convert anything. But the other day I was doing a batch of screenshots of an application and the default output format was PNG.

How do you convert from PNG to EPS? There are lots of ways, but since I was on ubuntu, I opted for using sam2p. It’s a small application that converts raster images into bunch of different formats. You probably won’t find it in a default installation so just do:

apt-get install sam2p

Note that you need to have universe enabled in your apt sources. Once you install it, the rest is easy:

sam2p Screenshot-1.png EPS: Screenshot-1.eps

First argument is the source image, second is the desired format followed by a colon, and the third is the desired name of the output file. Easy as pie. The only slight disadvantage of doing this is that you pay a considerable size penalty for this conversion:

Screenshot-1.png: 6.4KB
Screenhsot-1.eps: 20.0 KB

But that’s to be expected – after all we are goinf from tight binary image format to a textual postscript monstrosity. Note that you can cat the EPS file you will see nice coded segments composed of only ASCII characters. If you try to do that to a PNG file, you will see garbled binary mess and hear the system bell sound off once or twice (encountering the ASCII code for system bell in the output).

[tags]eps, png, png to eps, latex, sam2p, ubuntu, screenshots[/tags]

This entry was posted in Uncategorized. Bookmark the permalink.



35 Responses to Convert PNG to EPS

  1. Sergejs LATVIA Mozilla Firefox Linux says:

    Thank you very much :)
    I was searching for converting tool between PNG to EPS, you post helped me alot.

    Reply  |  Quote
  2. Luke UNITED STATES Mozilla Firefox Windows says:

    Glad I could help :mrgreen:

    Reply  |  Quote
  3. Giordano ITALY Mozilla Firefox Linux says:

    Thank you very much for the advice! I was lokking for such a tool :)

    Reply  |  Quote
  4. Giordano ITALY Mozilla Firefox Linux says:

    Thank you very much for the advice! I was looking for such a tool :)

    Reply  |  Quote
  5. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Awesome. :)

    Reply  |  Quote
  6. Chris UNITED KINGDOM Mozilla Firefox Linux says:

    Two things that might help: ImageMagick has a ‘convert’ command that will translate between any two of over 100 different formats. That includes PNG, EPS, etc. I use this in a Makefile to convert any unsupported formats to PDF ready for inclusion in latex.

    Secondly, as you noticed the conversion process to EPS results in large files. If you use pdflatex you get PDF output, including automatically generated links in table of contents, citations etc., with the ‘hyperref’ package, and you can include native PNG and JPG without conversion to EPS. Native PDF can also be included which is supported as an export from many applications, and there’s eps2pdf and ps2pdf for all the older apps. It’s great.

    Reply  |  Quote
  7. Luke UNITED STATES Mozilla Firefox Ubuntu Linux says:

    Thanks for the tip. I think I was specifically using something that didn’t work nice with pdflatex – but yeah, I think this would work. :)

    Reply  |  Quote
  8. Brett UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    Here is a little bash script I used to convert a folder full of png files into eps for latex. It was modified from here

    #!/bin/bash
    
    if [ ! -d ./eps_images ]; then mkdir ./eps_images; fi;
    
    # creates the eps image
    for f in *.png;
    do
        echo "Processing $f"
        sam2p $f EPS: ./eps_images/${f/.png}.eps
    done
    
    Reply  |  Quote
  9. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Very nice! Thanks for posting it!

    Reply  |  Quote
  10. Mats Rauhala FINLAND Mozilla Firefox Linux says:

    Do notice however that ‘convert’ utility creates absurdly large images.
    3992 -rw-r–r– 1 masse masse 4079736 2008-05-08 12:07 0798116.convert.eps
    2496 -rw-r–r– 1 masse masse 2550945 2008-05-08 12:08 0798116.gimp.eps
    372 -rw-r–r– 1 masse masse 375691 2008-05-05 17:17 0798116.jpg
    See the difference?

    As a sidenote I tried to bzip multiple eps files, and got around 98% packing ratio.

    Reply  |  Quote
  11. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    Yeah, that’s true. That’s the major downside of this method.

    Reply  |  Quote
  12. Naga Hyde UNITED STATES Internet Explorer Windows says:

    I suspect you forgot to compress the eps with convert. Try either

    convert Screenshot-1.png eps2:Screenshot-1.eps

    or use eps3 for tighter compression. Without these it is indeed huge.

    Reply  |  Quote
  13. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    Ah, thanks for the tip!

    Reply  |  Quote
  14. goodmami UNITED STATES Mozilla Firefox Linux says:

    I tried this on Ubuntu Hardy 8.04, and I got an error that it couldn’t find some command (pngtopnm). I did
    apt-get install netpbm
    and that fixed it.

    Also, I tried both convert and sam2p, and here’s the file sizes (source image is here)

    -rw-r--r-- 1 goodmami goodmami 356K 2008-07-20 19:34 convert_eps2.eps
    -rw-r--r-- 1 goodmami goodmami 32K 2008-07-20 19:34 convert_eps3.eps
    -rw-r--r-- 1 goodmami goodmami 5.0M 2008-07-20 19:33 convert_eps.eps
    -rw-r--r-- 1 goodmami goodmami 30K 2008-07-20 17:40 italophone_map.png
    -rw-r--r-- 1 goodmami goodmami 23K 2008-07-20 19:36 sam2p_eps2.eps
    -rw-r--r-- 1 goodmami goodmami 23K 2008-07-20 19:29 sam2p_eps.eps

    note that convert is horrible. The file sizes are generally much larger than the original (with the exception of eps3). eps2 did not even fully render the image. sam2p, however, had file sizes even smaller than the original (note that the image is very simple… not a photograph or anything). sam2p allowed eps and eps2 commands, but there was no difference.

    Reply  |  Quote
  15. Swaprava INDIA Mozilla Firefox Windows says:

    I know Linux users don’t like Windows, but since I’m using it and still want to convert png to ps, how should I go about it?

    Reply  |  Quote
  16. Luke Maciak UNITED STATES Mozilla Firefox Ubuntu Linux Terminalist says:

    @Swaprava: I think that OpenOffice.org has a functionality to export to EPS. I used it in the past to create simple drawings and flowcharts for some of my papers. I think you can just insert your image into a OOo Writer, fiddle around with margins and save the page as EPS.

    Either that or try Gimp (the open source Photoshop alternative) – it works pretty well on Windows. Not the most intuitive tool, but it gets to job done.

    Reply  |  Quote
  17. Chris UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    Swaprava: imagemagick is available on Windows too. link

    Reply  |  Quote
  18. Adam UNITED KINGDOM Mozilla Firefox Ubuntu Linux says:

    Another note, sometimes transparency in a PNG causes problems. Using GIMP to get rid of it before converting to EPS works well, and it can also be used to crop the original image as well. Try using eps2 or eps3 as well as plain eps, for me some types worked better on some files than others so a bit of trial-and-error was needed.

    Reply  |  Quote
  19. pavel UNITED STATES Mozilla Firefox Linux says:

    Well done, thanks for pointing this out.

    Reply  |  Quote
  20. cesar CANADA Mozilla Firefox Ubuntu Linux says:

    Great post… Fight for a long time for reducing EPS size by converting to greyscale… tried Convert … but in a sea of options is sometimes difficult to find what i was looking for.

    BTW… I have the following problem…

    EPS is great for generating plots… but sometimes my EPS gets HUGE when the database is biiiig… is there any way to reduce the size of the EPS in such a way that if I print the plot in a paper it still looks good? (I mean using a EPS conversion mechanism not by scattering my datapoints)

    Thanks man for your posts… have been useful more than once

    Reply  |  Quote
  21. ernest POLAND Mozilla Firefox Ubuntu Linux says:

    Thank you very much! It’s very helpful for me.

    Reply  |  Quote
  22. Breciox BRAZIL Mozilla Firefox Ubuntu Linux says:

    Good tip dude, I appreciate very much :)

    Reply  |  Quote
  23. Ravish Soni INDIA Safari Windows says:

    Please help me

    How to convert number of png files in eps formate in one Command. I use Ubunto for latex

    Reply  |  Quote
  24. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Ravish Soni:

    Wildcards dude. Just use wildcards:

    sam2p *.png EPS: *.eps

    I hope this helps.

    Reply  |  Quote
  25. Niek NETHERLANDS Mozilla Firefox Linux says:

    @ Luke Maciak:

    Seems obvious indeed, but sam2p doesn’t accept multiple file input. So the wildcard only works if there is only one file to convert in the directory.

    Wasn’t much of a help.

    Reply  |  Quote
  26. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Niek:

    Oh, then you can do the following:

    for i in $( ls *.png ); do
          sam2p $i EPS: $i.eps
    done

    This will iterate over all the png files in the directory.

    Reply  |  Quote
  27. Niek NETHERLANDS Mozilla Firefox Linux says:

    @ Luke Maciak:
    Thank you Luke, this would work perfectly. In fact it tries to run all the png-images in a folder. Seems to be a problem with sam2p though that keeps it from converting. Probably a crapy install. It echos

    sh: pngtopnm: not found
    sam2p: Error: Filter::PipeE: system() failed: (if (type -p png22pnm >/dev/null); then png22pnm -rgba /tmp/tmp__sam2p__6498_0; else (pngtopnm </tmp/tmp__sam2p__6498_0 && pngtopnm -alpha /tmp/tmp__sam2p__6498_1

    It does that even when you try to convert just one file with a straight forward command line. I will try to find out how to handle this.

    Reply  |  Quote
  28. Luke Maciak UNITED STATES Mozilla Firefox Windows Terminalist says:

    @ Niek:

    Sounds like you might be missing pngtopnm whatever that is. I’m not on linux right now, but I’d see if you can apt-get that pngtopnm package. Or obtain it using the package management system that is used by your distro.

    Reply  |  Quote
  29. Niek NETHERLANDS Mozilla Firefox Linux says:

    @ Luke Maciak:
    Let me start with wishing you a Happy 2010!
    Then on the topic: I did find the pngtopnm package, although it wasn’t in the repositories. I have downloaded png22pnm from the code site of sam2p (http://code.google.com/p/sam2p/). The errors above vanished and it seemed to start working, but the next error I got is that there were too many colors in my png-file. Quite strange because these were pictures drawn by a java-application (geogebra) and I have read that people have used sam2p to convert even pictures. How many more colors can a drawing have compared to a picture?
    Anyway, II gave up and converted all of the files by hand using GIMP.

    Reply  |  Quote
  30. foobar FINLAND Google Chrome Linux says:

    Luke Maciak wrote:

    @ Niek:

    Oh, then you can do the following:

    for i in $( ls *.png ); do
    sam2p $i EPS: $i.eps
    done
    This will iterate over all the png files in the directory.

    If someone is reading these, I should probably mention that using ls here breaks if the png files have whitespace in their names. Just use

    for i in *.png; do

    done

    instead.

    Reply  |  Quote
  31. psfrag SPAIN Safari Mac OS says:

    @ Chris:

    This is partially true. For example, if you use the package “psfrag” to replace text characters in your graphics by equations (or just because you want to use in your plot the same font that you use in the rest of the document). For doing this, the figure must be given in EPS format.

    Reply  |  Quote
  32. Risto FINLAND Mozilla Firefox Fedora Linux says:

    Neat! Thanks guys! A simple convert worked for me!

    convert banana.png banana.eps

    Reply  |  Quote
  33. Nana UNITED STATES Mozilla Firefox Linux says:

    Really helpful!

    Reply  |  Quote
  34. Abhi NETHERLANDS Google Chrome Ubuntu Linux says:

    HI Luke,
    Nice post buddy.
    However I am experiencing something weird.
    I generate a .png file from matlab, somehow I am having problems with .eps/.pdf files in Matlab. My .png file looks pretty, and now when I am converting this to .eps using $ sam2p test15_111.png EPS: test-1.eps
    I am experiencing something weird, that .eps files looks a bit stretched and also I loose the axis labels I have on my .png file. Can you please help me on this?
    Thanks

    Reply  |  Quote
  35. fdgs NORWAY Internet Explorer Windows says:

    The only slight disadvantage of doing this is that you pay a considerable size penalty for this conversion

    This made me giggle.

    Reply  |  Quote

Leave a Reply

Your email address will not be published. Required fields are marked *