2

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:

tileset_nn_0_0.jpg
tileset_nn_0_0.jpg.aux.xml
tileset_nn_0_0.png
tileset_nn_1_0.jpg
tileset_nn_1_0.jpg.aux.xml
tileset_nn_1_0.png
tileset_nn_2_0.jpg
...

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 at 18:03
  • related (pgw) gis.stackexchange.com/questions/84363/… – Mapperz Apr 11 at 18:05
  • Does anyone have an example xml file for a png? – user15741 Apr 11 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 at 19:25
0

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'):
        root.remove(metadata)
    for metadata in root.findall('PAMRasterBand'):
        root.remove(metadata)

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

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

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

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, privacy policy and cookie policy

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