Skip to content

Add strip_namespaces argument to Image.getxmp() - #9882

Open
geeno16 wants to merge 7 commits into
python-pillow:mainfrom
geeno16:getxmp-preserve-namespaces
Open

Add strip_namespaces argument to Image.getxmp()#9882
geeno16 wants to merge 7 commits into
python-pillow:mainfrom
geeno16:getxmp-preserve-namespaces

Conversation

@geeno16

@geeno16 geeno16 commented Aug 20, 2026

Copy link
Copy Markdown

Changes proposed in this pull request:

  • Added an optional strip_namespaces keyword argument to Image.getxmp()
    (defaults to True, preserving current behaviour).
  • When set to False, each tag keeps its full {namespace-uri}local-name
    form instead of having the namespace prefix stripped.

Currently, getxmp() always strips namespace prefixes, which can merge values from different namespaces that happen to share a local tag name. For example:

>>> im.getxmp()
{'xmpmeta': {'RDF': {'Description': {'id': ['from-a', 'from-b']}}}}

Two unrelated id tags from different namespaces (a:id and b:id) get merged into a list, with no way to tell which value came from which namespace. With strip_namespaces=False:

>>> im.getxmp(strip_namespaces=False)
{'{...}xmpmeta': {'{...}RDF': {'{...}Description': {
    '{http://example.com/ns/a/}id': 'from-a',
    '{http://example.com/ns/b/}id': 'from-b',
}}}}

Comment thread src/PIL/Image.py Outdated

def get_name(tag: str) -> str:
return re.sub("^{[^}]+}", "", tag)
return re.sub("^{[^}]+}", "", tag) if strip_namespaces else tag

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd only check the (constant-in-this-function) option once instead of every tag?

Suggested change
return re.sub("^{[^}]+}", "", tag) if strip_namespaces else tag
if strip_namespaces:
def get_name(tag: str) -> str:
return re.sub("^{[^}]+}", "", tag)
else:
def get_name(tag: str) -> str:
return tag

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, thanks!

Comment thread Tests/test_image.py Outdated
Comment thread docs/releasenotes/13.0.0.rst Outdated
Comment thread docs/releasenotes/13.0.0.rst Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants