1

I am trying to split .tif image that i have downloaded from satellite imaginary. The .tif image is 114MB and has lot of data and i want to split the image into further small images to work more on the images.

The code I have return for that:

import os, gdal

in_path = 'C:/Users/naresh/'
input_filename = '20180831_181344_0f49_3B_AnalyticMS_DNN.tif'

out_path = 'C:/Users/naresh/output_folder/'
output_filename = 'tile_'

tile_size_x = 50
tile_size_y = 70

ds = gdal.Open(in_path + input_filename)
band = ds.GetRasterBand(1)
xsize = band.XSize
ysize = band.YSize

for i in range(0, xsize, tile_size_x):
for j in range(0, ysize, tile_size_y):
    com_string = "gdal_translate -of GTIFF -srcwin " + str(i)+ ", " + str(j) + ", " + str(tile_size_x) + ", " + str(tile_size_y) + " " + str(in_path) + str(input_filename) + " " + str(out_path) + str(output_filename) + str(i) + "_" + str(j) + ".tif"
    os.system(com_string)

While i am trying to execute this code i am getting error in AttributeError: 'NoneType' object has no attribute 'GetRasterBand' in band = ds.GetRasterBand(1) line.

  • This would be because ds is None, which is returned from gdal.Open if the dataset can't be opened, check the path and file name with os.path.exists(in_path + input_filename) then check ds != None before trying to grab a band from it. – Michael Stimson Aug 30 at 6:15

Your Answer

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

Browse other questions tagged or ask your own question.