Ask Dave Taylor
  • Facebook
  • Instagram
  • Linkedin
  • Pinterest
  • Twitter
  • YouTube
  • Home
  • YouTube Videos
  • Top Categories
  • Subscribe via Email
  • Ask A Question
  • Meet Dave
  • Home
  • HTML & Web Page Design
  • Can I analyze EXIF information on the Mac OS X command line?

Can I analyze EXIF information on the Mac OS X command line?

January 1, 2014 / Dave Taylor / HTML & Web Page Design, Linux Shell Script Programming, Mac & MacOS Help / 7 Comments

I’m learning how to write shell scripts on my Mac OS X Mavericks system and would like to identify and extract specific information from the EXIF information in photos. Is that possible from the Bash shell command line?

Ah, what a refreshing question! I’ve been writing shell scripts for so long that it’s burned into my brain. Not to mention that I write a column on shell script programming for Linux Journal and am the author of the best selling Wicked Cool Shell Scripts for NoStarch Press. Yeah, I’ve been exploring shell scripts for a long time now, so I’m the perfect person to ask. 🙂

As you already know, photos taken by modern cameras (and cell phones) embed extra information including film speed, lens size, date, time, geographic location of the photo and many other items of data. EXIF, in fact, stands for “EXchangeable Image Format”, if you’re curious.

Generally it’s required special third party open source programs to access EXIF information on photos from the command line, but Mac OS X includes a slick utility called “mdls” (which stands for metadata-ls). Here’s what I see when I use “mdls” on a photo sitting on my desktop:

$ mdls IMG_1331.JPG
kMDItemAcquisitionMake         = "Apple"
kMDItemAcquisitionModel        = "iPhone 5s"
kMDItemAltitude                = 33.75805651958354
kMDItemAperture                = 2.275007124536905
kMDItemBitsPerSample           = 32
kMDItemColorSpace              = "RGB"
kMDItemContentCreationDate     = 2013-12-19 15:19:49 +0000
kMDItemContentModificationDate = 2013-12-19 15:19:49 +0000
kMDItemContentType             = "public.jpeg"
kMDItemContentTypeTree         = (
    "public.jpeg",
    "public.image",
    "public.data",
    "public.item",
    "public.content"
)
kMDItemCreator                 = "7.0.4"
kMDItemDateAdded               = 2013-12-30 06:19:43 +0000
kMDItemDisplayName             = "IMG_1331.JPG"
kMDItemEXIFVersion             = "2.2.1"
kMDItemExposureMode            = 0
kMDItemExposureProgram         = 2
kMDItemExposureTimeSeconds     = 0.03333333333333333
kMDItemFlashOnOff              = 0
kMDItemFNumber                 = 2.2
kMDItemFocalLength             = 4.12
kMDItemFSContentChangeDate     = 2013-12-19 15:19:49 +0000
kMDItemFSCreationDate          = 2013-12-19 15:19:49 +0000
kMDItemFSCreatorCode           = ""
kMDItemFSFinderFlags           = 0
kMDItemFSHasCustomIcon         = (null)
kMDItemFSInvisible             = 0
kMDItemFSIsExtensionHidden     = 0
kMDItemFSIsStationery          = (null)
kMDItemFSLabel                 = 0
kMDItemFSName                  = "IMG_1331.JPG"
kMDItemFSNodeCount             = (null)
kMDItemFSOwnerGroupID          = 20
kMDItemFSOwnerUserID           = 501
kMDItemFSSize                  = 2163587
kMDItemFSTypeCode              = ""
kMDItemGPSDateStamp            = "2013:12:19"
kMDItemHasAlphaChannel         = 0
kMDItemImageDirection          = 310.9829545454546
kMDItemISOSpeed                = 80
kMDItemKind                    = "JPEG document"
kMDItemLatitude                = 47.6087
kMDItemLogicalSize             = 2163587
kMDItemLongitude               = -122.3407216666667
kMDItemOrientation             = 0
kMDItemPhysicalSize            = 2166784
kMDItemPixelCount              = 7990272
kMDItemPixelHeight             = 2448
kMDItemPixelWidth              = 3264
kMDItemProfileName             = "sRGB IEC61966-2.1"
kMDItemRedEyeOnOff             = 0
kMDItemResolutionHeightDPI     = 72
kMDItemResolutionWidthDPI      = 72
kMDItemTimestamp               = "16:19:48"
kMDItemWhiteBalance            = 0

As you can see, quite a huge amount of data is produced from the command, far more than you want, I’m betting!

To me, the most interesting parts are the camera info — camera name, focal length and ISO speed setting — the location of the photo — latitude / longitude — and the dimensions of the image.

For the first, look at kMDItemAcquisitionMake, kMDItemAcquisitionModel, kMDItemExposureTimeSeconds, kMDItemFNumber and kMDItemISOSpeed. The location is stored in kMDItemLatitude and kMDItemLongitude, and the dimensions are kMDItemPixelHeight and kMDItemPixelWidth.

From a scripting perspective, these are easily extracted and saved as named variables like this:

height=$(mdls IMG_1331.JPG | grep PixelHeight | awk '{print $3}')

Do that for the individual variables and you’ve got the values you want loaded up. Latitude and Longitude, for example? Like this:

