# [Numpy-discussion] dealing with RGB images

Nathaniel Smith njs@pobox....
Tue Aug 9 18:06:56 CDT 2011

```1) Have you considered using MxNx1 arrays for greyscale images, so all
images have the same dimensionality?

2) Instead of defining an RGB class from scratch, would a structured dtype
do what you want?

- Nathaniel
On Aug 9, 2011 2:23 PM, "Alex Flint" <alex.flint@gmail.com> wrote:
> 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)
> else:
> 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
> numpy?
>
> Alex
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.scipy.org/pipermail/numpy-discussion/attachments/20110809/2297c3c5/attachment.html
```