2

I want to convert the data type (Int32 to Float32) of all the tiff images in a directory 'F:\\prep\\input\\' and save/write the outputs to a new directory 'F:\\prep\\converted\\'.

I have tried using gdal.Translate and glob.glob:

from osgeo import gdal
import glob
import os
data_to_process = glob.glob(os.path.join('F:\\prep\\input\\*.tif'))
for data_path in data_to_process:
raster_dataset = gdal.Open(data_path, gdal.GA_ReadOnly)
raster = gdal.Translate('F:\\prep\\converted\\*.tif', raster_dataset, outputType = gdal.GDT_Float32)
New contributor
Sai Kiran is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
1

Your indentation is off, but I'll assume that's just a copy/paste issue.

Your issue is that 'F:\\prep\\converted\\*.tif' will try to output a file called literally *.tif which won't work. You need to provide an actual file name. I suggest using os.path.join and os.path.basename

Try this:

from osgeo import gdal
import glob
import os

input_path = 'F:\\prep\\input'
output_path = ''F:\\prep\\converted'

data_to_process = glob.glob(os.path.join(input_path, '*.tif'))
for data_path in data_to_process:
    raster_dataset = gdal.Open(data_path, gdal.GA_ReadOnly)
    raster = gdal.Translate(os.path.join(output_path, os.path.basename(data_path)), raster_dataset, outputType = gdal.GDT_Float32)
  • Awesome! It worked. Thanks – Sai Kiran Feb 14 at 6:51

Your Answer

Sai Kiran is a new contributor. Be nice, and check out our Code of Conduct.

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.