Repository: wagtail/wagtail

Issue:
Getting a 500 error when trying to add an existing image via ImageChoosers
In Wagtail 0.8.1, trying to add an image with a focal point region and getting the following

```
/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/PIL/Image.py:755: UserWarning: Palette images with Transparency expressed  in bytes should be converted to RGBA images
  ' in bytes should be converted to RGBA images')

Internal Server Error: /admin/images/chooser/3844/

UnboundLocalError at /admin/images/chooser/3842/
local variable 'new' referenced before assignment

Request Method: GET
Request URL: http://myproject.local:8111/admin/images/chooser/3842/
Django Version: 1.6.7
Python Executable: /home/vagrant/.virtualenvs/myproject/bin/python
Python Version: 2.7.3
Python Path: ['/home/vagrant/myproject', '/home/vagrant/.virtualenvs/myproject/lib/python2.7', '/home/vagrant/.virtualenvs/myproject/lib/python2.7/plat-linux2', '/home/vagrant/.virtualenvs/myproject/lib/python2.7/lib-tk', '/home/vagrant/.virtualenvs/myproject/lib/python2.7/lib-old', '/home/vagrant/.virtualenvs/myproject/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-linux2', '/usr/lib/python2.7/lib-tk', '/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages']

[...]

Traceback:
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  112.                     response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/django/contrib/auth/decorators.py" in _wrapped_view
  22.                 return view_func(request, *args, **kwargs)
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/views/chooser.py" in image_chosen
  109.         {'image_json': get_image_json(image)}
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/views/chooser.py" in get_image_json
  22.     preview_image = image.get_rendition('max-130x100')
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/models.py" in get_rendition
  181.             generated_image = filter.process_image(file_field.file, backend_name=backend_name, focal_point=self.get_focal_point())
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/models.py" in process_image
  362.         image = method(image, method_arg, focal_point=focal_point)
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/backends/base.py" in resize_to_max
  62.         return self.resize(image, final_size)
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/wagtail/wagtailimages/backends/pillow.py" in resize
  21.             image = image.convert('RGB')
File "/home/vagrant/.virtualenvs/myproject/local/lib/python2.7/site-packages/PIL/Image.py" in convert
  810.             del(new.info['transparency'])

Exception Type: UnboundLocalError at /admin/images/chooser/3842/
Exception Value: local variable 'new' referenced before assignment
Request information:
GET: No GET data

POST: No POST data

FILES: No FILES data
```

The image in question: ![torch](https://cloud.githubusercontent.com/assets/31622/4993072/dbe5c776-69a1-11e4-928f-9d10944b17be.png)

Thank you,
Dan



Discussion:

Resolving patch:
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -35,7 +35,7 @@
     "django-modelcluster>=0.4",
     "django-taggit==0.12.2",
     "django-treebeard==2.0",
-    "Pillow>=2.3.0",
+    "Pillow>=2.6.1",
     "beautifulsoup4>=4.3.2",
     "html5lib==0.999",
     "Unidecode>=0.04.14",
diff --git a/wagtail/wagtailimages/backends/pillow.py b/wagtail/wagtailimages/backends/pillow.py
--- a/wagtail/wagtailimages/backends/pillow.py
+++ b/wagtail/wagtailimages/backends/pillow.py
@@ -16,20 +16,20 @@ def open_image(self, input_file):
     def save_image(self, image, output, format):
         image.save(output, format, quality=self.quality)
 
+    def _to_rgb(self, image):
+        if image.mode not in ['RGB', 'RGBA']:
+            if 'transparency' in image.info and isinstance(image.info['transparency'], bytes):
+                image = image.convert('RGBA')
+            else:
+                image = image.convert('RGB')
+        return image
+
     def resize(self, image, size):
-        if image.mode in ['1', 'P']:
-            image = image.convert('RGB')
-        return image.resize(size, PIL.Image.ANTIALIAS)
+        return self._to_rgb(image).resize(size, PIL.Image.ANTIALIAS)
 
     def crop(self, image, rect):
         return image.crop(rect)
 
     def image_data_as_rgb(self, image):
-        # https://github.com/thumbor/thumbor/blob/f52360dc96eedd9fc914fcf19eaf2358f7e2480c/thumbor/engines/pil.py#L206-L215
-        if image.mode not in ['RGB', 'RGBA']:
-            if 'A' in image.mode:
-                image = image.convert('RGBA')
-            else:
-                image = image.convert('RGB')
-
+        image = self._to_rgb(image)
         return image.mode, image.tostring()


Test patch: