XMP 图像标记和 Python

发布于 2024-07-13 05:14:00 字数 630 浏览 10 评论 0原文

如果我要在 Python 中通过 XMP 标记一堆图像,最好的方法是什么? 我使用过 Perl 的 Image::ExifTool 并且非常习惯它的可靠性。 我的意思是,这东西在数以万计的图像上从来不会变砖。

我发现这个,得到了欧洲航天局等一些重量级人物的支持,但它被明确标记为不稳定。

现在,假设我熟悉 C++,那么使用 Adobe XMP 工具包< /a> 直接,在 Python 中? 以前从未这样做过,我不确定我会注册什么。

更新:我尝试了一些库,包括前面提到的工具包,它们仍然非常不成熟并且存在明显的问题。 我实际上编写了一个基于 Perl 的服务器,它接受 XML 请求来读取和写入元数据,并使用经过实战测试的 Image::EXIF。 代码量实际上非常少,绝对胜过尝试让 Python 库工作来折磨自己。 服务器解决方案与语言无关,因此它是一个双重的。

If I were to tag a bunch of images via XMP, in Python, what would be the best way? I've used Perl's Image::ExifTool and I am very much used to its reliability. I mean the thing never bricked on tens of thousands of images.

I found this, backed by some heavy-hitters like the European Space Agency, but it's clearly marked as unstable.

Now, assuming I am comfortable with C++, how easy is it to, say, use the Adobe XMP toolkit directly, in Python? Having never done this before, I am not sure what I'd sign up for.

Update: I tried some libraries out there and, including the fore mentioned toolkit, they are still pretty immature and have glaring problems. I resorted to actually writing an Perl-based server that accepts XML requests to read and write metadata, with the combat-tested Image::EXIF. The amount of code is actually very light, and definitely beats torturing yourself by trying to get the Python libraries to work. The server solution is language-agnostic, so it's a twofer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

挽心 2024-07-20 05:14:01

对于将来发现此线程的人,我想分享我的解决方案。 我在 Python 包索引 (PyPI) 上放置了一个名为 imgtag 的包。 它允许您使用 python-xmp-toolkit 进行基本的 XMP 主题字段标签编辑,但将实际使用 python-xmp-toolkit 的所有令人沮丧的废话抽象为一行命令。

为您的平台安装 exempi ,然后运行

python3 -m pip install imgtag

​​现在您可以这样使用它:

from imgtag import ImgTag

# Open image for tag editing
test = ImgTag(
           filename="test.jpg", # The image file
           force_case="lower",  # Converts the case of all tags
                                # Can be `None`, `"lower"`, `"upper"`
                                # Default: None
           strip=True,          # Strips whitespace from the ends of all tags
                                # Default: True
           no_duplicates=True   # Removes all duplicate tags (case sensitive)
                                # Default: True
       )

# Print existing tags
print("Current tags:")
for tag in test.get_tags():
    print("  Tag:", tag)

# Add tags
test.add_tags(["sleepy", "happy"])

# Remove tags
test.remove_tags(["cute"])

# Set tags, removing all existing tags
test.set_tags(["dog", "good boy"])

# Save changes and close file
test.close()

# Re-open for tag editing
test.open()

# Remove all tags
test.clear_tags()

# Delete the ImgTag object, automatically saving and closing the file
del(test)

我还没有为其他 XMP 字段(如描述、日期、创建者等)添加方法。也许有一天我会的,但是如果您查看源代码中现有函数的工作原理< /a>,您可能可以自己弄清楚如何添加该方法。 如果您确实添加了更多方法,请提出拉取请求。 :)

For people finding this thread in the future, I would like to share my solution. I put a package up on the Python Package Index (PyPI) called imgtag. It lets you do basic XMP subject field tag editing using python-xmp-toolkit, but abstracts away all of the frustrating nonsense of actually using python-xmp-toolkit into one-line commands.

Install exempi for your platform, then run

python3 -m pip install imgtag

Now you can use it as such:

from imgtag import ImgTag

# Open image for tag editing
test = ImgTag(
           filename="test.jpg", # The image file
           force_case="lower",  # Converts the case of all tags
                                # Can be `None`, `"lower"`, `"upper"`
                                # Default: None
           strip=True,          # Strips whitespace from the ends of all tags
                                # Default: True
           no_duplicates=True   # Removes all duplicate tags (case sensitive)
                                # Default: True
       )

# Print existing tags
print("Current tags:")
for tag in test.get_tags():
    print("  Tag:", tag)

# Add tags
test.add_tags(["sleepy", "happy"])

