该Extensible Metadata Platform (XMP) logo inside a syringe.

Embed XMP sidecars into image files withembed_xmp

I wanted a program to inject Extensible Metadata Platform (XMP) sidecar (stand-alone) files into an image file. Image metadata is mostuseful when embedded into image filesafter all. I was surprised to learn that options for XMP editors were thin on the ground. I had to create a new program to get the job done.

Two existing programs come to mind:Exiv2ExifTool. Both tools interpret the sidecar file and write the recognize metadata back into the image files using their own distinct set of rules. This would be fine except when you want to include metadata from namespaces/vocabularies they don’t recognize or want to make sure the resulting metadata takes up as little space as possible.

Exiv2writes XMP in a compacted form that isn’t compatible with the underlyingRDFstandard, the XMP standard, and isn’t supported by any popular image viewers or operating systems. ExifTool, on the other hand, produces overly verbose XMP markup that takes up more space than necessary. It can be configured to compact it somewhat and reduce optional whitespace. At my request, the author of ExifTool added even more options for compacting it further.

Both of these tools also have a few different image format-specific bugs and they don’t support all of the formats I need (including WebP and SVG).

I wanted a program that takes an XMP sidecar file without processing it and injects it as-is into the right place inside of image files. A third program calledwebpmuxdoes exactly this. However, it only works for WebP images.

So I wrote a new program calledembed_xmpto fulfill this need. It’s available as a Ruby gem/library and as a command-line interface (CLI). (Sorry if you were expecting a graphical user interface!) It supports writing XMP sidecars into JPEG, PNG, SVG, and WebP image files.

It’s not quite what I explained above, though. It doesn’t inject XMP data verbatim. It will wrap the XMP data in anxpacketXML processing instruction, as recommended by theXMP规格. This ensures the broadest client-compatibility. It also doesn’t preserve XML whitespace (unless overwritten by anxml:space在XMP文件中的指令)。空格可以大量文件的大小超过它在其他情况下,因为当嵌入到文件XMP数据不被压缩。图片文件usually not recompressedwhen served through a web server either.

该计划将不会进行其他更改图像文件,除非在必要时通过图像格式。最显着的变化是基本的WebP图像被升级为需要时WebP的扩展文件格式(WEFF)。WEFF可能无法在旧版浏览器渲染。该WebP container specificationonly supports embedding XMP data into WEFF files.

You’ll need another program to author the XMP sidecar files — such as Corel AfterShot Pro, Adobe Photoshop or Lightroom, or Photo Mechanic — and can then useembed_xmpto insert that sidecar into an image file. You’ll probably want to useembed_xmpwith some sort of content management system that holds image metadata, though.

You can install theembed_xmpCLI and library usingRubyGems. You’ll need to install Ruby and RubyGems first, and then run the following command to download and install the latest version ofembed_xmp:

gem install embed_xmp

该CLI tool requires three positional arguments:

embed_xmp sidecar.xmp input.png output.png

输入文件的文件扩展名来确定的图像格式。输出文件的名称是不重要的,但它会在相同的格式作为输入图像输出。无图像格式转换会发生。该附属文件必须是有效的XML,它不能被包裹在一个xpacketXML processing instruction. There are no sanity-checks or format-validation.

该Ruby library has a few more options. The short and sweet usage instructions are as follows:

require "embed_xmp" input_file = "/example.png" # ::PNG, ::JPEG, ::SVG, or ::WebP embedder = EmbedXMP::PNG.new(input_file) sidecar_file = "/example.xmp" # can also be supplied as a string embedder.join_sidecar(sidecar_file) output_file = "/example-xmp.png" embedder.write(output_file)

You can find some more detailed usage in thedocumentation on RubyDoc. (You also installed a local copy of the documentation along with the gem.) Although not the intended use, it also includes methods for removing XMP data from image files.

该XMP规格requires that you ensure that legacy/other metadata formats — e.g. native metadata fields — are updated to be aligned with the XMP metadata.embed_xmp没有做到这一点。(这是不知道的,你要插入的元数据的内容。)你必须确保你更新或删除其它格式冲突的元数据。

You can be notified of future updates by following the gem’sAppCast feedor by visitingits page on RubyGems.

I’ve not yet published the source code for this gem anywhere. However, you get a copy when installing the gem via RubyGems. It’s licensed under a BSD 3-Clause license. I’m evaluatingdistributed alternatives to GitHub并没有想上传任何更多的仓库那里。一旦我决定上的东西我会更新这篇文章链接到源代码。