In the package numpy their are two function resize and reshape. How internally they work? What kind of interpolation does they use ?I looked into the code, but didnt get it. Can anyone help me out. Or how does an image get resized. What happens with its pixels ?
-
from the docs:If the new array is larger than the original array, then the new array is filled with repeated copies of a. https://docs.scipy.org/doc/numpy/reference/generated/numpy.resize.html – OMRY VOLK Dec 15 '16 at 19:28
-
Neither `np.resize` nor `np.reshape` are at all suitable for image resizing. Numpy is about arrays, not images. – Eric Dec 15 '16 at 20:47
2 Answers
Neither interpolates. And if you are wondering about interpolation and pixels of an image, they probably aren't the functions that you want. There some image
packages (e.g in scipy
) that manipulate the resolution of images.
Every numpy
array has a shape
attribute. reshape
just changes that, without changing the data at all. The new shape has to reference the same total number of elements as the original shape.
x = np.arange(12)
x.reshape(3,4) # 12 elements
x.reshape(6,2) # still 12
x.reshape(6,4) # error
np.resize
is less commonly used, but is written in Python and available for study. You have to read its docs, and x.resize
is different. Going larger it actually repeats values or pads with zeros.
examples of resize acting in 1d:
In [366]: x=np.arange(12)
In [367]: np.resize(x,6)
Out[367]: array([0, 1, 2, 3, 4, 5])
In [368]: np.resize(x,24)
Out[368]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 1, 2, 3, 4,
5, 6, 7, 8, 9, 10, 11])
In [369]: x.resize(24)
In [370]: x
Out[370]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0])
A recent question about scipy.misc.imresize
. It also references scipy.ndimage.zoom
:
-
3_"reshape just changes [`.shape`]"_ - more accurately, it creates a view with that changed, rather than actually modifying the object it is called on – Eric Dec 16 '16 at 00:57
As far as I know numpy.reshape()
just reshapes a matrix (does not matter if it is an image or not). It does not do any interpolation and just manipulates the items in a matrix.
a = np.arange(12).reshape((2,6))
a= [[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]]
b=a.reshape((4,3))
b=[[ 0 1 2]
[ 3 4 5]
[ 6 7 8]
[ 9 10 11]]

- 21
- 2