I want to read large image. The image size is 92117 x 30313 pixels. So far I know cv2.imread
can not read large size.
How can I read using GDAL API?
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.
Sign up to join this communityI want to read large image. The image size is 92117 x 30313 pixels. So far I know cv2.imread
can not read large size.
How can I read using GDAL API?
To read an image using gdal
as a numpy
array you can use the following two lines:
ds = gdal.Open('path_to_img', 0)
arr = ds.ReadAsArray()
However, you can run into a MemoryError
if your image is too large. Depending on your needs, you can read one chunk of the image at the time. The ReadAsArray()
method can take four useful arguments to accomplish this:
xoff
: offset to start reading the columnsyoff
: offset to start reading the rowswin_xsize
: number of columns to readwin_ysize
: number of rows to readYou can use two for
loops to read the data in chunks. For example:
ds = gdal.Open('path_to_img', 0)
cols = ds.RasterXSize
rows = ds.RasterYSize
win_xsize = 100 # arbitrary value
win_ysize = 100 # arbitrary value
for i in range(0, rows, win_ysize):
for j in range(0, cols, win_xsize):
arr = ds.ReadAsArray(xoff=j, yoff=i, win_xsize=win_xsize, win_ysize=win_ysize)