Caspershire Meta

jpeg-recompress and pngquant

I saw this thread on Hacker News on archiving JPEGs for long-term storage. For as long as I can remember, I am a big fan of compressing files to free up storage space. As far as blogging is concerned, I’ve always used TinyPNG web service to compress image files before uploading them to a server.

From that thread on Hacker News, I thought that it would be nicer and faster if I could compress all my images locally without having to upload to compress through a web service. That thought was put into a plan. I grabbed JPEG-Archive v2.1.1 from its release page and placed the pre-compiled binaries in my $PATH (I am using Git Bash on Windows 10). There are three binaries in total: jpeg-recompress, jpeg-compare, and jpeg-hash. In theory, jpeg-recompress binary alone should suffice but it would be nice to have all three.

To run jpeg-recompress, simply run:

$ jpeg-recompress image-original.jpg image-compressed.jpg

I use GNU find utility to run jpeg-recompress sequentially over a large number or files (instead of writing a bash script). Caution! This command below overwrites the existing file.

$ find * -type f -print -exec jpeg-recompress {} {} \;

Look at that!

The result? Well, I must say I was really pleased.

However, jpeg-recompress can only work with JPEGs and it won’t work with PNGs. Since most of images on Caspershire Meta are PNGs (mostly desktop screenshots), I needed to figure out a way to do this in CLI-style. I went with pngquant and it worked perfectly. I downloaded the binary file from its official website and placed the binary into my $PATH.

To use pngquant, simply run:

$ pngquant --quality=65-80 image.png

The newly-compressed file (a.k.a the output) will have the -fs8.png extension. To run sequentially with GNU find utility and to overwrite existing file, run this:

$ find * -type f -print -exec pngquant --quality=65-80 {} --ext=.png --force \;

What about the compression? I was really pleased!

Performance is great!

This would make my blogging workflow a little bit faster and now I have a better way of compressing photos before storing them on AWS S3 or sending them to friends.

jpeg-recompress is based on mozjpeg. So, in theory, I could also use mozjpeg to compress my JPEGs. I’ve read some reviews about mozjpeg and its performance, seems like it works pretty darn good.