I am using 3 band jpeg files with 1 band png masks for image analysis. The names match except for extension, and I am merging the jpgs using gdalbuildvrt, then gdal_translate to generate a geotiff. I would like to merge the pngs in the same manner (reusing my existing scripts if possible), but my mask generator does not create a worldfile.

Example files:


The worldfile I have for the jpgs is a PAM/GDAL dataset, and looking inside the files, they won't work by just renaming because they include attributes regarding JPEG compression, bands, etc that don't exist in the pngs.

I have thought of using gdal_translate to convert each jpg image to a grayscale png in a temp directory, then delete the png and copy the worldfile out, but I will be doing this repeatedly for several datasets, so I would prefer a cleaner solution.

  • What are your scripts written in? To georeference a PNG you only need the SRS and GeoTransform elements from the XML - everything else is optional. Some python code to read one XML and write another with only those tags can be done in a few lines. There might even be a command-line processor to do it...
    – Spacedman
    Apr 11, 2019 at 18:03
  • related (pgw) gis.stackexchange.com/questions/84363/…
    – Mapperz
    Apr 11, 2019 at 18:05
  • Does anyone have an example xml file for a png?
    – user15741
    Apr 11, 2019 at 19:00
  • Edit your JPG metafile and leave only the SRS and GeoTransform - rename it and save it and that will be a valid metadatafile for a PNG.
    – Spacedman
    Apr 11, 2019 at 19:25

1 Answer 1


Based on the comments by Spacedman, I put together the following script which works in my case:

import glob
import os
from pathlib import Path
from xml.etree import ElementTree

srcpath = 'D:/jpglocation/'
destpath = 'D:/pnglocation/'
filemask = '*.jpg.aux.xml'

wfs = glob.glob(f'{srcpath}{filemask}')
i = 0
for wf in wfs:
    et = ElementTree.parse(wf)
    root = et.getroot()

    # Strip jpg metadata
    for metadata in root.findall('Metadata'):
    for metadata in root.findall('PAMRasterBand'):

    #add png metadata
    metaImg = ElementTree.SubElement(root, "Metadata", domain="IMAGE_STRUCTURE")
    mdi = ElementTree.Element("MDI", key="INTERLEAVE")
    mdi.text = "PIXEL"

    meta = ElementTree.SubElement(root, "Metadata")
    mdi = ElementTree.Element("MDI", key="AREA_OR_POINT")
    mdi.text = "AREA"
    mdi = ElementTree.Element("MDI", key="COLORSPACE")
    mdi.text = "RGB"
    mdi = ElementTree.Element("MDI", key="COMPRESSION_RATE_TARGET")
    mdi.text = "1"

    # Write back to file
    p = Path(wf)
    destfile = destpath + (p.stem).replace('.jpg', '.png') + '.xml'

And it works with gdalbuildvrt now to easily merge the png tiles into a larger composite:

gdalbuildvrt -input_file_list merged.vrt *.png
gdal_translate merged.vrt merged.tif

The worldfile didn't function with only SRS and GeoTransform, but it worked with generic png settings (my pngs are grayscale but the png metadata was from an RGB file).

Your Answer

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

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