Skip to content Skip to sidebar Skip to footer

Read Image Xmp Data In Python

Can I use PIL, like in this example? I only need to read the data, and I'm looking for the easiest simplest way to do it (I can't install pyexiv). edit: I don't want to believe tha

Solution 1:

Well, I was looking for something similar, then I came across the PHP equivalent question and I translated the anwer to Python:

f = 'example.jpg'
fd = open(f)
d= fd.read()
xmp_start = d.find('<x:xmpmeta')
xmp_end = d.find('</x:xmpmeta')
xmp_str = d[xmp_start:xmp_end+12]
print(xmp_str)

you can then convert xmp_str and parse it with an XML API.

Solution 2:

I am also interested to know if there is a 'proper' easy way to do this.

In the mean time, I've implemented reading XMP packets using pure Python in PyAVM. The relevant code is here. Maybe this would be useful to you?

Solution 3:

with open( imgFileName, "rb") as fin:
    img = fin.read()
    imgAsString=str(img)
    xmp_start = imgAsString.find('<x:xmpmeta')
    xmp_end = imgAsString.find('</x:xmpmeta')
    if xmp_start != xmp_end:
        xmpString = imgAsString[xmp_start:xmp_end+12]

    xmpAsXML = BeautifulSoup( xmpString )
    print(xmpAsXML.prettify())

Or you can use the Python XMP Toolkit

Solution 4:

A search through the PIL source (1.1.7) tells me that it can recognize XMP information in Tiff files, but I cannot find any evidence of a documented or undocumented API for working with XMP information using PIL at the application level.

From the CHANGES file included in the source:

+ Support for preserving ICC profiles (by Florian Böch via Tim Hatch).

  Florian writes:

  It's a beta, so still needs some testing, but should allow you to:
  - retain embedded ICC profiles when saving from/to JPEG, PNG, TIFF.
     Existing code doesn't need to be changed.
  - access embedded profiles in JPEG, PNG, PSD, TIFF.

  It also includes patches for TIFF to retain IPTC, Photoshop and XMP
  metadata when saving as TIFF again, read/write TIFF resolution
  information correctly, andto correct inverted CMYK JPEG files.

So the support for XMP is limited to TIFF, and only allows XMP information to be retained when a TIFF image is loaded, possibly changed, and saved. The application cannot access or create XMP data.

Post a Comment for "Read Image Xmp Data In Python"