Gimp-Forum.net
Editing metadata in 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: Editing metadata in python (/Thread-Editing-metadata-in-python)



Editing metadata in python - vndep - 09-24-2019

Are there any good tutorials/sample code for using python scrips to edit metadata (Image > Metadata > Edit Metadata)?

Thanks in advance!


RE: Editing metadata in python - Ofnuts - 09-24-2019

See pdb.gimp_image_get_metadata(image) and pdb.gimp_image_set_metadata(image,data). But the "data" is in XML format, so you have to use the Python XML library to alter it.

Unless you have a good reason to do that in Gimp, the tool of choice is usually ExifTool, and i you want to write a Python shell script around it, you can use the PyExifTool module.


RE: Editing metadata in python - vndep - 09-24-2019

(09-24-2019, 08:50 AM)Ofnuts Wrote: See pdb.gimp_image_get_metadata(image) and pdb.gimp_image_set_metadata(image,data). But the "data" is in XML format, so you have to use the Python XML library to alter it.

Unless you have a good reason to do that in Gimp, the tool of choice is usually ExifTool, and i you want to write a Python shell script around it, you can use the PyExifTool module.

Thanks.  I am not quite sure what to make of this:  "But the 'data' is in XML format, so you have to use the Python XML library to alter it."

Would you please elaborate?

Thanks!


RE: Editing metadata in python - Ofnuts - 09-24-2019

The returned string looks like the attached file (but what you get isn't a file, it just a huge string). This is XML data (but not very clean one), and to parse/edit this you would normally use a XML library such as xml.etree.ElementTree.


RE: Editing metadata in python - YeboiXd - 10-04-2021

Is there any way to only update certain fields rather than having to add the whole XML file? Also, how would you reference the XML file in the script?


RE: Editing metadata in python - Ofnuts - 10-05-2021

1) No, Gimp doesn't provide an API for this, but updating XML is reasonably easy with the general XML libraries.
2) See post #2


RE: Editing metadata in python - YeboiXd - 10-05-2021

(10-05-2021, 12:11 AM)Ofnuts Wrote: 1) No, Gimp doesn't provide an API for this, but updating XML is reasonably easy with the general XML libraries.
2) See post #2
I see. Is there any way to retrieve the metadata in the image, edit the fields I want to change and then add it back to the image? Also how is the XML library referenced in the program? Through a directory? pdb.gimp_image_set_metadata(image,[what goes here?]).

For example this is a way I am trying to add a plug-in to BIMP in order to change the metadata for a batch of images. META.txt being my XML library I set the fields to be updated, this is in the same folder as the .py file. It is not working.
 
#!/usr/bin/python

# -*- coding: utf-8 -*-



from gimpfu import*



def plugin_no_meta(timg, tdrawable):



    pdb.gimp_image_set_metadata( timg, 'META.txt')

     



register(

        "python_no_meta",

        "strip",

        "for imgur or BB",

        "*",

        "*",

        "2016",

        "<Image>/Tools/no metadata...",

        "*",

        [],

        [],

        plugin_no_meta)


main()