ASK DAVE TAYLOR

ASK DAVE TAYLOR

  • Home
  • Video Help
  • Latest Reviews
  • Most Popular
  • Top Categories
  • About Dave
  • Ask!
  • Home
  • / HTML and Web 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 and Web Design, Linux Shell Script Programming, MacOS X 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 *

© 2018 by Dave Taylor. "Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.
Privacy Policy - Terms and Conditions

OVERWHELMED BY TECH?

You are not alone! Join our weekly newsletter to build up your confidence and capabilities in solving everyday technology problemss. Learn to master all your tech and gadgets!

“I love your newsletter. Keep up the very good work!”
– Julie Redd

“Thank you, Dave. I really do appreciate your updates.”
– Peter Kimball

Please wait...
Please enter all required fields Click to hide
Correct invalid entries Click to hide

 
  •  
  • More Info

    Search

    Recent Posts

    • How Can I Work with Random Numbers in the Linux Shell?
    • How Can I Format a Table in Microsoft Word?
    • How Can I Add a Christmas Desktop Theme to my Windows PC?
    • How Much Money did the Movie “Shutter Island” Earn?
    • How Can I Free Up Space on My Android Phone?

    On Our YouTube Channel

    HITCASE TrueLux Clip-On Camera Lenses -- DEMO & REVIEW

    Categories

    • AdSense, AdWords and PPC Help (106)
    • Amazon Echo & Kindle Help (74)
    • Android Help (136)
    • Apple Watch Help (47)
    • Articles, Tutorials and Reviews (322)
    • Business Advice (182)
    • Computer and Internet Basics (686)
    • d) None of the Above (158)
    • eBay, Amazon and Online Shopping Help (150)
    • Facebook Help (343)
    • Gmail Tips & Help (119)
    • Google Plus Support (59)
    • HTML and Web Design (238)
    • Instagram Help (40)
    • iPad Help (129)
    • iPhone Help (542)
    • iPod and MP3 Player Help (181)
    • LinkedIn Help (72)
    • Linux Help (137)
    • Linux Shell Script Programming (86)
    • MacOS X Help (779)
    • Most Popular (5)
    • Pinterest Help (51)
    • SEO Tips and Tricks (78)
    • Trade Show Updates (23)
    • Twitter Help (205)
    • Video Game Tips and Reviews (66)
    • Web Site Traffic Tips (61)
    • Windows Help (749)
    • Wordpress Help (197)
    • Writing and Publishing (70)
    • YouTube Help (40)
    • YouTube Video Reviews (159)

    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


  •  
  • Popular Pages

    • Home
  • Stay In Touch

    • Twitter
    • Facebook
 
© askdavetaylor.com. All rights reserved.
  • Privacy Policy