ExifTool Forum

ExifTool => Developers => Topic started by: dropf on November 04, 2022, 06:21:42 AM

Title: Renaming image files with PyExifTool 0.5.4
Post by: dropf on November 04, 2022, 06:21:42 AM
Hi guys, I already posted on stackoverflow (https://stackoverflow.com/questions/74305074/renaming-image-files-with-pyexiftool-0-5-4-exiftool-in-python) but I guess it's a little too specialized, so I'm triying here too.

I'm trying to rename jpg files from a python script with exiftool using PyExifTool 0.5.4. I can change tags, e.g. DateTimeOriginal, but when I try to rename files using tags I can't get the correct formatting for the filename.

with exiftool.ExifToolHelper() as et:
    et.execute('-d %Y-%m.%%e', '-filename<DateTimeOriginal', os.path.join(subdir, file))

When called from python this code seems to ignore the format given with -d and simply renames files to the content of the DateTimeOriginal tag, e.g. 2021:12:25 16:26:37. The same result is produced when I run it in a terminal without '-d %Y-%m.%%e'

Running the command from the terminal, e.g. exiftool -d %Y%m.%%e '-filename<DateTimeOriginal' b.jpg renames the file using YYYYmm as expected.
Title: Re: Renaming image files with PyExifTool 0.5.4
Post by: Phil Harvey on November 04, 2022, 07:01:10 AM
Maybe try putting -d and %Y-%m.%%e as separate arguments in the execute() call.

- Phil
Title: Re: Renaming image files with PyExifTool 0.5.4
Post by: dropf on November 04, 2022, 08:01:07 AM
Thanks for the quick reply Harvey (and obviously the exiftool ;) )

Unfortunately no change and I'm at a loss since the same command works in a terminal. It must have something to do with the argument processing but I also don't know how to debug further.
Title: Re: Renaming image files with PyExifTool 0.5.4
Post by: Phil Harvey on November 04, 2022, 09:09:22 AM
There are a couple of ways to progress here.  If you can see the console output from your exiftool command, you could use the -echo option to play with the arguments to be sure they are getting through properly.

Another idea would be to put your arguments in a file and use the -@ option to load them from the file instead of the command line.  But of course that assumes that the -@ will work in your execute() call.

- Phil
Title: Re: Renaming image files with PyExifTool 0.5.4
Post by: bparks0263 on December 27, 2022, 12:43:08 PM

Maybe it's a typo but the format string is different, in Python there is dash between %Y and %m, and not in the terminal command.