lat=$(mdls IMG_1331.JPG | grep Latitude | awk '{print $3}')
long=$(mdls IMG_1331.JPG | grep Longitude | awk '{print $3}')
echo Photo was taken at $lat / $long

From this point I bet you can proceed with the script manipulation you seek!

Let’s Stay In Touch!

Never miss a single article, review or tutorial here on AskDaveTaylor, sign up for my fun weekly newsletter!
Name: 
Your email address:*
Please enter all required fields
Correct invalid entries
No spam, ever. Promise. Powered by FeedBlitz
Please choose a color:
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you, Dave, for all your helpful information by buying you a cup of coffee!
analyze photos, bash programming, command line, digital photography, exif, linux, mac os x, shell script, shell script programming

7 comments on “Can I analyze EXIF information on the Mac OS X command line?”

  1. Kuba Ober says:
    December 16, 2018 at 7:33 pm

    In almost all cases, the use of grep and awk together is baffling. Awk has made grep obsolete pretty much as soon as it got released. If you use awk, there’s never a need to use grep.

    grep foo | awk {bar}

    can be replaced with

    awk /foo/ {bar}

    Another pet peeve of mine: using cat to feed something in a pipe. It’s not needed. Like ever. Say

    cat foo | bar | baz

    can be generically replaced with

    bar < foo | baz

    Reply
    • Dave Taylor says:
      December 17, 2018 at 10:43 am

      Ahh, always a purist in the group. 🙂 I am always seeking the balance between maximally efficient and maximally readable and understandable. So x < y | z might make sense to you, but for someone learning the shell that can be a bit baffling.

      Reply
  2. Roland says:
    September 6, 2016 at 10:50 am

    Cool, thanks. I am missing all of the lens info. Where is this hidden? Appreciate any help!

    Reply
    • Dave Taylor says:
      September 7, 2016 at 8:44 am

      Not every camera or photo device records all the EXIF information. I think it’s all basically optional, so perhaps you’re missing the lens information because your camera isn’t recording it, Roland?

      Reply
  3. Steve says:
    August 25, 2016 at 12:59 pm

    The kMDItemOrientation field is not to be confused with the EXIF standard orientation field (which contains a value from 1..9 if memory serves). kMDItemOrientation is 0 or 1 based on whether the image is in portrait or landscape format.

    Reply
  4. karl says:
    February 26, 2014 at 2:41 am

    and forgotten…

    mdls -raw -name kMDItemLatitude IMG_1331.JPG

    will directly give the value, “47.6087” in your case.

    Reply
  5. karl says:
    February 26, 2014 at 2:39 am

    instead of grep you can directly access the data with

    mdls -name kMDItemLatitude IMG_1331.JPG

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Search

Recent Posts

  • Prevent “Printer Added” Notifications During Ubuntu Linux Boot?
  • Can I Use Google Maps to Find Cheap Gas and Great Coffee?
  • How Can I Make My Mouse Pointer and Text Cursor More Legible in Windows 11?
  • How to Enable “Hey Siri” on my iMac or MacBook?
  • How to Customize your Google Search Settings and Use Advanced Search

On Our YouTube Channel

Szanto Desert Sands Chronograph Watch (4553) -- REVIEW

Hagibis Nintendo Switch TV Dock -- DEMO & REVIEW

Categories

  • AdSense, AdWords, and PPC Help (106)
  • Amazon, eBay, and Online Shopping Help, (161)
  • Android Help (201)
  • Apple iPad Help (145)
  • Apple Watch Help (52)
  • Articles, Tutorials, and Reviews (344)
  • Auto Tech Help (11)
  • Business Advice (199)
  • Chrome OS Help (24)
  • Computer & Internet Basics (763)
  • d) None of the Above (165)
  • Facebook Help (383)
  • Google, Chrome & Gmail Help (179)
  • HTML & Web Page Design (245)
  • Instagram Help (47)
  • iPhone & iOS Help (607)
  • iPod & MP3 Player Help (173)
  • Kindle & Nook Help (93)
  • LinkedIn Help (85)
  • Linux Help (166)
  • Linux Shell Script Programming (87)
  • Mac & MacOS Help (893)
  • Most Popular (16)
  • Outlook & Office 365 Help (26)
  • PayPal Help (69)
  • Pinterest Help (53)
  • Reddit Help (18)
  • SEO & Marketing (81)
  • Spam, Scams & Security (92)
  • Trade Show News & Updates (23)
  • Twitter Help (217)
  • Video Game Tips (66)
  • Web Site Traffic Tips (62)
  • Windows PC Help (919)
  • Wordpress Help (204)
  • Writing and Publishing (72)
  • YouTube Help (46)
  • YouTube Video Reviews (159)
  • Zoom, Skype & Video Chat Help (57)

Archives

Social Connections:

Ask Dave Taylor


Follow Me on Pinterest
Follow me on Twitter
Follow me on LinkedIn
Follow me on Instagram


AskDaveTaylor on Facebook



microsoft insider mvp


This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this site or on any linked site. Further, please note that by submitting a question or comment you're agreeing to our terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site. Our lawyer says "Thanks for your cooperation."
© 2022 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions - Accessibility Policy