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]
Thank you very much :)
I was searching for converting tool between PNG to EPS, you post helped me alot.
Glad I could help :mrgreen:
Thank you very much for the advice! I was lokking for such a tool :)
Thank you very much for the advice! I was looking for such a tool :)
Awesome. :)
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.
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. :)
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
Very nice! Thanks for posting it!
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.
Yeah, that’s true. That’s the major downside of this method.
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.
Ah, thanks for the tip!
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.
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?
@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.
Swaprava: imagemagick is available on Windows too. link
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.
Well done, thanks for pointing this out.
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
Thank you very much! It’s very helpful for me.
Good tip dude, I appreciate very much :)
Please help me
How to convert number of png files in eps formate in one Command. I use Ubunto for latex
@ Ravish Soni:
Wildcards dude. Just use wildcards:
I hope this helps.
@ 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.
@ Niek:
Oh, then you can do the following:
This will iterate over all the png files in the directory.
@ 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
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.
@ 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.
@ 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.
Luke Maciak wrote:
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.
@ 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.
Neat! Thanks guys! A simple convert worked for me!
convert banana.png banana.eps
Really helpful!
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
This made me giggle.