2015-04-02 15:18:51 +02:00
===============
Advanced topics
===============
2015-04-02 15:40:33 +02:00
Animated GIF support
====================
Pillow (Wagtail's default image library) doesn't support resizing animated
GIFs. If you need animated GIFs in your site, install
`Wand <https://pypi.python.org/pypi/Wand> `_ .
When Wand is installed, Wagtail will automatically start using it for resizing
GIF files, and would continue to resize other images with Pillow.
2014-07-29 18:19:50 +02:00
.. _image_feature_detection:
2014-07-29 14:13:39 +02:00
Feature Detection
=================
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Wagtail has the ability to automatically detect faces and features inside your images and crop the images to those features.
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Feature detection uses OpenCV to detect faces/features in an image when the image is uploaded. The detected features stored internally as a focal point in the `` focal_point_{x, y, width, height} `` fields on the `` Image `` model. These fields are used by the `` fill `` image filter when an image is rendered in a template to crop the image.
2014-07-22 18:40:30 +02:00
Setup
2015-04-02 15:18:51 +02:00
-----
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Feature detection requires OpenCV which can be a bit tricky to install as it's not currently pip-installable.
2014-07-22 18:40:30 +02:00
Installing OpenCV on Debian/Ubuntu
2015-04-02 15:18:51 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Debian and ubuntu provide an apt-get package called `` python-opencv `` :
2014-07-22 18:40:30 +02:00
.. code-block :: bash
sudo apt-get install python-opencv python-numpy
2014-07-29 14:28:13 +02:00
This will install PyOpenCV into your site packages. If you are using a virtual environment, you need to make sure site packages are enabled or Wagtail will not be able to import PyOpenCV.
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Enabling site packages in the virtual environment
2015-04-02 15:18:51 +02:00
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
If you are not using a virtual envionment, you can skip this step.
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Enabling site packages is different depending on whether you are using pyvenv (Python 3.3+ only) or virtualenv to manage your virtual environment.
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
pyvenv
`` ` ` ``
Go into your pyvenv directory and open the `` pyvenv.cfg `` file then set `` include-system-site-packages `` to `` true `` .
virtualenv
`` ` ` ` ` ` ` ``
Go into your virtualenv directory and delete a file called `` lib/python-x.x/no-global-site-packages.txt `` .
Testing the OpenCV installation
2015-04-02 15:18:51 +02:00
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2014-07-29 14:28:13 +02:00
You can test that OpenCV can be seen by Wagtail by opening up a python shell (with your virtual environment active) and typing:
2014-07-22 18:40:30 +02:00
.. code-block :: python
import cv
2014-07-29 17:35:01 +02:00
If you don't see an `` ImportError `` , it worked. (If you see the error `` libdc1394 error: Failed to initialize libdc1394 `` , this is harmless and can be ignored.)
2014-07-23 10:20:17 +02:00
2014-07-22 18:40:30 +02:00
2014-07-29 14:28:13 +02:00
Switching on feature detection in Wagtail
2015-04-02 15:18:51 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-07-23 10:20:17 +02:00
2014-07-29 14:28:13 +02:00
Once OpenCV is installed, you need to set the `` WAGTAILIMAGES_FEATURE_DETECTION_ENABLED `` setting to `` True `` :
2014-07-22 18:40:30 +02:00
.. code-block :: python
# settings.py
2014-07-23 12:28:32 +02:00
WAGTAILIMAGES_FEATURE_DETECTION_ENABLED = True
2014-07-22 18:40:30 +02:00
2014-07-23 10:20:17 +02:00
Manually running feature detection
2015-04-02 15:18:51 +02:00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2014-07-23 10:20:17 +02:00
2014-07-29 14:28:13 +02:00
Feature detection runs when new images are uploaded in to Wagtail. If you already have images in your Wagtail site and would like to run feature detection on them, you will have to run it manually.
You can manually run feature detection on all images by running the following code in the python shell:
2014-07-23 10:20:17 +02:00
.. code-block :: python
from wagtail.wagtailimages.models import Image
2014-07-22 18:40:30 +02:00
2014-07-23 10:20:17 +02:00
for image in Image.objects.all():
2014-10-26 13:04:03 +01:00
if not image.has_focal_point():
image.set_focal_point(image.get_suggested_focal_point())
2014-07-23 10:20:17 +02:00
image.save()