【问题标题】:How to get webp image EXIF metadata in lua?如何在lua中获取webp图像EXIF元数据?
【发布时间】:2021-08-27 03:01:45
【问题描述】:

我可以使用以下代码获取此数据。但是它运行得太慢了:

local handle = io.popen("exiftool image.webp")
local result = handle:read("*a")
handle:close()

有没有更优雅的方式来获取元数据?

更新:

我用这个软件:

  • 码头工人 (20.10.7)
  • openresty/openresty:xenial (1.15.8.3)
  • luarocks (3.2.1)
  • LuaJIT (2.1.0-beta3)

这是一个带有 UserComment 字段的图片示例:link

Exiftool 看到这个属性:

$ exiftool -EXIF:UserComment Johnrogershousemay2020.webp  
User Comment                    : {"foo":"bar"}  

【问题讨论】:

  • 也就是说,要获取webp图片的宽高吗?
  • 不,我需要从 UserComment 字段中获取信息
  • 要求软件推荐是违反指南的。所以要么使用特定的库/软件询问问题,要么在自己实现时询问问题(然后添加更多代码)。
  • 您能否提供一个具有 UserComment 字段的 webp 文件示例?
  • 我添加了示例图片,以及有关软件的信息

标签: lua exif webp openresty luajit


【解决方案1】:

这个方法不太优雅,但它不需要运行外部应用程序:-)

function get_webp_user_comment(file_name)
   local file = io.open(file_name, "rb")
   local exif_offset, exif_found, is_big_endian, user_comment = 12

   local function read_string(offset, size)
      file:seek("set", offset)
      return file:read(size)
   end

   local function read_uint(offset, size)
      local n, s = 0, read_string(offset, size)
      for j = 1, size do
         n = n * 256 + s:byte(is_big_endian and j or size + 1 - j)
      end
      return n
   end

   local function read_uint32(disp)
      return read_uint(exif_offset + disp, 4)
   end

   local function read_uint16(disp)
      return read_uint(exif_offset + disp, 2)
   end

   local function search_for_tag(ifd_disp, tag)
      if ifd_disp ~= 0 then
         local entry_disp = ifd_disp + 2
         for j = 1, read_uint16(ifd_disp) do
            if read_uint16(entry_disp) == tag then
               return read_uint32(entry_disp + 8), read_uint32(entry_disp + 4)
            end
            entry_disp = entry_disp + 12
         end
         return search_for_tag(read_uint32(entry_disp), tag)
      end
   end

   if read_string(0, 4) == "RIFF" and read_string(8, 4) == "WEBP" then
      local max_offset = read_uint32(-8)
      while exif_offset < max_offset do
         local section_name = read_string(exif_offset, 4)
         exif_offset = exif_offset + 8
         if section_name == "EXIF" then
            local endianness = read_string(exif_offset, 2)
            is_big_endian = endianness == "MM"
            exif_found = is_big_endian or endianness == "II"
            if exif_found then
               break
            end
         end
         exif_offset = exif_offset + read_uint32(-4)
         exif_offset = exif_offset + exif_offset % 2
      end
   end
   if exif_found then
      local exif_ifd = search_for_tag(read_uint32(4), 0x8769)
      if exif_ifd then
         local disp, count = search_for_tag(exif_ifd, 0x9286)
         user_comment = read_string(exif_offset + disp + 8, count - 8)
      end
   end
   file:close()
   return user_comment
end

使用示例:

local file_name = "path/to/Johnrogershousemay2020.webp"
print(get_webp_user_comment(file_name))

【讨论】:

  • 也许“不那么优雅”但更加独立,因为它不依赖(执行)第 3 方软件。使用 cmets 和使用常量作为幻数的代码会更有价值(0x8769 是 IFD_Exif,0x9286 是 EXIF 的用户注释,但 Windows XP 的注释 0x9c9c 没有提到)。
  • @AmigoJack - 你能提供一张带有0x9c9c 评论的图片吗?
  • github.com/drewnoakes/.../windowsXpFields.jpg 以及该仓库中类似名称的文件。
  • @AmigoJack - 你说的是XP Comment 字段吗?这是另一个领域。 OP 只想要 User Comment 字段(可能有一些协议可以在其中保存 JSON 数据?)
  • 您认为我的评论还有什么意义?我什至提供了 TIFF ID。不,0x92860x9c9c 都不是用于 JSON 的——没有读者会想到这一点。 Windows XP 的注释字段始终以 UCS2 编码保存。是的,OP 仅表示“UserComment”,但在阅读文件时,我宁愿为所有类型的 cmets 做好准备。
【解决方案2】:

如果您只需要“UserComment”,则在您的 popen 调用期间将其作为参数传递:

local handle = io.popen("exiftool -EXIF:UserComment image.webp")

【讨论】:

  • 不幸的是,打开新的 popen 进程太耗费资源。
猜你喜欢
  • 1970-01-01
  • 2019-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多