Esempio n. 1
0
    def __call__(self, projectables, **info):
        if len(projectables) != 2:
            raise ValueError("Expected 2 datasets, got %d" %
                             (len(projectables), ))

        # TODO: support datasets with palette to delegate this to the image
        # writer.

        data, palette = projectables
        palette = palette / 255.0

        from trollimage.colormap import Colormap
        if data.dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)
        elif 'valid_range' in data.info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)
            colormap.set_range(*data.info['valid_range'])
        r, g, b = colormap.colorize(data)
        r[data.mask] = palette[-1][0]
        g[data.mask] = palette[-1][1]
        b[data.mask] = palette[-1][2]
        r = Dataset(r, copy=False, mask=data.mask, **data.info)
        g = Dataset(g, copy=False, mask=data.mask, **data.info)
        b = Dataset(b, copy=False, mask=data.mask, **data.info)

        return super(PaletteCompositor, self).__call__((r, g, b), **data.info)
Esempio n. 2
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        tups = [(val, tuple(tup)) for (val, tup) in enumerate(palette)]
        colormap = Colormap(*tups)

        sf = info.get('scale_factor', np.array(1))
        colormap.set_range(*(np.array(info['valid_range']) * sf +
                             info.get('add_offset', 0)))

        return colormap
Esempio n. 3
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        tups = [(val, tuple(tup))
                for (val, tup) in enumerate(palette)]
        colormap = Colormap(*tups)

        sf = info.get('scale_factor', np.array(1))
        colormap.set_range(
            *(np.array(info['valid_range']) * sf + info.get('add_offset', 0)))

        return colormap
Esempio n. 4
0
    def build_colormap(palette, info):
        """Create the colormap from the `raw_palette` and the valid_range."""
        from trollimage.colormap import Colormap
        if 'palette_meanings' in palette.attrs:
            palette_indices = palette.attrs['palette_meanings']
        else:
            palette_indices = range(len(palette))

        squeezed_palette = np.asanyarray(palette).squeeze() / 255.0
        tups = [(val, tuple(tup))
                for (val, tup) in zip(palette_indices, squeezed_palette)]
        colormap = Colormap(*tups)
        if 'palette_meanings' not in palette.attrs:
            sf = info.get('scale_factor', np.array(1))
            colormap.set_range(*(np.array(info['valid_range']) * sf +
                                 info.get('add_offset', 0)))

        return colormap, squeezed_palette
Esempio n. 5
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap
        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

            sf = info['scale_factor']
            colormap.set_range(*info['valid_range'] * sf + info['add_offset'])

        return colormap
