A Virtual Home - Ubuntuhttps://blog.avirtualhome.com/2018-03-28T09:03:00-04:00Replace JPEG libraries with MozJPEG2018-03-28T09:03:00-04:002018-03-28T09:03:00-04:00Peter van der Doestag:blog.avirtualhome.com,2018-03-28:/replace-jpeg-libraries-with-mozjpeg/<p>For a project in Python we had to squeeze more bytes out of <span class="caps">JPG</span> files using Pillow. Currently MozJPEG fits that bill but there isn&rsquo;t a repository available to install it on&nbsp;Ubuntu.</p><p>For an image heavy site we were building we needed to squeeze more bytes out of the <span class="caps">JPEG</span> files. We use <a href="http://pillow.readthedocs.io/en/latest/">Pillow</a> within our Python project to create thumbnails which in turn uses the <span class="caps">JPEG</span> libraries installed on your system, so we had to look for a 1-on-1 replacement of the system jpeg&nbsp;libraries.</p> <p>For Ubuntu you can use <a href="https://libjpeg-turbo.org/">libjpeg-turbo</a> but using <a href="https://github.com/mozilla/mozjpeg">MozJPEG</a> by Mozilla makes the thumbnails even smaller. The only problem we ran into was the fact there is no repository you can add in Ubuntu and therefore we had to compile MozJPEG&nbsp;manually.</p> <p>If you just want to skip the steps go to <a href="#tldr">tl;dr</a>. All the steps need to be ran as&nbsp;root.</p> <h4 id="install-requirements">Install&nbsp;requirements</h4> <p>To compile MozJPEG you need to install some&nbsp;requirements.</p> <div class="highlight"><pre><span></span>apt -y install build-essential cmake libtool autoconf automake m4 nasm pkg-config </pre></div> <p>and then configure the dynamic linker run-time bindings <div class="highlight"><pre><span></span>ldconfig /usr/lib </pre></div></p> <h4 id="get-mozjpeg-source">Get MozJPEG&nbsp;source</h4> <p>We&rsquo;ll be working with version 3.2 of the MozJPEG library. <div class="highlight"><pre><span></span>wget https://github.com/mozilla/mozjpeg/archive/v3.2.tar.gz tar xf v3.2.tar.gz </pre></div></p> <h4 id="configure-and-install">Configure and&nbsp;Install</h4> <p>Before we can configure and install we have to create the configuration. Go to the directory you extract the archive&nbsp;in.</p> <div class="highlight"><pre><span></span><span class="nb">cd</span> mozjpeg-3.2 autoreconf -fiv </pre></div> <p>To keep source and build separate we&rsquo;ll do the build in it&rsquo;s own&nbsp;directory.</p> <div class="highlight"><pre><span></span>mkdir build <span class="nb">cd</span> build sh ../configure --with-jpeg8 make install <span class="nv">libdir</span><span class="o">=</span>/usr/lib/x86_64-linux-gnu <span class="nv">prefix</span><span class="o">=</span>/usr </pre></div> <p>We have to copy one source file over as it&rsquo;s not included in the build. <div class="highlight"><pre><span></span>cp ../jpegint.h /usr/include/jpegint.h </pre></div></p> <p>That&rsquo;s it, now almost any program on your server that use the <span class="caps">JPEG</span> libraries to create images will be using MozJPEG and making the files much smaller than with the standard or even libjpeg-turbo&nbsp;libraries.</p> <h4 id="tldr"><span class="caps">TL</span>;<span class="caps">DR</span></h4> <p>The script below will do all the above steps. Remember to run this as&nbsp;root.</p> <div class="highlight"><pre><span></span><span class="c1">#/bin/sh</span> apt -y install build-essential cmake libtool autoconf automake m4 nasm pkg-config ldconfig /usr/lib rm -rf mozjpeg-3.2 wget https://github.com/mozilla/mozjpeg/archive/v3.2.tar.gz tar xf v3.2.tar.gz <span class="nb">cd</span> mozjpeg-3.2 autoreconf -fiv mkdir build <span class="nb">cd</span> build sh ../configure --with-jpeg8 make install <span class="nv">libdir</span><span class="o">=</span>/usr/lib/x86_64-linux-gnu <span class="nv">prefix</span><span class="o">=</span>/usr cp ../jpegint.h /usr/include/jpegint.h </pre></div> <h4 id="bonus">Bonus</h4> <p>If you use Pillow in your Python project and it was already installed you need to reinstall it, we ran into issues where after reinstalling it still would not use the MozJPEG libraries. In order to make that work, we had to recompile Pillow. The code below will recompile&nbsp;pillow</p> <div class="highlight"><pre><span></span>pip install --upgrade --no-cache-dir --force-reinstall --no-binary :all: --compile -v Pillow </pre></div>