Gimp-Forum.net
Gimp 3.0 get Metadata with Python - Printable Version

+- Gimp-Forum.net (https://www.gimp-forum.net)
+-- Forum: GIMP (https://www.gimp-forum.net/Forum-GIMP)
+--- Forum: Extending the GIMP (https://www.gimp-forum.net/Forum-Extending-the-GIMP)
+--- Thread: Gimp 3.0 get Metadata with Python (/Thread-Gimp-3-0-get-Metadata-with-Python)



Gimp 3.0 get Metadata with Python - Volker - 11-08-2025

How do I retrieve jpg metadata from a Python plug-in in Gimp 3.0?

I open a jpg photo and need some metadata. With Gimp 2 and Python 2 I used the plug-in "gimp_image_get_metadata".
My code was (abridged): 
Code:
import xml.etree.ElementTree as ET

metadata = pdb.gimp_image_get_metadata(self.image)
dateTag  = "exif.photo.datetimeoriginal"
date = self.get_metadata_value(metadata, dateTag)

def get_metadata_value(self, metadata,
                            metadataName):
 ciName = metadataName.lower()
 value = ""
 rootNode = ET.XML(metadata)
 for tag in rootNode.findall('tag'):
   if tag.attrib['name'].lower() == ciName:
     value = tag.text
     break
 return value
The plug-in isn't there any more in Gimp 3. Instead I find the class Gimp.Metadata. This class provides some methods for restricted operations, but none for my needs. I try:
Code:
metadata = image.get_metadata()
I expect to get an xml document, but I don't know how to look for it.
Is there a different approach?

My system: Mac mini (Apple M2), macOS Tahoe (26.0.1)
My Gimp: 3.0.6
My Python: 3.10



 


RE: Gimp 3.0 get Metadata with Python - Volker - 11-09-2025

In the meantime I think there must be a buggy Gimp.Image.serialize().

It seems to be an xml string, but Python's minidom as well as elementtree throw a parse error reading the string.

I help myself with searching the string with string functions, but it feels like computing with an abacus:
Code:
def get_metadata_value(image, tag):
 metadata = image.get_metadata().serialize()
 valueStartPos = metadata.find(tag) + len(tag) + 2
 valueStart = metadata[valueStartPos:]
 value = valueStart[:valueStart.find("<")]
 return value



RE: Gimp 3.0 get Metadata with Python - CmykStudent - 11-09-2025

Volker: Hi! This appears to be a bug that will be fixed for the next release. More info here: https://gitlab.gnome.org/GNOME/gimp/-/issues/15179