Esempio n. 6
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range.

        Colormaps come in different forms, but they are all supposed to have
        color values between 0 and 255. The following cases are considered:
        - Palettes comprised of only a list on colors. If *dtype* is uint8,
          the values of the colormap are the enumaration of the colors.
          Otherwise, the colormap values will be spread evenly from the min
          to the max of the valid_range provided in `info`.
        - Palettes that have a palette_meanings attribute. The palette meanings
          will be used as values of the colormap.

        """
        from trollimage.colormap import Colormap
        squeezed_palette = np.asanyarray(palette).squeeze() / 255.0
        set_range = True
        if hasattr(palette, 'attrs') and 'palette_meanings' in palette.attrs:
            set_range = False
            meanings = palette.attrs['palette_meanings']
            iterator = zip(meanings, squeezed_palette)
        else:
            iterator = enumerate(squeezed_palette[:-1])

        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in iterator]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in iterator]
            colormap = Colormap(*tups)

            if set_range:
                sf = info.get('scale_factor', np.array(1))
                colormap.set_range(
                    *(np.array(info['valid_range']) * sf
                      + info.get('add_offset', 0)))
        else:
            raise AttributeError("Data needs to have either a valid_range or be of type uint8" +
                                 " in order to be displayable with an attached color-palette!")

        return colormap, squeezed_palette
Esempio n. 7
0
    def build_colormap(palette, dtype, info):
        """Create the colormap from the `raw_palette` and the valid_range."""

        from trollimage.colormap import Colormap

        palette = np.asanyarray(palette).squeeze()
        if dtype == np.dtype('uint8'):
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

        elif 'valid_range' in info:
            tups = [(val, tuple(tup))
                    for (val, tup) in enumerate(palette[:-1])]
            colormap = Colormap(*tups)

            sf = info.get('scale_factor', np.array(1))
            colormap.set_range(
                *info['valid_range'] * sf + info.get('add_offset', 0))

        return colormap
Esempio n. 8
0
def create_colormap(palette):
    """Create colormap of the given numpy file, color vector, or colormap.

    Args:
        palette (dict): Information describing how to create a colormap
            object. See below for more details.

    **From a file**

    Colormaps can be loaded from ``.npy`` files as 2D raw arrays with rows for
    each color. The filename to load can be provided with the ``filename`` key
    in the provided palette information. The colormap is interpreted as 1 of 4
    different "colormap modes": ``RGB``, ``RGBA``, ``VRGB``, or ``VRGBA``. The
    colormap mode can be forced with the ``colormap_mode`` key in the provided
    palette information. If it is not provided then a default will be chosen
    based on the number of columns in the array (3: RGB, 4: VRGB, 5: VRGBA).

    The "V" in the possible colormap modes represents the control value of
    where that color should be applied. If "V" is not provided in the colormap
    data it defaults to the row index in the colormap array (0, 1, 2, ...)
    divided by the total number of colors to produce a number between 0 and 1.
    See the "Set Range" section below for more information.
    The remaining elements in the colormap array represent the Red (R),
    Green (G), and Blue (B) color to be mapped to.

    See the "Color Scale" section below for more information on the value
    range of provided numbers.

    **From a list**

    Colormaps can be loaded from lists of colors provided by the ``colors``
    key in the provided dictionary. Each element in the list represents a
    single color to be mapped to and can be 3 (RGB) or 4 (RGBA) elements long.
    By default the value or control point for a color is determined by the
    index in the list (0, 1, 2, ...) divided by the total number of colors
    to produce a number between 0 and 1. This can be overridden by providing a
    ``values`` key in the provided dictionary. See the "Set Range" section
    below for more information.

    See the "Color Scale" section below for more information on the value
    range of provided numbers.

    **From a builtin colormap**

    Colormaps can be loaded by name from the builtin colormaps in the
    ``trollimage``` package. Specify the name with the ``colors``
    key in the provided dictionary (ex. ``{'colors': 'blues'}``).
    See :doc:`trollimage:colormap` for the full list of available colormaps.

    **Color Scale**

    By default colors are expected to be in a 0-255 range. This
    can be overridden by specifying ``color_scale`` in the provided colormap
    information. A common alternative to 255 is ``1`` to specify floating
    point numbers between 0 and 1. The resulting Colormap uses the normalized
    color values (0-1).

    **Set Range**

    By default the control points or values of the Colormap are between 0 and
    1. This means that data values being mapped to a color must also be
    between 0 and 1. When this is not the case, the expected input range of
    the data can be used to configure the Colormap and change the control point
    values. To do this specify the input data range with ``min_value`` and
    ``max_value``. See :meth:`trollimage.colormap.Colormap.set_range` for more
    information.

    """
    from trollimage.colormap import Colormap
    fname = palette.get('filename', None)
    colors = palette.get('colors', None)
    # are colors between 0-255 or 0-1
    color_scale = palette.get('color_scale', 255)
    if fname:
        data = np.load(fname)
        cols = data.shape[1]
        default_modes = {3: 'RGB', 4: 'VRGB', 5: 'VRGBA'}
        default_mode = default_modes.get(cols)
        mode = palette.setdefault('colormap_mode', default_mode)
        if mode is None or len(mode) != cols:
            raise ValueError(
                "Unexpected colormap shape for mode '{}'".format(mode))

        rows = data.shape[0]
        if mode[0] == 'V':
            colors = data[:, 1:]
            if color_scale != 1:
                colors = data[:, 1:] / float(color_scale)
            values = data[:, 0]
        else:
            colors = data
            if color_scale != 1:
                colors = colors / float(color_scale)
            values = np.arange(rows) / float(rows - 1)
        cmap = Colormap(*zip(values, colors))
    elif isinstance(colors, (tuple, list)):
        cmap = []
        values = palette.get('values', None)
        for idx, color in enumerate(colors):
            if values is not None:
                value = values[idx]
            else:
                value = idx / float(len(colors) - 1)
            if color_scale != 1:
                color = tuple(elem / float(color_scale) for elem in color)
            cmap.append((value, tuple(color)))
        cmap = Colormap(*cmap)
    elif isinstance(colors, str):
        from trollimage import colormap
        import copy
        cmap = copy.copy(getattr(colormap, colors))
    else:
        raise ValueError("Unknown colormap format: {}".format(palette))

    if palette.get("reverse", False):
        cmap.reverse()
    if 'min_value' in palette and 'max_value' in palette:
        cmap.set_range(palette["min_value"], palette["max_value"])
    elif 'min_value' in palette or 'max_value' in palette:
        raise ValueError("Both 'min_value' and 'max_value' must be specified")

    return cmap