3

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

Tonechas
  • 13,398
  • 16
  • 46
  • 80
user3284140
  • 71
  • 1
  • 1
  • 6
  • See related question http://stackoverflow.com/questions/23878165/python-opencv-only-read-part-of-image. – Mihai8 May 18 '15 at 15:52
  • This is possibly a duplicate of : http://stackoverflow.com/questions/19695249/load-just-part-of-an-image-in-python – OYRM May 18 '15 at 15:58

2 Answers2

6

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.

tmdavison
  • 64,360
  • 12
  • 187
  • 165
0

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.

WestCoastProjects
  • 58,982
  • 91
  • 316
  • 560