Indexing with a list of locations
Let's use the ix_()
function to shuffle the Lena image. This function creates a mesh from multiple sequences. As arguments, we give one-dimensional sequences, and the function returns a tuple of NumPy arrays. For example, check the following code snippet:
In : ix_([0,1], [2,3]) Out: (array([[0], [1]]), array([[2, 3]]))
To index the array with a list of locations, perform the following steps:
Shuffle the array indices. Create a random indices array with the
shuffle()
function of thenumpy.random
module, as shown in the following lines of code. The function changes the arrayinplace
by the way.def shuffle_indices(size): arr = np.arange(size) np.random.shuffle(arr) return arr
Now plot the shuffled indices as follows:
plt.imshow(lena[np.ix_(xindices, yindices)])
What we get is a completely scrambled Lena, as shown in the following image:
The following code for this section is without comments. The complete code for this can be found in the ix.py
file in the...