Repository: python-pillow/Pillow

Issue:
Error in Image.new
After update to 3.2.0 I have broken test in my project with error.

``` python
from PIL import Image

Image.new('P', (200, 200), color='#DDEEFF')

ERROR:
  File "/home/igor/.virtualenvs/dju-image/local/lib/python2.7/site-packages/PIL/Image.py", line 2036, in new
    return Image()._new(core.fill(mode, size, color))
TypeError: function takes exactly 1 argument (3 given)
```



Discussion:
Maybe I should pass color as integer 0-255. But I can't pass RGB color as before.

Pretty sure that's from the cleanup of the _getink function so that it doesn't use uninitialized variables. 

Try passing in a tuple of (0xdd, 0xee, 0xff). 

(edit) Actually, since it's a P mode image, I bet that's something that's covered in https://github.com/python-pillow/Pillow/issues/1664

Looks like an unintentional bug, especially as it's occurring between the Python and C layers of Pillow.

`git bisect` says 5ff9f24fcc713795b580e6e22b42e50e7f17fcf0 is the first bad commit from PR https://github.com/python-pillow/Pillow/pull/1663.

`Image.new('P', (200, 200), color=(0xdd, 0xee, 0xff))` also has the same error.

Did the previous version work correctly, or did it just run?

When doing getink in a `P` mode image, it has to be a integer, since there's no support for quantizing the rgb color into a number. 

I think for it to work correctly, the image would have to be created as `RGB(A)` and then converted to a `P` mode so that it would go through the quantization process. 

edit:

Without actually running it with gdb, I'm going to say that the error is in the C layer, _imaging.c, line 512, where it's parsing a 1 tuple into an integer. Because the color that's passed in is a tuple, and it's 3 elements long due to the output from ImageColor. 

``` python
from PIL import Image
im = Image.new('P', (200, 200), color='#DDEEFF')
im.save("out.png")
```

3.1.0 runs but not correctly:
![out](https://cloud.githubusercontent.com/assets/1324225/14261048/7adc3b6a-fab7-11e5-979d-9cb5a0042456.png)

``` python
from PIL import Image
im = Image.new('RGBA', (200, 200), color='#DDEEFF')
im.convert("P")
im.save("out2.png")
```

Converting with 3.1.0 works correctly:
![out2](https://cloud.githubusercontent.com/assets/1324225/14261153/0f2976d4-fab8-11e5-925c-df11f32cd96f.png)

Converting with 3.2.0 also works correctly:
![out3](https://cloud.githubusercontent.com/assets/1324225/14261303/a8975020-fab8-11e5-8272-9b9d9c924a47.png)

So: 
- the original snippet didn't work in 3.1.0 but otherwise ran smoothly so the error wasn't visible,
- in 3.2.0 the original snippet also doesn't work, and also results in an error,
- creating as RGBA and converting to P works with both 3.1.0 and 3.2.0. 

Resolving patch:
diff --git a/src/PIL/Image.py b/src/PIL/Image.py
--- a/src/PIL/Image.py
+++ b/src/PIL/Image.py
@@ -2374,7 +2374,14 @@ def new(mode, size, color=0):
         from . import ImageColor
         color = ImageColor.getcolor(color, mode)
 
-    return Image()._new(core.fill(mode, size, color))
+    im = Image()
+    if mode == "P" and \
+       isinstance(color, (list, tuple)) and len(color) in [3, 4]:
+        # RGB or RGBA value for a P image
+        from . import ImagePalette
+        im.palette = ImagePalette.ImagePalette()
+        color = im.palette.getcolor(color)
+    return im._new(core.fill(mode, size, color))
 
 
 def frombytes(mode, size, data, decoder_name="raw", *args):


Test patch:
diff --git a/Tests/test_image.py b/Tests/test_image.py
--- a/Tests/test_image.py
+++ b/Tests/test_image.py
@@ -522,6 +522,16 @@ def _make_new(base_image, im, palette_result=None):
         _make_new(im, blank_p, ImagePalette.ImagePalette())
         _make_new(im, blank_pa, ImagePalette.ImagePalette())
 
+    def test_p_from_rgb_rgba(self):
+        for mode, color in [
+            ("RGB", '#DDEEFF'),
+            ("RGB", (221, 238, 255)),
+            ("RGBA", (221, 238, 255, 255))
+        ]:
+            im = Image.new("P", (100, 100), color)
+            expected = Image.new(mode, (100, 100), color)
+            self.assert_image_equal(im.convert(mode), expected)
+
     def test_no_resource_warning_on_save(self):
         # https://github.com/python-pillow/Pillow/issues/835
         # Arrange