allow lists as arguments for Image.new

This commit is contained in:
homm 2016-10-04 03:06:35 +03:00
parent 68a0b5e5a5
commit c5e111e6b8
2 changed files with 8 additions and 6 deletions

View File

@ -1020,7 +1020,7 @@ class Image(object):
4-tuple defining the left, upper, right, and lower pixel 4-tuple defining the left, upper, right, and lower pixel
coordinate. coordinate.
Note: Prior to Pillow 3.4.0, this was a lazy operation. Note: Prior to Pillow 3.4.0, this was a lazy operation.
:param box: The crop rectangle, as a (left, upper, right, lower)-tuple. :param box: The crop rectangle, as a (left, upper, right, lower)-tuple.
:rtype: :py:class:`~PIL.Image.Image` :rtype: :py:class:`~PIL.Image.Image`
@ -1993,7 +1993,7 @@ def _check_size(size):
:returns: True, or raises a ValueError :returns: True, or raises a ValueError
""" """
if not isinstance(size, tuple): if not isinstance(size, (list, tuple)):
raise ValueError("Size must be a tuple") raise ValueError("Size must be a tuple")
if len(size) != 2: if len(size) != 2:
raise ValueError("Size must be a tuple of length 2") raise ValueError("Size must be a tuple of length 2")
@ -2019,7 +2019,7 @@ def new(mode, size, color=0):
""" """
_check_size(size) _check_size(size)
if color is None: if color is None:
# don't initialize # don't initialize
return Image()._new(core.new(mode, size)) return Image()._new(core.new(mode, size))

View File

@ -238,7 +238,7 @@ class TestImage(PillowTestCase):
self.assert_image_similar(im2, im3, 110) self.assert_image_similar(im2, im3, 110)
def test_check_size(self): def test_check_size(self):
# Checking that the _check_size function throws value errors when we want it to. # Checking that the _check_size function throws value errors when we want it to.
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
Image.new('RGB', 0) # not a tuple Image.new('RGB', 0) # not a tuple
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
@ -247,19 +247,21 @@ class TestImage(PillowTestCase):
Image.new('RGB', (0,0)) # w,h <= 0 Image.new('RGB', (0,0)) # w,h <= 0
self.assertTrue(Image.new('RGB', (1,1))) self.assertTrue(Image.new('RGB', (1,1)))
# Should pass lists too
self.assertTrue(Image.new('RGB', [1,1]))
def test_storage_neg(self): def test_storage_neg(self):
# Storage.c accepted negative values for xsize, ysize. Was # Storage.c accepted negative values for xsize, ysize. Was
# test_neg_ppm, but the core function for that has been # test_neg_ppm, but the core function for that has been
# removed Calling directly into core to test the error in # removed Calling directly into core to test the error in
# Storage.c, rather than the size check above # Storage.c, rather than the size check above
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
Image.core.fill('RGB', (2,-2), (0,0,0)) Image.core.fill('RGB', (2,-2), (0,0,0))
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()