HUGO

Properly formatting EXIF data tag "Exposure time"

I’m having some problems properly formatting the “exposure time” exif tag. According to this Github issue comment:

If the numerator is 1 (as in 1/500), I will use the rational number, else convert it to a float.

Here are some values I am getting, and some of them are not very readable:

  • Exposure time: 0.016666666666666666 s
  • Exposure time: 0.025 s
  • Exposure time: 0.016158 s
  • Exposure time: 1/317 s

But since the value can also be a rational number, I’m getting an error trying to use lang.NumFmt.

What is the best approach here?

That comment was probably from me.

Hmm. OK, I think your best bet would be to test the type with printf. e.g:

{{ $type := printf "%T" .ExposureTime }}
{{ if eq $type "float64" }}
// Use NumFmt
{{ else }}
// Print the rational as is.
{{ end }}

1 Like

Thanks, that works :slight_smile:

But looking at some of these numbers;

  • 0.016666666666666666 = 1/60
  • 0.025 = 1/40

Shouldn’t these, by your comment, be displayed as rational numbers?

My comment (if I remember correctly) was that numbers with 1 as numerator was kept as a rational numbers, others converted to float. It’s hard for me looking at your examples if there is a flaw in that logic, but please open a GitHub issue if you think it is. I mostly used a gallery from my Pentax K3 camera to get something that looked sensible.

If your camera saves them as 0.016666666666666666 how would Hugo (Go) know that they could be represented as rational numbers? They seem to actually be NOT rational numbers in the EXIF fields. It’s about what they are, not what they might represent at that point.

How my camera actually stores it in the exif data I don’t know.

But both Unsplash and exifdata reports 1/60 as the exposure time:

"ExposureTime": "1/60"

Sounds like a bug then :slight_smile:

Try something like this?

layouts/partials/format-exposure-time.html

{{ $n := "- . ," }}   {{/* numFmt options */}}
{{ $p := 9 }}         {{/* precision */}}

{{ $e := "" }}

{{ if eq (printf "%T" .) "float64" }}
  {{ $r := div 1 . }}
  {{ if math.ModBool (mul $r (pow 10 $p)) (mul (int $r) (pow 10 $p)) }}
    {{ $e = printf "1/%d s" (int $r) }}
  {{ else }}
    {{ $e = printf "%s s" (lang.NumFmt $p . $n ) }}
  {{ end }}
{{ else }}
  {{ $e = printf "%s s" . }}
{{ end }}

{{ return $e }}

Then call it like this:

{{ with resources.Get "a.jpg" }}
  {{ with .Exif }}
    {{ with .Tags.ExposureTime }}
      {{ partial "format-exposure-time.html" . }}
    {{ end }}
  {{ end }}
{{ end }}

If it’s stored as a rational, print as-is.
If it’s stored as a float that is really a rational, convert it to a rational (depends on precision value).
Otherwise display it as a formatted float.

1 Like

Oh wow! I can confirm this works great :smiley:

image

I’ve also experimented with creating a json file with exifdata and reading this in the layout:

{{ with .Page.Resources.GetMatch "exif.json" }}

{{ with (index (.Content | unmarshal) 0) }}
  <ul>
    {{ with .Make }}<li>Camera make: {{ . }}</li>{{ end }}
    {{ with .Model }}<li>Camera model: {{ . }}</li>{{ end }}
    {{ with .LensModel }}<li>Lens model: {{ . }}</li>{{ end }}
    {{ with .FocalLength }}<li>Focal length: {{ . }}</li>{{ end }}
    {{ with .FNumber }}<li>Aperture: ƒ/{{ lang.NumFmt 1 . }}</li>{{ end }}
    {{ with .ExposureTime }}<li>Exposure time: {{ . }} s</li>{{ end }}
    {{ with .ISO }}<li>ISO: {{ . }}</li>{{ end }}
  </ul>

  <a href="{{ .SourceFile | absURL }}">Download this photo</a>
{{ end }}
{{ end }}

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.