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?

1 Answer 1


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 columns
  • yoff: offset to start reading the rows
  • win_xsize: number of columns to read
  • win_ysize: number of rows to read

You 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)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.