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
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.
The only way that it would be possible to read only a portion of the file would be if it were in a columnar format and partitioned on disk both horizontally (rows) and vertically (columns). Hive , and Hadoop provide such mechanisms (and Spark supports them). But those are for more datastores and not for individual (image) files.
So the answer from tmdavison
is correct - and maybe this provides some better feel for why that is.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.