[SciPy-User] Convert data to image and then image to numpy.ndarray

Robert Kern robert.kern at gmail.com
Thu Apr 27 00:55:54 EDT 2017


On Wed, Apr 26, 2017 at 8:20 PM, Carlton Banks <noflaco at gmail.com> wrote:
>
> Is it possible to convert a data set stored as a numpy.ndarray -> plot
the data with a colormap , possibly cm.jet.  -> and then store the image
pixels to a numpy.ndarray.
>
> The data array of the image should have the same shape of the data array
used to make the array.
> The image conversion is only used for getting a visual representation of
the data, and normalise it using the cm.jet.
> So I actually just interested in the RGB values of the colormap
normalised input data.
>
> IS this possible?

Sure, you can use matplotlib's colormap objects as functions to convert
arrays with values in the interval [0.0, 1.0] to arrays of colors.

[~]
|6> cm.viridis(np.linspace(0.0, 1.0, 5))

array([[ 0.267004,  0.004874,  0.329415,  1.      ],
       [ 0.229739,  0.322361,  0.545706,  1.      ],
       [ 0.127568,  0.566949,  0.550556,  1.      ],
       [ 0.369214,  0.788888,  0.382914,  1.      ],
       [ 0.993248,  0.906157,  0.143936,  1.      ]])

[~]
|7> cm.viridis?
...
Call signature: cm.viridis(X, alpha=None, bytes=False)
Call docstring:
Parameters
----------
X : scalar, ndarray
    The data value(s) to convert to RGBA.
    For floats, X should be in the interval ``[0.0, 1.0]`` to
    return the RGBA values ``X*100`` percent along the Colormap line.
    For integers, X should be in the interval ``[0, Colormap.N)`` to
    return RGBA values *indexed* from the Colormap with index ``X``.
alpha : float, None
    Alpha must be a scalar between 0 and 1, or None.
bytes : bool
    If False (default), the returned RGBA values will be floats in the
    interval ``[0, 1]`` otherwise they will be uint8s in the interval
    ``[0, 255]``.

Returns
-------
Tuple of RGBA values if X is scalar, othewise an array of
RGBA values with a shape of ``X.shape + (4, )``.

--
Robert Kern
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scipy-user/attachments/20170426/f443ed96/attachment.html>


More information about the SciPy-User mailing list