Exact pixel offset for crop

I was surprised to find no support for an exact pixel offset for the image crop operation.

None of the built-in anchor keywords will work for my use case.

I don’t want to preprocess in another tool (extra compression artefacts), nor do I want to fake it once it’s already in the user’s browser (sending data to the user they don’t need).

I suggest that the crop operation should allow a custom offset specifier, perhaps in the same format Imagemagick and the like use, such as “+100+244”, meaning the top-left of the area to crop to is 100 pixels from the original left edge and 244 pixels from the original top edge.

$crop := .Crop "3994x3994" "+100+244" would then give a 3994x3994 image which has discarded 100 columns from the left and 244 rows from the top, and whatever is beyond the next 3994 rows/columns.

or maybe just $crop := .Crop "3994x3994+100+244" to be the same as the Imagemagick form, if that’s possible. In that case it would be disallowed to also include an anchor.

I am working around it for now like this:

{{ with resources.Get "xyz.jpg" }}
  {{ $tmpCrop := .Crop (printf "%dx%d BottomRight" (sub .Width 102) (sub .Height 244)) }}
  {{ $crop := $tmpCrop.Crop "3994x3994 TopLeft" }}
  ...

https://gohugo.io/functions/images/padding

Negative padding values will crop the image.

I have added a “Tip” callout to https://gohugo.io/methods/resource/crop that points to https://gohugo.io/functions/images/padding, and the latter now includes negative padding recipes at the bottom of the page.