Skip to main content

Splitting up large geoTIFF orthomosaics

Doodler can work with really large images, but it is usually best to keep your images < 10,000 pixels in any dimension, because then the program will do CRF inference on the whole image at once rather than in chunks. This usually results in better image segmentations that are more consistent with your doodles.

So, this post is all about how you make smaller image tiles from a very large geoTIFF format orthomosaic, using python. The smaller tiles will also be written out as image tiles, with their relative position in the larger image described in the file name, for easy reassembly

We'll need a dependency not included in the doodler environment: gdal

conda install gdal

Now, in python:

import os, gdal
from gdalconst import *
from glob import glob

How large do you want your output (square) image tiles to be? (in pixels)

tilesize = 5000

What images would you like to chop up?

bigfiles = [
'Sandwich/2017-01-09_Sandwich_5cm_ortho.tif',
'Sandwich/2017-02-14_Sandwich_5cm_ortho.tif',
'Sandwich/2017-03-16_Sandwich_5cm_ortho.tif',
'Sandwich/2018-01-10_Sandwich_5cm_ortho.tif',
]

List the widths and heights of those input bigfiles

widths = [13314, 13314, 13314, 19972]
heights = [6212, 6212, 6212, 9319]

Specify a new folder for each set of image tiles (one per big image)

folders = ['Sandwich/2017-01-09_5cm', 'Sandwich/2017-02-14_5cm',\
'Sandwich/2017-03-16_5cm','Sandwich/2017-01-10_5cm']

Make file name prefixes by borrowing the folder name:

prefixes = [f.split('/')[-1] for f in folders]

Finally, loop through each file, chop it into chunks using gdal_translate, called by an os.system() command. Then moves the tiles into their respective folders

for b,f,p in zip(bigfiles, folders, prefixes):
# chop the image into chunks
for i in range(0, widths[k], tilesize):
for j in range(0, heights[k], tilesize):
gdaltranString = "gdal_translate -of GTIFF -srcwin "+str(i)+", "+str(j)+", "+str(tilesize)+", " \
+str(tilesize)+" "+b+" "+p+"_"+str(i)+"_"+str(j)+".tif"
os.system(gdaltranString)
##move those chunks to a directory
os.mkdir(f)
os.system('mv '+p+'*.tif '+f)