python matplotlib.pyplot imread

14,899

I think you have to read the entire image, after which you can slice it before you do any processing on it:

import matplotlib.pyplot as plt
my_img = plt.imread('my_img.tiff')
my_clipped_img = my_img[s1:s2,r1:r2]

or, in one line:

import matplotlib.pyplot as plt
my_img = plt.imread('my_img.tiff')[s1:s2,r1:r2]

The latter has the benefit of not creating a full sized array, but just of the size you want.

Bear in mind that s1:s2 here should be your limits in the vertical direction, and r1:r2 in the horizontal direction.

Share:
14,899
Admin
Author by

Admin

Updated on June 28, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm using plt.imread for reading big .tiff images. Because of the big dimensions, I would like to select just a part of the image to be loaded. I would like to do something like:

    plt.imread(filename, [s1:s2, r1:r2])
    

    choosing the initial and final pixel for both dimensions.

    Is there a way to do this?

    Many thanks