How to exec exiftool from a hugo template?

There was discussion a while back about the ability to do an arbitrary exec from hugo templates, but it looks like the idea died due to security concerns.

I need to call exiftool from hugo templates (yes, the $img.Exif ability is nice, but it has a very limited set of tags available, and doesn’t provide access to basic values like the width and height of an image).

So is there any way I can invoke exiftool from inside a hugo template? I’m not building a theme, and am fine with whatever hack or workaround would allow me to do this (including invoking Go, if someone can point me in the right direction).


The only sensible way is to write a script that lives outside of Hugo that writes this Exif info to a file that can be read by Hugo.

You can generate json output from exiftool like this:

exiftool -json -w exif-output.json arb-photo.jpg

You could then use Hugo data templates to get to the data from your templates.

You’d have to copy/move this .json output into the ./data directory and then use standard Hugo .Site.Data.whateveryourfilenameis to get to the values.

Depending on how known/hard-coded your file-names are, you might want to use index to get to the appropriate data key in the .Data collection.

Like @bep is saying, running exiftool & moving and renaming the output json file into the the data dir would have to happen outside of Hugo.

Thanks @bep and @yaythomas. Is there any sort of hook I could tie executing an external script into, so that every time Hugo rebuilt the site (whether in server mode or not) it would run my exiftool script and regenerate the data files?

Not unless you’re wanting to fork Hugo and pop your own insertion point in. . .

Easier is, create a file ./hugo in your project dir, give it execution rights (chmod+x, assuming you’re on a sensible O/S :laughing: ) and the file would look something like this:

#!/usr/bin/env bash
# do exif stuff here
exiftool -json -w exif-output.json arb-photo.jpg
mv exif-output.json data/
# call Hugo, passing input args straight through so it works like the Hugo cmd normally does
path/to/your/hugo "$@"

In this case you can run from your project dir:

# run your exif stuff and then Hugo
$ ./hugo server
# just run Hugo
$ hugo server