I want to pansharpen several true color and panchromatic Landsat-8 images in a folder using open source tools (GDAL, Python). File names are of format: L8_2018-09-25T18-17-51_TrueColor_ERaymond.tiff L8_2018-09-25T18-17-51_Pan_ERaymond.tiff L8_2018-08-31T18-23-30_TrueColor_ERaymond.tiff L8_2018-08-31T18-23-30_Pan_ERaymond.tiff

These are clipped images from Sentinel Hub Python API (https://sentinelhub-py.readthedocs.io/en/latest/).

How can I automate this?

  • gdal has a python utility for this, which you could import into your script and call the gdal_pansharpen() function or run via subprocess. Grab the base image names, then run that for each unique set – mikewatt Dec 6 '18 at 18:27
  • Thanks I understand that as I was manually using GDAL (OSGeo4W Shell) to pansharpen one image. My challenge is how to replicate for all L8 images in the folder (how to get the list of image dates and then get Python GDAL to select the true color and pan image of an exact same date to feed into the loop... – Arun Govind Dec 6 '18 at 21:10
  • One idea I could think is to print the list of image dates and download each image into their date folder and then process each date folder in a loop. I'm very new to Python and doesn't really know how to automate this. GDAL doesn't seem to allow to filter out for today's date, e.g. *.06Dec2018.tiff – Arun Govind Dec 6 '18 at 21:17

Since you already know about gdal_pansharpen, what you're asking is more of a general Python question than anything.

That said, here's how you could build a lookup dict with regex:

import os
import re
from collections import defaultdict

src_dir = r"C:\temp\example"
pattern = '^(.*)_(Pan|TrueColor)_ERaymond.tiff'

path_lookup = defaultdict(dict)

for name in os.listdir(src_dir):
    match = re.match(pattern, name)
    if not match:
    base_name, image_type = match.groups()
    path_lookup[base_name][image_type] = os.path.join(src_dir, name)

for base_name, images in path_lookup.iteritems():
    path_pan = images['Pan']
    path_rgb = images['TrueColor']
    # do your thing

I'll leave the command building/execution up to you.

  • gdalinfo -mm L8_543.tiff gdal_translate L8_543.tiff L8_543_sc.tiff -scale src_min src_max 0 65535 -exponent 0.5 -co COMPRESS=DEFLATE -co PHOTOMETRIC=RGB gdalinfo -mm L8_B8.tiff gdal_translate L8_B8.tiff L8_p_sc.tiff -scale src_min src_max 0 65535 -exponent 0.5 -co COMPRESS=DEFLATE -co PHOTOMETRIC=RGB gdal_pansharpen L8_pan_sc.tiff L8_543_sc.tiff L8_543p.tiff -r bilinear -co COMPRESS=DEFLATE -co PHOTOMETRIC=RGB python gdalcopyproj.py L8_B2.tiff L8_543p.tiff – Arun Govind Dec 6 '18 at 22:19
  • Can I do image scaling for multi and pan images and then pansharp using scaled images like in here? medium.com/planet-stories/… – Arun Govind Dec 6 '18 at 22:26

Your Answer

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.