From 0002e18c74c66791b0b7a14fc4903fafd52ce8ec Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 9 Aug 2017 01:58:22 +0300 Subject: [PATCH] New Image.getchannel method --- PIL/Image.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/PIL/Image.py b/PIL/Image.py index 49c9f6ab2..f67009452 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -1959,6 +1959,22 @@ class Image(object): ims.append(self._new(self.im.getband(i))) return tuple(ims) + def getchannel(self, channel): + """ + Returns an image contained single channel of the source image. + + :param channel: What channel to return. Could be index + (0 for "R" channel of "RGB") or channel name + ("A" for alpha channel of "RGBA"). + :returns: An image in "L" mode. + """ + + if isStringType(channel) and len(channel) == 1: + if channel in self.im.mode: + channel = self.im.mode.index(channel) + + return self._new(self.im.getband(channel)) + def tell(self): """ Returns the current frame number. See :py:meth:`~PIL.Image.Image.seek`.