2009/2/17 Gael Varoquaux <gael.varoqu...@normalesup.org>:
> That's handy, you should commit this somewhere. Actually, it would be
> even cooler if you could have different zoom factor in different
> direction :).

Something like this:

a = np.array([[1, 2, 3],
              [4, 5, 6]])
print a
print zoom(a, x=2, y=3)

[[1 2 3]
 [4 5 6]]
[[1 1 2 2 3 3]
 [1 1 2 2 3 3]
 [1 1 2 2 3 3]
 [4 4 5 5 6 6]
 [4 4 5 5 6 6]
 [4 4 5 5 6 6]]

(Code attached)

Cheers
Stéfan
import numpy as np

def zoom(arr, x=2, y=None):
        """Nearest neighbour zoom.

        Examples
        --------
        >>> a = np.array([[1, 2],
                          [3, 4]])
        >>> zoom(a, 2)
        array([[1, 1, 2, 2],
               [1, 1, 2, 2],
               [3, 3, 4, 4],
               [3, 3, 4, 4]])

        """
        if y is None:
                y = x

        rows, cols = arr.shape
        row_stride, col_stride = arr.strides
        view = np.lib.stride_tricks.as_strided(arr,
                        (rows, y, cols, x),
                        (row_stride, 0, col_stride, 0))
        return view.reshape((rows*y, cols*x))

a = np.array([[1, 2, 3],
              [4, 5, 6]])
print a
print zoom(a, x=2, y=3)

_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to