[Python] xmp tag for photos

Introduction

You can record the shooting date and time and location in a JPG file of the photo. Tag information may be written on photos taken with drones, etc. They seem to be recorded according to a common format. The purpose is to read and write them.

There are many descriptions about GPS Exif. So I'll make a note of xmp, which seems to be less informative here. I can read it, but I'm not good at writing it. (Tears)

Preparation: Install xmp-toolkit

What is XMP

Apparently, it was defined by Adobe and used to add information to each file in XML format text to the file's header. I don't know the details. ^^;)

So, for example, if you look at it with the less command, you can read the XML part of the text as it is. My uncle is in a Linux environment (WSL), --Open the file with less ――You will be asked "It's binary, but will you open it?", So say Yes. --Searching for the string xml (slash xml return),


<x:xmpmeta xmlns:x="adobe:ns:meta/">
 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
  <rdf:Description rdf:about="DJI Meta Data"
    xmlns:tiff="http://ns.adobe.com/tiff/1.0/"
    xmlns:exif="http://ns.adobe.com/exif/1.0/"
    xmlns:xmp="http://ns.adobe.com/xap/1.0/"
    xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:crs="http://ns.adobe.com/camera-raw-settings/1.0/"
    xmlns:drone-dji="http://www.dji.com/drone-dji/1.0/"
   xmp:ModifyDate="2018-12-14"
   xmp:CreateDate="2018-12-14"
   tiff:Make="DJI"
   tiff:Model="FC6310R"
   dc:format="image/jpg"
   drone-dji:AbsoluteAltitude="+255.92"
   ...

Looks like. The purpose is to read and write this information.

In python

There are, of course, useful tools.

python3 -m pip install python-xmp-toolkit

The XMP-Toolkit-SDK is also available from Adobe for each source. I wonder if it can be used in embedded or smartphone apps. https://github.com/adobe/XMP-Toolkit-SDK

Read the value of the xmp tag

The usage is written in Doc of python-xmp-toolkit, and I just followed it and there was no problem. .. Make a note.

Read the file

read_xmp01.py


from libxmp import XMPFiles
xmpfile = XMPFiles( file_path="./100_0020_0001.JPG", open_forupdate=True )
xmp = xmpfile.get_xmp()

If there is no error, you can read it. ^^;) You can check the contents of xmp in various ways.

Get value

In the previous sample

tiff:Make="DJI"

There was a line called. I want to get the value "DJI" directly for the key "tiff: Make". For that, two steps are required.

Get namespace

First, get the namespace (URI) of the tiff.

In[]: xmp.get_namespace_for_prefix("tiff")
Out[]: 'http://ns.adobe.com/tiff/1.0/'

There is also a namespace with adobe defined in advance, which can also be referenced from the library.

In []: libxmp.consts.XMP_NS_EXIF
Out[]: 'http://ns.adobe.com/exif/1.0/'

You can also find the variable name directly in "Common Namespaces" in Source. ..

Get property

To read the value of the tag tiff: Make, specify the namespace corresponding to the tiff.

In []: xmp.get_property(xmp.get_namespace_for_prefix('tiff'),'Make')
Out[]: 'DJI'

You can prevent errors by checking in advance if the keyword is in the namespace.

In []: xmp.does_property_exist(xmp.get_namespace_for_prefix('tiff'),'TEST')
Out[]: False

write

To be precise, it should be possible to "rewrite", but in reality. .. ..

First, set the value of xmp.

In []: xmp.set_property(xmp.get_namespace_for_prefix('tiff'),'Make', 'some/makers')
In []: xmp.get_property(xmp.get_namespace_for_prefix('tiff'),'Make')
Out[]: 'some/makers'

It seems that you need to put to xmpfile and close the file to write to the file.

In []: if xmpfile.can_put_xmp(xmp):
  ...:     xmpfile.put_xmp(xmp)
  ...:     xmpfile.close_file()

It should be fine. .. .. The result was not rewritten correctly. I don't know if the procedure is different or it's a bug, but I'll make a note of it for now. No error occurs at run time.

Note

I was in a state where I could only read it for the time being. Below is my task:

--Can you get all the tags in each namespace in advance? --Checking the writing operation

that's all. (2020/08/08)

Recommended Posts

[Python] xmp tag for photos
2016-10-30 else for Python3> for:
About Python for loops
Python basics ② for statement
About Python, for ~ (range)
python textbook for beginners
Refactoring tools for Python
python for android Toolchain
python tag integration test
OpenCV for Python beginners
Install Python (for Windows)
[Python] for statement error
Python environment for projects
Python memo (for myself): Array
About Fabric's support for Python 3
Python list, for statement, dictionary
Python for Data Analysis Chapter 4
Modern Python for intermediate users
Learning flow for Python beginners
Python 3.6 installation procedure [for Windows]
BigQuery integration for Python users
Python learning plan for AI learning
Set Up for Mac (Python)
Search for strings in Python
Python Tkinter notes (for myself)
OpenCV3 installation for Python3 @macOS
Python code memo for yourself
Python environment construction For Mac
Techniques for sorting in Python
pp4 (python power for anything)
Python3 environment construction (for beginners)
Roadmap for publishing Python packages
Python 3 series installation for mac
Python #function 2 for super beginners
Python template for Codeforces-manual test-
Basic Python grammar for beginners
3 months note for starting Python
Qt for Python app self-update
Python for Data Analysis Chapter 2
100 Pandas knocks for Python beginners
Checkio's recommendation for learning Python
Keyword arguments for Python functions
[For organizing] Python development environment
[Python] Sample code for Python grammar
Python for super beginners Python #functions 1
[Python / PyQ] 4. list, for statement
Simple HTTP Server for python
[Python + Selenium] Tips for scraping
Python #list for super beginners
~ Tips for beginners to Python ③ ~
Extract only Python for preprocessing
Indentation formatting for python scripts
Introduction to Python For, While
About "for _ in range ():" in python
tesseract-OCR for Python [Japanese version]
[Python] Iterative processing (for, while)
Python for Data Analysis Chapter 3
Install dlib for Python (Windows)
What is Python? What is it used for?
Check for memory leaks in Python
Prolog Object Orientation for Python Programmers