Resize RGB image?

Ian Kelly ian.g.kelly at gmail.com
Sat Nov 24 13:04:57 CET 2012


On Sat, Nov 24, 2012 at 4:14 AM, Alasdair McAndrew <amca01 at gmail.com> wrote:
> I can resize a 2d image "im" with a command something like:
>
> r,c = shape(im)
> im2 = resize(im,(r//2,c//2))
>
> However, resize doesn't seem to work with an RGB image:
>
> r,c,n = shape(im)  # returns, say 500 600 3
>
> I then need to take each band separately, resize it, and put them all back together with dstack:
>
> imr = im[:,:,0]
> img = im[:,:,1]
> imb = im[:,:,2]
> imr2 = resize(imr,(r//2,c//2))
> img2 = resize(img,(r//2,c//2))
> imb2 = resize(imb,(r//2,c//2))
> im2 = dstack((imr2,img2,imb2))
>
> This works fine, but seems a little clumsy.  Of course this could be done in one command:
>
> im2 = dstack([resize(im[:,:,i],(r//2,c//2)) for i in range(3)])
>
> What I want to know is: is there a version of resize which can be applied directly to multi-band images, without having to apply to each band separately?

You appear to be using numpy for this, which is a numerical library,
not specifically an image-processing library.  I would suggest using a
library more suited for the task, such as PIL.

I don't think that resize calls above are doing what you want in any
case.  They're not going to do scaling with interpolation, as is
usually intended when resizing an image.  They're just taking as much
of the data as will fit (i.e., the first (r//2 * c//2) bytes) and
packing it into the new array in the requested shape.


More information about the Python-list mailing list