Skip to content

Instantly share code, notes, and snippets.

@runeb
Last active April 10, 2022 21:30
Show Gist options
  • Save runeb/4587530 to your computer and use it in GitHub Desktop.
Save runeb/4587530 to your computer and use it in GitHub Desktop.
Adobe XMP parser for JPEG files, in Ruby
def xmp(filename)
xap = "http://ns.adobe.com/xap/1.0/\0".freeze
f = File.open(filename, 'r')
xml = nil
begin
return nil unless f.readbyte == 0xFF
return nil unless f.readbyte == 0xD8
while !f.eof
case f.readbyte
when 0xFF
case f.readbyte
when 0xE1
size = f.read(2).unpack('H*').first.hex
next unless size > xap.length
ns = f.read(xap.length)
if ns == xap
xml = f.read(size - xap.length - 2)
break
end
end
end
end
ensure
f.close
end
xml
end
require 'nokogiri'
puts Nokogiri::XML.parse xmp('yeah.jpg')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment