0

I am having trouble figuring out how to extract an EXIF tag from a raw photo using PyExifTool. All I want to do is extract the date the photo was created and rename the project folder with that date. I've tried a variety of things I keep getting a series of errors ending with "ValueError("No JSON object could be decoded").

Like:

    import exiftool
    files = "CRW_1368.CRW"

    with exiftool.ExifTool() as et:
        metadata = et.get_tag(DateTimeOriginal, files)

    print execute_json(metadata)

It's not clear to me how to properly set it up. I am relatively new to scripting.

Thanks for the help!

  • This question is not in accordance with StackOverflow's guidelines. `Nothing seems to work` does not properly describe the problem. What happens? What error codes do you receive? Since it is a salvageable question, i will not, at this time, vote to close it. But do edit it quick because, otherwise, someone else will close this question down. – tony gil Feb 18 '17 at 23:13

1 Answers1

0

First, you have to make sure that the tag exists. In my test, there was no DateTimeOriginal tag, so I had to select a tag that was actually in my file. I chose 'DateCreated'. I got the error that execute_json wasn't defined. When I changed it to et.execute_json, I was able to get your same error. Since you're only pulling one tag, you should be able to just print it. By a quick change of your print statement, I got the expected date/time.

import exiftool
import os, errno
files = file.jpg

with exiftool.ExifTool() as et:
    metadata = et.get_tag('DateCreated', files)
    print(metadata)
# or, (skipping those two lines) as per your actual question
    new_folder = et.get_tag('DateCreated', files)
    try:
        os.makedirs(new_folder)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise

And in my case, with the print statement, got back the expected '2017:04:25 17:40:42' from my file.

For updated alternatives to directory checking/creating, see: How can I create a directory if it does not exist?

LOlliffe
  • 1,538
  • 5
  • 18
  • 40