Viewing Data Results a Step at a Time

By John Paul Mueller, Luca Massaron

Sometimes you need to take an application that you intend to create apart one step at a time to see how it really works and where you might be going wrong. The code should work, but it doesn’t work the way you thought it would. Viewing the intermediate results can make a huge difference in the outcome of the tasks you perform.

The following steps show how to use IPython Notebook in this fashion.

  1. Open your copy of IPython Notebook and create a new notebook.

    You see a blank notebook displayed.

    image0.jpg

  2. Type the following code into the first cell and click Run Cell.

    from skimage.io import imread
    from skimage.transform import resize 
    from matplotlib import pyplot as plt
    import matplotlib.cm as cm

    IPython Notebook verifies that all your imports are correct. These imports help you work with images so that you can perform tasks such as resizing them.

    image1.jpg

  3. Type the following code into the next cell and click Run Cell.

    example_file = (
        "http://blog.johnmuellerbooks.com/" +
        "wp-content/uploads/2015/04/Layer-Hens.jpg")
    image = imread(example_file, as_grey=True)
    print image

    You see that image does indeed contain an array of values from example_file. At this point, you have downloaded an image from an online source and can manipulate it as needed.

    image2.jpg

  4. Type the following code into the next cell and click Run Cell.

    image2 = resize(image, (50, 50), mode='nearest')
    print image2

    The array in image2 contains a resized version of the original image.

    image3.jpg

  5. Type the following code into the next cell and click Run Cell.

    %matplotlib inline
    plt.imshow(image, cmap=cm.gray)
    plt.show()

    The original color image is resized and presented in shades of gray. At this point, you could perform analysis on it or manipulate the image further, as needed.

    image4.jpg