Skip to content

Commit

Permalink
ENH: Built-in as_grey pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
nkeim committed Jul 2, 2018
1 parent d45bedb commit dcc9597
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
16 changes: 14 additions & 2 deletions doc/source/opening_files.rst
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
Opening Files
=============

QuickStart
----------
Quick Start
-----------

Where possible, pims detects the type of file(s) automatically. Here are some
examples.
Expand All @@ -14,6 +14,18 @@ examples.
images = pims.open('my_directory/*.tif') # many TIFs with sequential names
images = pims.open('tiff_stack.tif') # one TIF file containing many frames
If your images are in a color format, but you need to convert them to greyscale
for processing, you can use ``pims.as_grey``:

.. code-block:: python
import pims
images = pims.as_grey(pims.open('my_directory/*.png'))
``as_grey`` operates on any PIMS reader object, and it only converts images as
they are loaded. PIMS makes it easy to create your own custom functions like
this, called :doc:`pipelines`.

Using Specific Readers
----------------------

Expand Down
6 changes: 4 additions & 2 deletions doc/source/pipelines.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ underlying video data is only accessed one element at a time.
Conversion to greyscale
-----------------------

Say we want to convert an RGB video to greyscale. We define a function as
follows and decorate it with ``@pipeline`` to turn it into a pipeline:
Say we want to convert an RGB video to greyscale. A pipeline to do this is
already provided as ``pims.as_grey``, but it is also easy to make our own.
We define a function as follows and decorate it with ``@pipeline`` to turn
it into a pipeline:

.. code-block:: python
Expand Down
1 change: 1 addition & 0 deletions pims/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from .norpix_reader import NorpixSeq # noqa
from pims.tiff_stack import TiffStack_tifffile # noqa
from .spe_stack import SpeStack
from pims.process import as_grey


def not_available(requirement):
Expand Down
25 changes: 25 additions & 0 deletions pims/process.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from __future__ import (absolute_import, division, print_function,
unicode_literals)

import six

import numpy as np

from slicerator import pipeline

@pipeline
def as_grey(frame):
"""Convert a 2D image or PIMS reader to greyscale.
This weights the color channels according to their typical
response to white light.
It does nothing if the input is already greyscale.
"""
if len(frame.shape) == 2:
return frame
else:
red = frame[:, :, 0]
green = frame[:, :, 1]
blue = frame[:, :, 2]
return 0.2125 * red + 0.7154 * green + 0.0721 * blue
8 changes: 8 additions & 0 deletions pims/tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ def _color_channel(img, channel):
else:
return img


class _image_series(_image_single):
def test_iterator(self):
self.check_skip()
Expand Down Expand Up @@ -332,6 +333,13 @@ def test_composed_pipelines(self):
expected = _rescale(_color_channel(self.v[0], 0))
assert_image_equal(composed[0], expected)

def test_as_grey(self):
gr = pims.as_grey(self.v)
assert len(gr[0].shape) == 2
# Calling a second time does nothing
gr2 = pims.as_grey(gr)
assert_image_equal(gr[0], gr2[0])

def test_getting_single_frame(self):
self.check_skip()
assert_image_equal(self.v[0], self.frame0)
Expand Down

0 comments on commit dcc9597

Please sign in to comment.