代码之家  ›  专栏  ›  技术社区  ›  Andrei Taranchenko

XMP图像标记和Python

  •  3
  • Andrei Taranchenko  · 技术社区  · 17 年前

    我非常习惯于它的可靠性。我的意思是,这件事从来没有在成千上万的图片上出现过。

    我发现 this 由一些重量级人物如欧洲航天局支持,但它显然被标记为不稳定。

    Adobe XMP toolkit 直接,用Python?由于以前从未这样做过,我不确定我会报名参加什么。

    更新:

    4 回复  |  直到 17 年前
        1
  •  4
  •   Torsten Marek    17 年前

    他们的网站上说,PythonXMP工具包通过ctypes使用基于AdobeXMP工具包的Exempi。我想说的是,你不可能自己创建一个更好的C++代码包。如果它是不稳定的(例如,有车的),那么对您来说,创建补丁很可能比自己从头开始更便宜。

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

        2
  •  4
  •   Xiong Chiamiov    15 年前
        3
  •  2
  •   sunyata    8 年前

    对于Python3.x,有 py3exiv2 它支持编辑XMP元数据

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

    exiv2 library 看起来保养得很好

    sudo apt-get install libexiv2-dev 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()
    

    tutorial (在文件中)

        4
  •  1
  •   Ella Jameson    5 年前

    对于将来找到这条线索的人,我想分享我的解决方案。 I put a package up on the Python Package Index (PyPI) called imgtag . 它允许您使用 python-xmp-toolkit PythonXMP工具包 转换为一行命令。

    安装 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字段添加方法,如description、date、creator等。也许有一天我会添加,但是如果您看看现有函数是如何工作的 in the source code ,您可能会自己想出如何添加该方法。如果您确实添加了更多方法,请提出拉取请求。:)

        5
  •  0
  •   Miki Tebeka    17 年前

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