Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError: decoder jpeg not available on Windows

I am developing on a Django project and recently ran into a nasty problem. I installed the Pillow library on my Windows computer and it unexpectedly threw an OSError when trying to display an image in my Django template.

OSError: decoder jpeg not available

How can I fix this?


This got downvoted the second I submitted it. So I feel forced to mention the following (I have my pride!):

  • This is a Q&A (self-answered).
  • I will go into detail in the answer.
  • It's originally a blog post that took me several hours to write! It's my contribution, not someone elses I stole.
  • Source: http://blog.danic.net/oserror-decoder-jpeg-not-available/
like image 552
Daniel Avatar asked Jan 10 '23 23:01

Daniel


1 Answers

A Short Explanation

This is because the Pillow package does not bring the required libraries. That’s something you need to take care of. Hence JPEG support (along with other file formats) is not available.

To verify this, have a look at the setup summary after installing Pillow. If you don't have that around, simplay reinstall the package and the summary will be shown.

$ sudo pip uninstall Pillow
$ sudo pip install Pillow

In the summary we see that I installed Pillow version 2.3.1 on Windows 8.1. Most importantly, we see that there is no support for JPEG and PNG (ZLIB) image files.

--------------------------------------------------------------------
PIL SETUP SUMMARY
--------------------------------------------------------------------
version      Pillow 2.3.1
platform     win32 3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:24:06
             [MSC v.1600 32 bit (Intel)]
--------------------------------------------------------------------
*** TKINTER support not available
(Tcl/Tk 8.6 libraries needed)
*** JPEG support not available
*** ZLIB (PNG/ZIP) support not available
*** LIBTIFF support not available
*** FREETYPE2 support not available
*** LITTLECMS2 support not available
*** WEBP support not available
*** WEBPMUX support not available
--------------------------------------------------------------------
To add a missing option, make sure you have the required
library, and set the corresponding ROOT variable in the
setup.py script.

To check the build, run the selftest.py script.

A definite Solution

On Linux, simply install package libjpeg-dev and reinstall the Pillow library.

But since we are working with Windows, we have to go a little deeper: We need to download the jpeg library sources to provide the header files and build the library file on our own. Also do some copy and paste.

You need to install Visual C++ 2010 Express. You need it anyway to build Pillow in the first place, or you end up with the error: Unable to find vcvarsall.bat.

Then download the jpeg library package from the Independent JPEG Group and extract it to a temporary location.

Copy the header files

In the jpeg package search for three files named

  • jpeglib.h
  • jmorecfg.h
  • jconfig.h (edit: needs to be created, see comments)

and copy them into the include folder of your Python installation directory (probably "C:\Python34\include\")

Build the library file

To be able to finish the next steps, run vcvarsall.bat. This will add all the necessary directories to your PATH variable. By default, you find this file in "C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\vcvarsall.bat"

Now open a command line and find the directory where you extracted the jpeg library source earlier on. Execute the following two commands:

> nmake /f makefile.vc setup-v10
> msbuild jpeg.sln

The second command builds the required file and places them in the newly created subdirectory \Release\.

Clean up and rebuild Pillow with JPEG support

Awesome, now we only need to copy the freshly built Release\jpeg.lib into our Python-libs directory (probably "C:\Python34\libs\"). Note: "libs", not "Lib".

Once more, install the Pillow package and take care of the setup summery. JPEG support is now available.

--- JPEG support available
like image 96
Daniel Avatar answered Jan 22 '23 16:01

Daniel