Mozilla’s new lossless JPEG compression

I was quite impressed with Mozillas new jpegtran in mozjpeg. It crushed a 2.2GB photo shoot down to 1.4GB, a bit smaller than the 1.5GB achieved by jpegoptim. So I thought I’d try to make it drop-dead simple to play with.

My Online Demo



If you wanted to losslessly recompress an entire directory you could do something like:
mkdir -p crush; for F in *.jpg; do curl -F "file=@$F" https://dansted.org/cgi-bin/jpegtran.cgi > "crush/$F"; done

on Linux/Mac, or on the Windows Command Prompt:
MKDIR crush & FOR %F IN (*.jpg) DO ( CURL -F "file=@%F" "https://dansted.org/cgi-bin/jpegtran.cgi" > "crush\%F" )

But you might want to install mozjpeg locally instead. I won’t try to do anything evil with your photos, but my server is not exactly secured with CrowdStrike :P.

Downloads

64bit Windows Binaries: These were created on Windows 11 using:

git clone https://github.com/Microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
vcpkg install mozjpeg
REM Wait 1.5 minutes... and DONE!

The rest of this article will focus on Linux. Windows users probably wouldn’t that excited until there is a GUI anyway. I was thinking of making one, but that is a whole new kettle of fish.

CGI Wrapper
64bit Linux Binaries:
If you are going to grab random binaries off the web and run them, I suggest you at least use firejail. Something like:

firejail --quiet --net=none --blacklist=/boot --blacklist=/chroot --blacklist=/chroot24.04 --blacklist=/dev --blacklist=/etc --blacklist=/home --blacklist=/install --blacklist=/lost+found --blacklist=/media --blacklist=/mnt --blacklist=/opt --blacklist=/proc --blacklist=/root --blacklist=/run --blacklist=/sbin --blacklist=/snap --blacklist=/srv --blacklist=/sys --blacklist=/tmp --blacklist=/var jpegtran -progressive < in.jpg > out.jpg

This should block everything except /bin, /lib, /lib64 and /usr, the only directories jpegtran needs. It would be nice if the –whitelist option of firejail worked, but hey. In general, you can test what directories an executable needs with a script such as testfirejail.sh.

The binaries were created on Oracle Linux 7.9 with:
sudo yum install git cmake make nasm gcc
wget https://download.sourceforge.net/libpng/libpng-1.6.43.tar.xz
wget https://www.zlib.net/zlib-1.3.1.tar.gz
git clone https://github.com/mozilla/mozjpeg.git
tar -xf libpng-1.6.43.tar.xz
tar -xf zlib-1.3.1.tar.gz
( cd zlib-1.3.1; ./configure; make -j4; sudo make install )
( cd libpng-1.6.43; ./configure; make -j4; sudo make install )
( cd mozjpeg ; cmake . ; make -j4 ; sudo make install)

This can also be installed on Ubuntu 16.04 with:
sudo apt install git cmake make nasm gcc
wget https://download.sourceforge.net/libpng/libpng-1.6.43.tar.xz
git clone https://github.com/mozilla/mozjpeg.git
tar -xf libpng-1.6.43.tar.xz
( cd libpng-1.6.43; ./configure; make -j4; sudo make install )
( cd mozjpeg ; cmake . ; make -j4 ; sudo make install)

Note that with supported Ubuntu versions (i.e. 20.04 or newer) the included libpng is 1.6+ so the following will work:

sudo apt install git cmake make nasm gcc libpng-dev
git clone https://github.com/mozilla/mozjpeg.git
( cd mozjpeg ; cmake . ; make -j4 ; sudo make install)

Comments

Leave a Reply

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