add tests, fix implementation

This commit is contained in:
Alexander 2017-08-12 14:10:39 +03:00
parent 04ebc23939
commit e16ab0ad2e
2 changed files with 25 additions and 8 deletions

View File

@ -1975,9 +1975,12 @@ class Image(object):
"""
self.load()
if isStringType(channel) and len(channel) == 1:
if channel in self.im.mode:
channel = self.im.mode.index(channel)
if isStringType(channel):
try:
channel = self.getbands().index(channel)
except ValueError:
raise ValueError(
'The image has no channel "{}"'.format(channel))
return self._new(self.im.getband(channel))

View File

@ -145,14 +145,28 @@ class TestImage(PillowTestCase):
self.assertEqual(im.size[1], orig_size[1] + 2*ymargin)
def test_getbands(self):
# Arrange
# Assert
self.assertEqual(hopper('RGB').getbands(), ('R', 'G', 'B'))
self.assertEqual(hopper('YCbCr').getbands(), ('Y', 'Cb', 'Cr'))
def test_getchannel_wrong_params(self):
im = hopper()
# Act
bands = im.getbands()
self.assertRaises(ValueError, im.getchannel, -1)
self.assertRaises(ValueError, im.getchannel, 3)
self.assertRaises(ValueError, im.getchannel, 'Z')
self.assertRaises(ValueError, im.getchannel, '1')
# Assert
self.assertEqual(bands, ('R', 'G', 'B'))
def test_getchannel(self):
im = hopper('YCbCr')
Y, Cb, Cr = im.split()
self.assert_image_equal(Y, im.getchannel(0))
self.assert_image_equal(Y, im.getchannel('Y'))
self.assert_image_equal(Cb, im.getchannel(1))
self.assert_image_equal(Cb, im.getchannel('Cb'))
self.assert_image_equal(Cr, im.getchannel(2))
self.assert_image_equal(Cr, im.getchannel('Cr'))
def test_getbbox(self):
# Arrange