Search code examples
pythonnumpyipythonimshow

iPython imshow() or numpy row selection


I'm trying to plot, using imshow(), only the middle hundred rows of an image I have. I was wondering if there are any numpy commands that can slice only the middle hundred rows of my image's array. If not, can I use some variation on imshow() itself to be able to select and show only the middle hundred rows?


Solution

  • What you are looking for is :

    pic[np.shape(pic)[0]/2-50:np.shape(pic)[0]/2+50,np.shape(pic)[1]/2-50:np.shape(pic)[1]/2+50]
    

    example code:

    import numpy as np
    import matplotlib.pyplot as plt
    
    pic = np.random.rand(300,300)
    fig1 = plt.figure()
    fig1.suptitle('Full image')
    plt.imshow(pic)
    cropped = pic[np.shape(pic)[0]/2-50:np.shape(pic)[0]/2+50,np.shape(pic)[1]/2-50:np.shape(pic)[1]/2+50]
    fig2 = plt.figure()
    fig2.suptitle('middle 100 rows and column cropped')
    plt.imshow(cropped)
    plt.show()
    

    Result:

    enter image description here enter image description here