Search code examples
luaexifwebpopenrestyluajit

How to get webp image EXIF metadata in lua?


I can get this data with the following code. But it runs too slow:

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

Is there a more elegant way to get the metadata?

UPD:

I use this software:

  • docker (20.10.7)
  • openresty/openresty:xenial (1.15.8.3)
  • luarocks (3.2.1)
  • LuaJIT (2.1.0-beta3)

Here is an example of a picture with a UserComment field: link

Exiftool sees this property:

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

Solution

  • This method is less elegant, but it does not require to run external application :-)

    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
    

    Usage example:

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