I am trying to clip a raster image into a small image using coordinates in a CSV file. In the CSV file, I have minx,maxx,miny,maxy as the coordinates which makes the bounding box and clips the images.

I have a total of 100,000 coordinates and it will create 50,000 images. I am trying to number the images in ascending numbers. Example 0,1,2,3.... till 50,000. The problem is that when clipping it's escaping some images and only clipping 26,000 images. I just cannot find the bug in my code.

I thought it was the coordinate but when checking the coordinates it seems fine, the bug somehow is in my code.

import pandas as pd 
from osgeo import gdal

df = pd.read_csv("G:\\satellite\\align\\satellite.txt")
fs = gdal.Open('G:\\satellite\\align\\satellite.tif')

i, j = 0, 0

while True:
    minx = df.iloc[i][0]
    miny = df.iloc[i][1]
    maxx = df.iloc[i+1][0]
    maxy = df.iloc[i+1][1]
    out_tif = r"G:\\satellite\\training_images\\satellite_rename\\{0}.tif".format(j)
    ds = gdal.Translate(out_tif, fs, projWin = [minx, miny, maxx, maxy])
    j = j+1
    i += 2
    if i > 100000: # The iteration depends on how many co-ordinates you have
  • 2
    are your coordinates and rasters in the same projection? Jul 12, 2022 at 21:39
  • Yes i took the coordinates from point feature class and they are in the same projection
    – user123
    Jul 13, 2022 at 7:43
  • another question. have you stopped the process after one clip to view it? along with the coordinates. does the clip make sense? Jul 13, 2022 at 19:10

1 Answer 1


According to gdal python docs:

projWin --- subwindow in projected coordinates to extract: [ulx, uly, lrx, lry]

uly would be 'upper-left' which corresponds to a maximum for y. Try the following for your projWin:

projWin = [minx, maxy, maxx, miny]

It could also explain why your script stops making crops. If the raster is not square (they rarely are) then at some point in your iteration the coordinates may be out of bounds, so to speak, and thus no crop.

  • I tired this and it directly throws error. One weird thing is i also have other images and the same numbers are not being cropped.
    – user123
    Jul 18, 2022 at 11:54

Your Answer

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

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