[Numpy-discussion] dealing with RGB images

Alex Flint alex.flint@gmail....
Tue Aug 9 16:23:36 CDT 2011

Until now, I've been representing RGB images in numpy using MxNx3 arrays, as
returned by scipy.misc.imread and friends. However, when performing image
transformations, the color dimension is semantically different from the
spatial dimensions. For example, I would like to write an image scaling
function that works for both grayscale array (MxN) and RGB images (MxNx3):

def imscale(image, scale):
  return scipy.ndimage.zoom(imscale, scale)

But this will apply scaling along the color dimension, resulting in an image
with more/less image channels. So I do:

def imscale(image, scale)
  if image.ndim == 2:
    return scipy.ndimage.zoom(imscale, scale)
    return scipy.ndimage.zoom(imscale, (scale[0], scale[1], 1))

But now this fails if the scale argument is a scalar. It is possible to
cover all cases but all my functions are become case nighmares as the
combinations of RGB and scalar images multiply.

I am thinking of writing an RGB class with overrides for all the math
operations that make sense (addition, scalar multiplication), and then
creating arrays with dtype=RGB. This will mean that color images always have
ndim=2. Does this make sense? Is there a neater way to achieve this within

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110809/38f894d7/attachment.html 

More information about the NumPy-Discussion mailing list