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.
How to do it...
We will start by randomly shuffling the array indices:
Shuffle array indices.
Create a random indices array with the
shuffle
function of thenumpy.random
module:def shuffle_indices(size): arr = numpy.arange(size) numpy.random.shuffle(arr) return arr
Plot the shuffled indices:
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...