How to use Pillow with Django

38,500

Solution 1

The problem is that imports now work slightly differently with Pillow vs PIL. The differences are described here: http://pillow.readthedocs.org/en/latest/porting-pil-to-pillow.html

Django has also now been changed to prefer Pillow over PIL, via this ticket (https://code.djangoproject.com/ticket/19934)

This commit is present in the new Django 1.6a1 release, so the new behaviour will be present in the Django 1.6 release. For now, however, it appears that you can use a new library (initially released May 20, 2013) called Pillow-PIL which will provide a compatibility layer. This can be easily installed with pip via: pip install --pre Pillow-PIL

Solution 2

First, install pillow (having virtualenv activated preferably) with:

pip install pillow

You should import it in Django project:

from PIL import Image

After that you don't need to change settings or anything else. All the modules should work.

Solution 3

In a Python module that makes a drawing I simply put the following.

import PIL.Image as Image

and likewise for ImageDraw and ImageFont. Those were the only changes that were necessary after a routine PIP installation.

Share:
38,500
woltob
Author by

woltob

Updated on July 09, 2022

Comments

  • woltob
    woltob almost 2 years

    I installed Pillow and now want to use it on my Django site to allow uploading of images of through my admin page. See previous question.

    What changes do I need to make in my settings file or elsewhere for Django to recognize Pillow and to allow the ImageField to upload the image properly?

  • woltob
    woltob about 11 years
    I don't have my settings.py file configured for PIL... so how would I do that?
  • Brandon Taylor
    Brandon Taylor about 11 years
    You don't have to make any settings changes at all. It simply has to be available on the PYTHONPATH. I will edit my answer to reflect that.
  • woltob
    woltob about 11 years
    how do i make sure it is available on PYTHONPATH?
  • Brandon Taylor
    Brandon Taylor about 11 years
    Good question. It depends on whether or not you're using virtualenv, which if you're not, you should.
  • Dan
    Dan almost 11 years
    I'm on Django 1.5.1 and using buildout. This worked like a charm, now all my images know how big they are. Thank You
  • 7stud
    7stud about 10 years
    I'm trying to do the Tango with Django tutorial, which uses django 1.5. I'm using virtualenvwrapper. I installed Pillow with pip. pip freeze shows: Django==1.5.5 Pillow==2.4.0 South==0.8.4 wsgiref==0.1.2. However, in chapter 8, I get the following error: (tangowithdjango)~/dev_django_projects/tangowithdjango$ ./manage.py syncdb *CommandError: One or more models did not validate: rango.userprofile: "picture": To use ImageFields, you need to install the Python Imaging Library...
  • Joey Wilhelm
    Joey Wilhelm almost 10 years
    @7stud If you are using Django 1.5.5, you are going to need to install Pillow-PIL still (pip install Pillow-PIL). If you want to eliminate this dependency, then upgrade to Django 1.6.x or the upcoming 1.7 release.