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 the numpy.random
module:
def shuffle_indices(size): arr = numpy.arange(size) numpy.random.shuffle(arr) return arr
matplotlib.pyplot.imshow(lena[numpy.ix_(xindices, yindices)])
What we get is a completely scrambled Lena image, as shown in the following screenshot:
The following is the complete code for the recipe:
import scipy.misc import matplotlib.pyplot import numpy.random import numpy.testing # Load the Lena array lena = scipy.misc.lena() xmax = lena.shape[0] ymax = lena.shape[1] def shuffle_indices(size): arr = numpy.arange(size) numpy.random.shuffle(arr) return arr xindices = shuffle_indices(xmax) numpy.testing.assert_equal(len(xindices), xmax) yindices = shuffle_indices(ymax) numpy.testing.assert_equal(len(yindices), ymax) # Plot Lena matplotlib.pyplot.imshow(lena[numpy.ix_(xindices, yindices)]) matplotlib.pyplot.show()