Space saving with MozJPEG
date: 2015-01-01 21:53:55+00:00 categories: - Linux tags: - tools
Today, I read a post about MozJPEG, and decided that it looked interesting enough to try out. This post is mostly based on that post, but a little more step-by-step.
Download a release version of the source from GitHub
Extract someplace convenent
cd $HOME/.local/src && tar xaf ~/Downloads/mozjpeg-3.0-release-source.tar.gz
- Build like any other source
cd mozjpeg/
export CFLAGS="-march=native -O2 -pipe -fstack-protector --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2" # optional, taken from my /etc/makepkg.conf
./configure --prefix=$HOME/.local && make && make install
- Test it!
cd $HOME/tmp
mkdir mozjpeg
cd mozjpeg
# use graphical file manager to copy in some pictures
/home/brian/.local/bin/cjpeg -quality 80 -outfile test.jpg 7A5bkcC.jpg
This works fine on JPEGs and PNGs, but when I tried a GIF it errored out: “GIF input is unsupported for legal reasons. Sorry.” No worries, you can use another utility to convert the GIF and pipe it in:
convert mypicture.gif TGA:- | /home/brian/.local/bin/cjpeg -quality 70 -outfile gifconverted.jpg
- Automate it If you’re comfortable that this doesn’t hurt image quality noticeably and don’t mind ugly filenames, you can convert whole batches of images.
for f in ./*.jpg; do /home/brian/.local/bin/cjpeg -quality 80 -outfile "$f.jpg" "$f" && rm "$f" ; done;
for f in ./*.png; do /home/brian/.local/bin/cjpeg -quality 80 -outfile "$f.jpg" "$f" && rm "$f" ; done;
You could do this with GIFs, but this is a bad idea because most GIFs are animated and not still images.