sylikc / pyexiftool

PyExifTool (active PyPI project) - A Python library to communicate with an instance of Phil Harvey's ExifTool command-line application. Runs one process with special -stay_open flag, and pipes data to/from. Much more efficient than running a subprocess for each command!
Other
148 stars 19 forks source link

datetime print format via `-d FMT` option ignored when `-n` option present #21

Closed beng closed 3 years ago

beng commented 3 years ago

Hey sylikc,

By default, exiftool uses : as the separator for the date aspect of all datetimes, eg: 2021:03:20 08:58:12. The CLI tool exposes a flag so that you can change how datetimes are formatted. I can add the below flag when running the CLI tool to render datetimes differently.

$ exiftool my-img.jpg -json -d "%Y-%m-%d %H:%M:%S%z"

[{
  "SourceFile": "my-img.jpg",
  ....
  "CreateDate": "2021-03-20 08:58:12-0400"
  ....
}]

Unfortunately, I am seeing different results when using the same flag and same file with pyexiftool. I have tried passing the datetime formatting parameter in via exiftool.get_metadata as well as exiftool.execute_json. I have also tried rendering the parameters in multiple ways. Regardless of how I render them, the only exifdata that is returned is "SourceFile". In the below example, if I omit passing in params, then the expected exifdata is returned, but uses the default rendering format of datetimes (as I would expect).

Python version 3.9.0

import exiftool

def extract_imagery_exifdata(source_filepath):
    with exiftool.ExifTool() as et:
        params = [exiftool.fsencode('-d "%Y-%m-%d %H:%M:%S%z"')]
        print(params)
        md = et.get_metadata(source_filepath, params=params)
        print(md)

>>> extract_imagery_exifdata("./my-img.jpg")

# note, that the params are being rendered correctly 
[b'-d "%Y-%m-%d %H:%M:%S%z"']

{'SourceFile': 'my-img.jpg'}

I haven't dug into the source code yet so I'm not sure if the issue is on my end or a small bug in the code, but would appreciate any guidance you may have. Thanks!

sylikc commented 3 years ago

I ran a few tests, and it appears that the -n flag (no print conversion) causes exiftool to ignore the -dateFormat parameter.

So, to get that code working, you would do the following

import exiftool

def extract_imagery_exifdata(source_filepath):
    with exiftool.ExifTool(common_args=['-G']) as et:
        params = [b'-d',b'"%Y-%m-%d %H:%M:%S%z"']
        print(params)
        md = et.get_metadata(source_filepath, params=params)
        print(md)

extract_imagery_exifdata(r"my-img.jpg")

in addition, as per above, passing parameters into the call is a list of SEPARATE parameters in each element. Don't bundle it together as a string with a space.

sylikc commented 3 years ago

changed the title as, the issue is that the params was not given as a list, and the bulk of the issue is that "-n" ignores "-d FMT".

I wouldn't say the ignoring is by design, but it is a "feature" of the underlying exiftool and one that can't be fixed

beng commented 3 years ago

hey @sylikc apologies about the delay, i missed the email a few days ago. really appreciate you looking into this and providing a work around. this will be super helpful.