display image as (r, g, b) from a (3xMxN) nympy.array

Hi All,

i’ve data store in a multydimension numpy.array,

it is composed by 3 MxN normalized (0 - 1) array

but tring to display it using “imshow” i get an error,

" TypeError: Invalid dimensions for image data "

please can you help me to fix this problem ?

my data is :

In [119]: dd[0]
Out[119]:
array([[[ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        ...,
        [ 0.22834646,  0.39370079,  0.38976378, ...,  0.2519685 ,
          0.25590551,  0.38582677],
        [ 0.30708661,  0.25984252,  0.30314961, ...,  0.2480315 ,
          0.25984252,  0.37401575],
        [ 0.58661417,  0.62598425,  0.5984252 , ...,  0.2480315 ,
          0.2519685 ,  0.32283465]],

       [[ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        ...,
        [ 0.17322835,  0.37401575,  0.4015748 , ...,  0.27952756,
          0.27559055,  0.42519685],
        [ 0.30314961,  0.24409449,  0.30314961, ...,  0.26771654,
          0.27165354,  0.38976378],
        [ 0.57086614,  0.61811024,  0.5984252 , ...,  0.28346457,
          0.26771654,  0.34251969]],

       [[ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        [ 1.        ,  1.        ,  1.        , ...,  1.        ,
          1.        ,  1.        ],
        ...,
        [ 0.19685039,  0.34251969,  0.37795276, ...,  0.2519685 ,
          0.25984252,  0.34251969],
        [ 0.27165354,  0.21653543,  0.27559055, ...,  0.2519685 ,
          0.26377953,  0.30708661],
        [ 0.57480315,  0.63385827,  0.58661417, ...,  0.26377953,
          0.25590551,  0.29527559]]])
In [120]: len(dd[0])
Out[120]: 3

In [121]: dd[0].shape
Out[121]: (3, 2058, 2607)

In [122]: matplotlib.pyplot.imshow(dd[0])

<details class='elided'>
<summary title='Show trimmed content'>&#183;&#183;&#183;</summary>

------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/matplotlib/pyplot.py", line 2035, in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax, origin, extent, shape, filternorm, filterrad, imlim, resample, url, **kwargs)
  File "/Library/Python/2.6/site-packages/matplotlib/axes.py", line 6272, in imshow
    im.set_data(X)
  File "/Library/Python/2.6/site-packages/matplotlib/image.py", line 372, in set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data

thanks for any help!!!

Massimo.

Hi All,

i've data store in a multydimension numpy.array,
it is composed by 3 MxN normalized (0 - 1) array
but tring to display it using "imshow" i get an error,
" TypeError: Invalid dimensions for image data "
please can you help me to fix this problem ?
my data is :
In [120]: len(dd[0])
Out[120]: 3

In [121]: dd[0].shape
Out[121]: (3, 2058, 2607)

In [122]: matplotlib.pyplot.imshow(dd[0])
------------------------------------------------------------
Traceback (most recent call last):
  File "<ipython console>", line 1, in <module>
  File "/Library/Python/2.6/site-packages/matplotlib/pyplot.py", line 2035,
in imshow
    ret = ax.imshow(X, cmap, norm, aspect, interpolation, alpha, vmin, vmax,
origin, extent, shape, filternorm, filterrad, imlim, resample, url,
**kwargs)
  File "/Library/Python/2.6/site-packages/matplotlib/axes.py", line 6272, in
imshow
    im.set_data(X)
  File "/Library/Python/2.6/site-packages/matplotlib/image.py", line 372, in
set_data
    raise TypeError("Invalid dimensions for image data")
TypeError: Invalid dimensions for image data

Your image data is 3xMxN, but according to the imshow docstring:

" MxNx3 -- RGB (float or uint8 array)"

So a quick way is to use the transpose do:

matplotlib.pyplot.imshow(dd[0].transpose())

However, this will rotate the image (result will have N rows and M
columns), if you're expecting it to have M rows and N columns. In that
case, you need to "roll" the axis to the end, which will end up with a
3xMxN array:

dd[0].shape

(3, 2058, 2607)

data2 = np.rollaxis(dd[0], 0, 3)
data2.shape

(2058, 2607, 3)
plt.imshow(data2)

Ryan

···

On Sat, Feb 27, 2010 at 11:04 PM, Massimo Di Stefano <massimodisasha@...174...> wrote:

--
Ryan May
Graduate Research Assistant
School of Meteorology
University of Oklahoma