4

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!):

10
  • Is this your own blogpost? And did you just copy it verbatim? Mar 21, 2014 at 12:46
  • 1
    +1 I don't know who -1'ed it without explanation. Probably because of the shameless plug. You are encouraged to answer your own question. The python ecosystem on windows needs posts like this that help even out the speedbumps. Mar 21, 2014 at 14:57
  • 1
    It is ok to link to your own work like this. This is no advertisement and the answer isn't just the link Mar 21, 2014 at 17:15
  • 1
    Someone has put a close vote on this question for "unclear what you're asking". If I were inclined to guess, I'd guess that the downvote (even if from someone else than the downvoter) was for this rather than any issue regarding authorship. (But authorship should still be clear. I'd suggest moving it in the answer though.) Although I disagree with the close vote, I can see where they are coming from. We get a lot of questions which consist of a small description + an error message, and this leads a whole bunch of answers based on hypotheticals.
    – Louis
    Mar 21, 2014 at 17:20
  • 1
    One thing that would help the question look better would be to move the beginning of the answer into the question to say something like (paraphrasing here) "I've tried reinstalling and notice that the summary says ... jpeg is not installed. How can I fix this?" This way, the people who are searching for the error message would still get to this question, and people just stumbling upon it would most likely not have a problem with the contents of the question since it would have been narrowed down to something reasonably specific.
    – Louis
    Mar 21, 2014 at 17:25

1 Answer 1

9

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
10
  • +1, I have no idea about the correctness of the solution (python on *nix is my cup of tea). But you took the time to markup your blogpost and share it with the world on a site with more chance for people to find it. Well done. You'll get more recognition once people that encounter this problem search for it. Mar 21, 2014 at 17:08
  • 1
    Ah, this file must be created: falconview.org/svn/Sandbox/mapnik/libjpeg-8a/install.txt
    – duhaime
    Oct 19, 2014 at 18:45
  • 1
    @duhaime, thanks for the addition. I edited accordingly.
    – Daniel
    Apr 15, 2015 at 9:51
  • 1
    @twobob I'm sorry to say this is really jogging the memory and I don't have access to a Windows machine right now. I do recall that pip install pillow would install PIL and handle more dependencies, but I don't know if that will bring this dependency with it. Sorry!
    – duhaime
    May 1, 2017 at 13:23
  • 1
    @twobob I think you're right about external linking. Would you want to post an answer with these notes in case they help others? Either way, I hope you're off and running!
    – duhaime
    May 1, 2017 at 15:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.