It’s also worth noting that Hugo’s Webp support requires the extended version, so you may also want to do something like (esp. if you’re creating a theme):
{{ $images := slice }}
{{ $images = $images | append ($img.Resize "300x") }}
{{ if hugo.IsExtended }}
{{ $images = $images | append ($img.Resize "300x webp") }}
{{ end }}