Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Image.getchannel method #2661

Merged
merged 8 commits into from Aug 16, 2017
Merged

Conversation

@homm
Copy link
Member

homm commented Aug 8, 2017

Sometimes you need to get just one channel from the image. Of course, there is split() method which will return all bands but this is extra computations. Also Image.getchannel() method can accept channel name alongside with index.

Benchmarks:

im.split()[band]
    get R channel of RGB        0.00834 s   491.18 Mpx/s
    get A channel of RGBA       0.01109 s   369.21 Mpx/s

im.getchannel(band)
    get R channel of RGB        0.00287 s  1429.15 Mpx/s
    get A channel of RGBA       0.00287 s  1429.15 Mpx/s
homm added 3 commits Aug 8, 2017
@hugovk

This comment has been minimized.

Copy link
Member

hugovk commented Aug 9, 2017

Looks good.

Would be good to add this to release notes and .. automethod:: PIL.Image.Image.getchannel to Image.rst. Also .. versionadded:: x.y.z to the docstring.

And maybe a note in Image.split's docstring to say getchannel is available/quicker?

@homm homm added this to the 4.3.0 milestone Aug 9, 2017
autodocs
fix docstring
note for `Image.split`
@homm

This comment has been minimized.

Copy link
Member Author

homm commented Aug 11, 2017

@hugovk All done

@@ -1959,6 +1962,25 @@ def split(self):
ims.append(self._new(self.im.getband(i)))
return tuple(ims)

def getchannel(self, channel):

This comment has been minimized.

Copy link
@homm

homm Aug 11, 2017

Author Member

Is it proper name? Other option is getband (personally, I don't like)

This comment has been minimized.

Copy link
@wiredfool

wiredfool Aug 16, 2017

Member

We use band elsewhere for just about everything related to channels, but channel is probably a more accepted name in the general graphics world.

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

This comment has been minimized.

Copy link
@homm

homm Aug 11, 2017

Author Member

Do we need negative indexing? (-1 for alpha channel in "RGBA" and "K" in "CMYK")

@@ -1947,6 +1947,9 @@ def split(self):
containing a copy of one of the original bands (red, green,
blue).
If you need only one band, :py:meth:`~PIL.Image.Image.getchannel`
method can be more convinient and faster.

This comment has been minimized.

Copy link
@radarhere

radarhere Aug 12, 2017

Member

convenient

@@ -1959,6 +1962,25 @@ def split(self):
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.

This comment has been minimized.

Copy link
@radarhere

radarhere Aug 12, 2017

Member

'Returns an image containing a single channel'?

@homm homm removed the Needs Tests label Aug 12, 2017
@homm

This comment has been minimized.

Copy link
Member Author

homm commented Aug 12, 2017

Tests added, channel naming fixed

@homm homm requested review from hugovk and radarhere Aug 12, 2017
-----

Get One Channel From Image
============================

This comment has been minimized.

Copy link
@radarhere

radarhere Aug 12, 2017

Member

The number of equals should match the number of characters in the heading

This comment has been minimized.

Copy link
@homm

homm Aug 12, 2017

Author Member

"number of equals should equal"
:-)

This comment has been minimized.

Copy link
@radarhere

radarhere Aug 12, 2017

Member

Yes, I know :p I edited my comment.

This comment has been minimized.

Copy link
@homm

homm Aug 12, 2017

Author Member

Done )

@wiredfool wiredfool merged commit e260245 into python-pillow:master Aug 16, 2017
5 checks passed
5 checks passed
codecov/patch 100% of diff hit (target 81.67%)
Details
codecov/project 81.68% (+0.01%) compared to 5f5ac09
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
coverage/coveralls Coverage increased (+0.01%) to 81.683%
Details
@homm homm deleted the uploadcare:get-channel branch Aug 16, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.