# Remove tags
test.remove_tags(["cute"])

# Set tags, removing all existing tags
test.set_tags(["dog", "good boy"])

# Save changes and close file
test.close()

# Re-open for tag editing
test.open()

# Remove all tags
test.clear_tags()

# Delete the ImgTag object, automatically saving and closing the file
del(test)

I haven't yet added methods for the other XMP fields like description, date, creator, etc. Maybe someday I will, but if you look at how the existing functions work in the source code, you can probably figure out how to add the method yourself. If you do add more methods, make a pull request please. :)

忘年祭陌 2024-07-20 05:14:01

您可以使用 ImageMagic convert,IIRC 还有一个 Python 模块。

You can use ImageMagic convert, IIRC there's a Python module to it as well.

梦屿孤独相伴 2024-07-20 05:14:00

好吧,他们的网站说 python-xmp-toolkit 通过 ctypes 使用 Exempi,它基于 Adob​​e XMP 工具包。 我想说的是,您不太可能自己创建更好的 C++ 代码包装。 如果它不稳定(即有错误),那么创建补丁很可能比您自己从头开始创建补丁更便宜。

但是,在您的特殊情况下,这取决于您需要多少功能。 如果您只需要单个函数,那么将 C++ 代码包装到小型 C 扩展库或使用 Cython 是可行的。 当您需要拥有所有功能时 灵活性,您必须手动或使用 SWIG 创建包装器,基本上重复其他人已经完成的工作。

Well, they website says that the python-xmp-toolkit uses Exempi, which is based on the Adobe XMP toolkit, via ctypes. What I'm trying to say is that you're not likely to create a better wrapping of the C++ code yourself. If it's unstable (i.e. buggy), it's most likely still cheaper for you to create patches than doing it yourself from scratch.

However, in your special situation, it depends on how much functionality you need. If you just need a single function, then wrapping the C++ code into a small C extension library or with Cython is feasible. When you need to have all functionality & flexibility, you have to create wrappers manually or using SWIG, basically repeating the work already done by other people.

策马西风 2024-07-20 05:14:00

我在 python-xmp-toolkit 上挣扎了几个小时,最终放弃了,只是将调用包装到 ExifTool

还有一个一个 Ruby 库也封装了 ExifTool(尽管比我创建的要好得多); 我认为将其移植到 Python 是值得的,以便以简单的方式处理 XMP。

I struggled for several hours with python-xmp-toolkit, and eventually gave up and just wrapped calls to ExifTool.

There is a Ruby library that wraps ExifTool as well (albeit, much better than what I created); I feel it'd be worth porting it to Python for a simple way of dealing with XMP.

当梦初醒 2024-07-20 05:14:00

对于 Python 3.x,有 py3exiv2 支持编辑 XMP 元数据

使用 py3exiv2,您可以读取和写入所有标准元数据、创建您自己的 XMP 命名空间或提取嵌入图像文件中的缩略图。

我喜欢 py3exiv2 的一件事是它构建在 (C++) exiv2 库 上,

我确实遇到过 这个库,它维护得很好但在我的系统(Ubuntu 16.04)上安装它时出现问题。 为了让它工作,我首先必须安装最新版本的 libexiv2-dev (sudo apt-get install libexiv2-dev),并且只有在安装 py3exiv2 之后 (sudo -H pip3 install py3exiv2

这是我如何使用 py3exiv2 编写新标签:(

import pyexiv2
metadata = pyexiv2.ImageMetadata("file_name.jpg")
metadata.read()
key = "Xmp.xmp.CustomTagKey"
value = "CustomTagValue"
metadata[key] = pyexiv2.XmpTag(key, value)
metadata.write()

还有一个 教程

For Python 3.x there's py3exiv2 which supports editing XMP metadata

With py3exiv2 you can read and write all standard metadata, create your own XMP namespace or extract the thumbnail embedded in image file.

One thing I like about py3exiv2 is that it's built on the (C++) exiv2 library which seems well-maintained

I did encounter a problem though when installing it on my system (Ubuntu 16.04). To get it working I first had to install the latest version of libexiv2-dev (sudo apt-get install libexiv2-dev), and only after this install py3exiv2 (sudo -H pip3 install py3exiv2)

Here's how I've used py3exiv2 to write a new tag:

import pyexiv2
metadata = pyexiv2.ImageMetadata("file_name.jpg")
metadata.read()
key = "Xmp.xmp.CustomTagKey"
value = "CustomTagValue"
metadata[key] = pyexiv2.XmpTag(key, value)
metadata.write()

(There's also a tutorial in the documentation)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文