Let's use the ix_()
function to shuffle the Lena image. This function creates a mesh from multiple sequences.
We will start by randomly shuffling the array indices:
Create a random indices array with the
shuffle()
function of thenumpy.random
module:def shuffle_indices(size): arr = np.arange(size) np.random.shuffle(arr) return arr
Plot the shuffled indices:
plt.imshow(lena[np.ix_(xindices, yindices)])
What we get is a completely scrambled Lena image, as shown in the following screenshot:
Here is the complete code for the recipe from the
ix.py
file in this book's code bundle:import scipy.misc import matplotlib.pyplot as plt import numpy as np # Load the Lena array lena = scipy.misc.lena() xmax = lena.shape[0] ymax = lena.shape[1] def shuffle_indices(size): ''' Shuffles an array with values 0 - size ''' arr = np.arange(size) np.random.shuffle(arr) return arr xindices = shuffle_indices(xmax) np.testing.assert_equal...