How to use Pillow with Django

风格不统一 提交于 2019-12-05 11:45:22

问题


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?


回答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




回答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.




回答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.




回答4:


You shouldn't have to make any changes in your settings.py or otherwise to use Pillow instead of PIL. Its a drop-in replacement, and simply has to be available on your PYTHONPATH.



来源:https://stackoverflow.com/questions/15080121/how-to-use-pillow-with-django

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!