Repository: python-pillow/Pillow

Issue:
P->RGBA->resize(LANCZOS)->P messes up transparency
### What did you do?
Given original image ![test.png](https://user-images.githubusercontent.com/25781945/27951758-500e2398-6349-11e7-8c68-b9cd5e39e166.png)
and following steps:

```python
from PIL import Image
im = Image.open('test.png')
im.mode # 'P'
im = im.convert('RGBA')
im = im.resize((320,240), Image.LANCZOS)
im = im.convert('P')
im.save('broken.png')
```
### What did you expect to happen?
![good](https://user-images.githubusercontent.com/2279051/29907685-b6a50a60-8dd1-11e7-87ff-bb972966fa82.png)

### What actually happened?
![bad](https://user-images.githubusercontent.com/2279051/29907638-62cec516-8dd1-11e7-9c81-8936a902d570.png)

### What versions of Pillow and Python are you using?
Python 2.7.13 / Pillow 4.2.1


However, if I slightly change the script and save RGBA image after convertion and resize to a file and then reopen that file, quantize it to palette, then it's all good:
```python
from PIL import Image
im = Image.open('test.png')
im = im.convert('RGBA')
im = im.resize((320,240), Image.LANCZOS)
im.save('intermidiary.png')
intermidiary = Image.open('intermidiary.png')
intermidiary.mode # 'RGBA'
intermidiary = intermidiary.convert('P')
intermidiary.save('good.png')
```


Discussion:

Resolving patch:
diff --git a/PIL/Image.py b/PIL/Image.py
--- a/PIL/Image.py
+++ b/PIL/Image.py
@@ -527,11 +527,12 @@ def _new(self, im):
         new.im = im
         new.mode = im.mode
         new.size = im.size
-        if self.palette:
-            new.palette = self.palette.copy()
-        if im.mode == "P" and not new.palette:
-            from . import ImagePalette
-            new.palette = ImagePalette.ImagePalette()
+        if im.mode in ('P', 'PA'):
+            if self.palette:
+                new.palette = self.palette.copy()
+            else:
+                from . import ImagePalette
+                new.palette = ImagePalette.ImagePalette()
         new.info = self.info.copy()
         return new
 


Test patch:
diff --git a/Tests/test_image.py b/Tests/test_image.py
--- a/Tests/test_image.py
+++ b/Tests/test_image.py
@@ -446,6 +446,33 @@ def test_radial_gradient(self):
             self.assert_image_equal(im, target)
 
 
+    def test__new(self):
+        from PIL import ImagePalette
+        
+        im = hopper('RGB')
+        im_p = hopper('P')
+
+        blank_p = Image.new('P', (10,10))
+        blank_pa = Image.new('PA', (10,10))
+        blank_p.palette = None
+        blank_pa.palette = None
+        
+        def _make_new(base_image, im, palette_result=None):
+            new_im = base_image._new(im)
+            self.assertEqual(new_im.mode, im.mode)
+            self.assertEqual(new_im.size, im.size)
+            self.assertEqual(new_im.info, base_image.info)
+            if palette_result is not None:
+                self.assertEqual(new_im.palette.tobytes(), palette_result.tobytes())
+            else:
+                self.assertEqual(new_im.palette, None)
+            
+        _make_new(im, im_p, im_p.palette)
+        _make_new(im_p, im, None)
+        _make_new(im, blank_p, ImagePalette.ImagePalette())
+        _make_new(im, blank_pa, ImagePalette.ImagePalette())
+        
+
 class MockEncoder(object):
     pass
 
diff --git a/Tests/test_image_convert.py b/Tests/test_image_convert.py
--- a/Tests/test_image_convert.py
+++ b/Tests/test_image_convert.py
@@ -87,6 +87,9 @@ def test_trns_p_rgba(self):
 
         # Assert
         self.assertNotIn('transparency', rgba.info)
+        # https://github.com/python-pillow/Pillow/issues/2702
+        self.assertEqual(rgba.palette, None)
+        
 
     def test_trns_l(self):
         im = hopper('L')