Skip to content

Instantly share code, notes, and snippets.

@ismasan
Created July 2, 2025 11:01
Show Gist options
  • Save ismasan/e2171b1a7229b60850f13f3e9ec5f5e6 to your computer and use it in GitHub Desktop.
Save ismasan/e2171b1a7229b60850f13f3e9ec5f5e6 to your computer and use it in GitHub Desktop.
Ruby extract EXIF data from JPEG images
require 'exif'
require 'geocoder'
require 'time'
def coord(deg, min, sec, _ref)
(deg + (min / 60) + (sec / 3600)).to_f # * (ref == "S" || ref == "W") ? -1 : 1
# %(#{deg.to_i}°#{min.to_i}'#{sec.to_f}"#{ref})
end
def coords(gos)
lat_deg, lat_min, lat_secs = gos[:gps_latitude]
lat_ref = gos[:gps_latitude_ref]
long_deg, long_min, long_secs = gos[:gps_longitude]
long_ref = gos[:gps_longitude_ref]
[coord(lat_deg, lat_min, lat_secs, lat_ref), coord(long_deg, long_min, long_secs, long_ref)]
end
data = Exif::Data.new(File.open('/Users/ismasan/Desktop/IMG_0724.jpeg'))
p data.model # => "NIKON D600"
p data.image_width # => 4000
p data[:gps]
cc = coords(data[:gps])
p cc
results = Geocoder.search(cc)
p data.date_time
p DateTime.strptime(data.date_time, '%Y:%m:%d %H:%M:%S')
p results.first.country
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment