Resize RGB image?

A

Alasdair McAndrew

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?

Thanks,
Alasdair
 
D

Dave Angel

I can resize a 2d image "im" with a command something like:

r,c = shape(im)
im2 = resize(im,(r//2,c//2))

You're missing at least one import there.

So how about you start by telling us what non-standard libraries you're
using, what version of python you're using, and what OS ? Then supply
us a working set of code, in the sense that people can see the problem
you're complaining about.
However, resize doesn't seem to work with an RGB image:

r,c,n = shape(im) # returns, say 500 600 3

Doesn't work is pretty vague. It could mean anything from "it makes the
image too small by 1%" to "it crashes my system after reformatting my
hard disk." If you get an error, include the entire traceback here. If
it doesn't work in some other sense,

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?

Thanks,
Alasdair
 
D

Dave Angel

On 11/24/2012 06:37 AM, Dave Angel wrote:

Oops, I sent before I was done editing. Corrections below.
You're missing at least one import there.

So how about you start by telling us what non-standard libraries you're
using, what version of python you're using, and what OS ? Then supply
us a working set of code, in the sense that people can see the problem
you're complaining about.

Doesn't work is pretty vague. It could mean anything from "it makes the
image too small by 1%" to "it crashes my system after reformatting my
hard disk." If you get an error, include the entire traceback here. If
it doesn't work in some other sense,

then tell us what you expected, and what it did wrong. In this case,
you might have to supply us with a link to sample data that demonstrates
the problem.
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?

Thanks,
Alasdair
 
I

Ian Kelly

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.
 
A

Alasdair McAndrew

Thank you for your speedy response! And yes, I was too hasty in firing off a request without the necessary details.

And in fact the resize from the PIL Image module does work:

f = Image.open('bird-of-paradise-flower-1.jpg')
r,c = f.size
f2 = f.resize((r//2,c//2))
f2.show()

shape(f) # returns(768, 1024, 3)

shape(f2) # returns (384, 512, 3)

I think I was confused about all the different "resize" methods floating about. Clearly I had been using the wrong one! But if f is defined - as I've just done - as an Image image, then it will use the correct resize method.

I'll crawl back under my rock now.

-Alasdair
 
A

Alasdair McAndrew

Thank you for your speedy response! And yes, I was too hasty in firing off a request without the necessary details.

And in fact the resize from the PIL Image module does work:

f = Image.open('bird-of-paradise-flower-1.jpg')
r,c = f.size
f2 = f.resize((r//2,c//2))
f2.show()

shape(f) # returns(768, 1024, 3)

shape(f2) # returns (384, 512, 3)

I think I was confused about all the different "resize" methods floating about. Clearly I had been using the wrong one! But if f is defined - as I've just done - as an Image image, then it will use the correct resize method.

I'll crawl back under my rock now.

-Alasdair
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top