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:

  1. Shuffle 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
  2. 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:

How to do it...

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()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset