2

I need to load image *.tif for process it in GDAL. I use this code:

data = gdal.Open("a.tif", gdal.GA_ReadOnly)
img = data.ReadAsArray()

All works well on small images (~10Mb). But when i try to load bigger image (~1Gb) it start to use a lot of memory (~15Gb RAM). How I can load this image by a chunks for sequential processing?

Thanks!

1

1 Answer 1

6

ReadAsArray() has some optional parameters to read portions of an image.

ReadAsArray(x_off, y_off, x_size, y_size)

Full code:

import gdal
ds = gdal.Open('input.tif', gdal.GA_ReadOnly)
rb = ds.GetRasterBand(1)
xsize = rb.XSize
ysize = rb.YSize
ystep = ysize / 10
yresidual = ysize - (ystep * 10)

for i in range(10):
    if i != 9:
        img_part = rb.ReadAsArray(0, ystep * i, xsize, ystep)
    else:
        img_part = rb.ReadAsArray(0, ystep * i, xsize, ystep + yresidual)
    # do something with img_part

ds = None
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.