# Smaller PNG graphic files from your LaTeX

I use LaTeX (LyX as the front end) to generate the occasional bits of mathematical script that appear here. Is it bad to admit it gives me a small thrill to see how the LaTex is transformed into something mathematical – makes me feel like a proper scientist/mathematician. Well, I’ve done it now…

WordPress.com will not display postscript natively (or if it can I have never been able to get it to work), so I usually open the .ps files in GIMP and convert them to a PNG – PNGs being optimised to handle things like line drawings and graphical images of text.

The PNGs that come out of this are of a reasonable size but they could be smaller. Some years ago I wrote some Perl code to do that (I needed something that worked in a CGI environment and while Perl is not so fast for even integer maths it was the best option).

That code is available on CPAN as Image::Pngslimmer and on a typical Linux/BSD (OSX too?) install (I am assuming you have perl and the cpan module installed) you should be able to get it on to your box with sudo cpan Image::Pngslimmer – which may ask you a lot of questions before it installs if you have never used CPAN but while YMMV the default answers should probably work.

You can write your own script or just use this quick and dirty one for PNGs of 64K or less:

#!/usr/bin/perl -w
use Image::Pngslimmer();
my $srcfile = shift; open INF,$srcfile;
binmode INF;
read(INF, $buffer, 65535); my$blob2 = Image::Pngslimmer::indexcolours($buffer); my$blob3 = Image::Pngslimmer::discard_noncritical($blob2); print Image::Pngslimmer::zlibshrink($blob3);


To use this copy the script above into your editor and save it with a name eg shrinker.pl and then execute like this: perl shrinker.pl some.png > smaller.png which will create a new file smaller.png from some.png (which is untouched).

The results can be reasonably impressive. This file – created by cutting from the GIMP – comes in at 14.4KB (NB the graphics you see here are preprocessed by wordpress.com, click on the graphics to see the original):

But after a bit of processing with the above script, this comes in at 9.7KB:

Finally, please do not say this doesn’t matter because your pipe is so fat. Millions of these things are being pumped through the internet every minute and the collective saving of bandwidth really would matter if we could deliver it…

Talk to the hand

## 4 thoughts on “Smaller PNG graphic files from your LaTeX”

1. You can skip the PNG files and go straight from LaTeX to blog display with MathJax. WordPress apparently has a plug-in for it. I wish Blogger did, but no such luck. 😦

2. Thanks.

But I don’t think you can use that with a wordpress.com blog – or if you can I have never worked it out. If I could be bothered to host a blog on my own server (I have done that in the past but it can be a pain) then there are a few plugins for this sort of stuff.

3. I don’t know much about WordPress, but I’m pretty sure the plug-in was intended to work in blogs. At least this post suggests so.

I use MathJax in my blog, by adding a script tag at the top of the body of each post. That should work if the plug-in doesn’t.

4. Paul,

Yes, the plugin works, I am sure, on a downloaded WordPress blog but not – as far as I know – on one of the ones they host (like this one): here you have to just take from the very limited range the provider offers you. I can see why that makes sense of course – means I cannot write a malicious plugin and take over wordpress.com, for instance.

Thanks for the tip about the script, though, I’ll check that one out