简体   繁体   中英

Load image by chunks in Python for GDAL processing

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!

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

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM