0

I have several hundred tiff files that I need to convert to png. I have a script that does this conversion one file at a time, but can not figure out how to loop through all of the tiff files at once and convert to png files with otherwise the same file names. Here is the code I have that converts one file at a time (I'm working on this in Jupyter):

from osgeo import gdal

options_list = [ '-ot Byte', '-of PNG', '-b 1', '-scale' ] options_string = " ".join(options_list)

gdal.Translate('File1.png', 'File1.tif', options=options_string)

Any idea how to edit this such that the code looks for all files ending with *.tif and converts them to *.png?

1 Answers1

0

you can use the glob module to find all TIFF files in the directory, and then loop over them to convert them one by one.

import glob
from osgeo import gdal

options_list = [ '-ot Byte', '-of PNG', '-b 1', '-scale' ] 
options_string = " ".join(options_list)

# Path to directory containing TIFF files
tiff_dir = '/path/to/tiff/directory/'

# Loop over all TIFF files in directory
for tiff_file in glob.glob(tiff_dir + '*.tif'):
    # Generate output filename
    ...
    # Convert TIFF to PNG
    ...
Sheykhmousa
  • 139
  • 9