2019-10-21 04:22:28 -04:00
---
2022-11-17 10:14:29 -05:00
title: Image Processing
description: Resize, crop, rotate, filter, and convert images.
categories: [content management]
2020-03-31 16:10:45 -04:00
keywords: [resources, images]
2019-10-21 04:22:28 -04:00
menu:
docs:
2022-11-17 10:14:29 -05:00
parent: content-management
weight: 90
toc: true
weight: 90
2019-10-21 04:22:28 -04:00
---
2022-03-26 05:04:57 -04:00
## Image Resources
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
To process an image, you must access the image as either a page resource or a global resource.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
### Page Resources
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
A page resource is a file within a [page bundle]. A page bundle is a directory with an `index.md` or `_index.md` file at its root.
```text
content/
└── posts/
└── post-1/ < -- page bundle
├── index.md
└── sunset.jpg < -- page resource
```
### Global Resources
A global resource is a file:
- Within the `assets` directory, or
- Within any directory [mounted] to the `assets` directory, or
- Located on a remote server accessible via `http` or `https`
```text
assets/
└── images/
└── sunset.jpg < -- global resource
```
To access a local image as a global resource:
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image := resources.Get "images/sunset.jpg" }}
```
To access a remote image as a global resource:
```go-html-template
{{ $image := resources.GetRemote "https://gohugo.io/img/hugo-logo.png" }}
```
## Image Rendering
Once you have accessed an image as either a page resource or a global resource, render it in your templates using the `Permalink` , `RelPermalink` , `Width` , and `Height` properties.
Example 1: Throws an error if the resource is not found.
```go-html-template
{{ $image := .Resources.GetMatch "sunset.jpg" }}
< img src = "{{ $image.RelPermalink }}" width = "{{ $image.Width }}" height = "{{ $image.Height }}" >
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
Example 2: Skips image rendering if the resource is not found.
```go-html-template
{{ $image := .Resources.GetMatch "sunset.jpg" }}
{{ with $image }}
< img src = "{{ .RelPermalink }}" width = "{{ .Width }}" height = "{{ .Height }}" >
{{ end }}
```
2020-05-18 09:24:58 -04:00
2022-03-26 05:04:57 -04:00
Example 3: A more concise way to skip image rendering if the resource is not found.
2020-05-18 09:24:58 -04:00
2021-03-21 08:31:17 -04:00
```go-html-template
2022-03-26 05:04:57 -04:00
{{ with .Resources.GetMatch "sunset.jpg" }}
< img src = "{{ .RelPermalink }}" width = "{{ .Width }}" height = "{{ .Height }}" >
{{ end }}
2021-03-21 08:31:17 -04:00
```
2020-05-18 09:24:58 -04:00
2019-10-21 04:22:28 -04:00
## Image Processing Methods
2022-11-17 10:14:29 -05:00
The `image` resource implements the [`Resize`], [`Fit`], [`Fill`], [`Crop`], [`Filter`], [`Colors`] and [`Exif`] methods.
2021-03-21 08:31:17 -04:00
{{% note %}}
2022-11-17 10:14:29 -05:00
Metadata (Exif, IPTC, XMP, etc.) is not preserved during image transformation. Use the [`Exif`] method with the _original_ image to extract Exif metadata from JPEG or TIFF images.
2021-03-21 08:31:17 -04:00
{{% /note %}}
2019-10-21 04:22:28 -04:00
### Resize
2022-03-26 05:04:57 -04:00
Resize an image to the specified width and/or height.
If you specify both width and height, the resulting image will be disproportionally scaled unless the original image has the same aspect ratio.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{/* Resize to a width of 600px and preserve aspect ratio */}}
{{ $image := $image.Resize "600x" }}
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
{{/* Resize to a height of 400px and preserve aspect ratio */}}
{{ $image := $image.Resize "x400" }}
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
{{/* Resize to a width of 600px and a height of 400px */}}
{{ $image := $image.Resize "600x400" }}
2019-10-21 04:22:28 -04:00
```
### Fit
2020-03-31 16:10:45 -04:00
2022-03-26 05:04:57 -04:00
Downscale an image to fit the given dimensions while maintaining aspect ratio. You must provide both width and height.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image := $image.Fit "600x400" }}
2019-10-21 04:22:28 -04:00
```
### Fill
2020-03-31 16:10:45 -04:00
2022-03-26 05:04:57 -04:00
Crop and resize an image to match the given dimensions. You must provide both width and height. Use the [`anchor`] option to change the crop box anchor point.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image := $image.Fill "600x400" }}
2019-10-21 04:22:28 -04:00
```
2022-03-08 13:37:17 -05:00
### Crop
2022-03-26 05:04:57 -04:00
Crop an image to match the given dimensions without resizing. You must provide both width and height. Use the [`anchor`] option to change the crop box anchor point.
2022-03-08 13:37:17 -05:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image := $image.Crop "600x400" }}
2022-03-08 13:37:17 -05:00
```
2019-10-21 04:22:28 -04:00
### Filter
2022-03-26 05:04:57 -04:00
Apply one or more [filters] to an image.
2019-10-21 04:22:28 -04:00
```go-html-template
2022-03-26 05:04:57 -04:00
{{ $image := $image.Filter (images.GaussianBlur 6) (images.Pixelate 8) }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
Write this in a more functional style using pipes. Hugo applies the filters in the order given.
2019-10-21 04:22:28 -04:00
```go-html-template
2022-03-26 05:04:57 -04:00
{{ $image := $image | images.Filter (images.GaussianBlur 6) (images.Pixelate 8) }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
Sometimes it can be useful to create the filter chain once and then reuse it.
2019-10-21 04:22:28 -04:00
```go-html-template
{{ $filters := slice (images.GaussianBlur 6) (images.Pixelate 8) }}
2022-03-26 05:04:57 -04:00
{{ $image1 := $image1.Filter $filters }}
{{ $image2 := $image2.Filter $filters }}
2019-10-21 04:22:28 -04:00
```
2022-11-17 10:14:29 -05:00
### Colors
{{< new-in " 0 . 104 . 0 " > }}
`.Colors` returns a slice of hex strings with the dominant colors in the image using a simple histogram method.
```go-html-template
{{ $colors := $image.Colors }}
```
This method is fast, but if you also scale down your images, it would be good for performance to extract the colors from the scaled down image.
2019-10-21 04:22:28 -04:00
### Exif
2022-03-26 05:04:57 -04:00
Provides an [Exif] object containing image metadata.
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
You may access Exif data in JPEG and TIFF images. To prevent errors when processing images without Exif data, wrap the access in a [`with`] statement.
2019-10-21 04:22:28 -04:00
```go-html-template
2022-03-26 05:04:57 -04:00
{{ with $image.Exif }}
Date: {{ .Date }}
Lat/Long: {{ .Lat }}/{{ .Long }}
Tags:
{{ range $k, $v := .Tags }}
TAG: {{ $k }}: {{ $v }}
{{ end }}
2019-12-15 04:35:09 -05:00
{{ end }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
You may also access Exif fields individually, using the [`lang.FormatNumber`] function to format the fields as needed.
2020-03-09 15:19:32 -04:00
```go-html-template
2022-03-26 05:04:57 -04:00
{{ with $image.Exif }}
2020-03-31 16:10:45 -04:00
< ul >
2022-03-26 05:04:57 -04:00
{{ with .Date }}< li > Date: {{ .Format "January 02, 2006" }}< / li > {{ end }}
{{ with .Tags.ApertureValue }}< li > Aperture: {{ lang.FormatNumber 2 . }}< / li > {{ end }}
{{ with .Tags.BrightnessValue }}< li > Brightness: {{ lang.FormatNumber 2 . }}< / li > {{ end }}
{{ with .Tags.ExposureTime }}< li > Exposure Time: {{ . }}< / li > {{ end }}
{{ with .Tags.FNumber }}< li > F Number: {{ . }}< / li > {{ end }}
{{ with .Tags.FocalLength }}< li > Focal Length: {{ . }}< / li > {{ end }}
{{ with .Tags.ISOSpeedRatings }}< li > ISO Speed Ratings: {{ . }}< / li > {{ end }}
{{ with .Tags.LensModel }}< li > Lens Model: {{ . }}< / li > {{ end }}
2020-03-31 16:10:45 -04:00
< / ul >
2020-03-09 15:19:32 -04:00
{{ end }}
```
2022-03-26 05:04:57 -04:00
#### Exif Variables
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
.Date
: Image creation date/time. Format with the [time.Format] function.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
.Lat
: GPS latitude in degrees.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
.Long
: GPS longitude in degrees.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
.Tags
: A collection of the available Exif tags for this image. You may include or exclude specific tags from this collection in the [site configuration ](#exif-data ).
2019-10-21 04:22:28 -04:00
## Image Processing Options
2022-11-17 10:14:29 -05:00
The [`Resize`], [`Fit`], [`Fill`], and [`Crop`] methods accept a space-separated, case-insensitive list of options. The order of the options within the list is irrelevant.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
### Dimensions
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
With the [`Resize`] method you must specify width, height, or both. The [`Fit`], [`Fill`], and [`Crop`] methods require both width and height. All dimensions are in pixels.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image := $image.Resize "600x" }}
{{ $image := $image.Resize "x400" }}
{{ $image := $image.Resize "600x400" }}
{{ $image := $image.Fit "600x400" }}
{{ $image := $image.Fill "600x400" }}
{{ $image := $image.Crop "600x400" }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
### Rotation
Rotates an image counter-clockwise by the given angle. Hugo performs rotation _before_ scaling. For example, if the original image is 600x400 and you wish to rotate the image 90 degrees counter-clockwise while scaling it by 50%:
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image = $image.Resize "200x r90" }}
```
2019-10-21 04:49:16 -04:00
2022-03-26 05:04:57 -04:00
In the example above, the width represents the desired width _after_ rotation.
2020-03-31 16:10:45 -04:00
2022-03-26 05:04:57 -04:00
To rotate an image without scaling, use the dimensions of the original image:
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ with .Resources.GetMatch "sunset.jpg" }}
{{ with .Resize (printf "%dx%d r90" .Height .Width) }}
< img src = "{{ .RelPermalink }}" width = "{{ .Width }}" height = "{{ .Height }}" >
{{ end }}
{{ end }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
In the example above, on the second line, we have reversed width and height to reflect the desired dimensions _after_ rotation.
2021-04-20 14:21:45 -04:00
2022-03-26 05:04:57 -04:00
### Anchor
2021-04-20 14:21:45 -04:00
2022-11-17 10:14:29 -05:00
When using the [`Crop`] or [`Fill`] method, the _anchor_ determines the placement of the crop box. You may specify `TopLeft` , `Top` , `TopRight` , `Left` , `Center` ,`Right`, `BottomLeft` , `Bottom` , `BottomRight` , or `Smart` .
2021-06-08 12:46:58 -04:00
2022-11-17 10:14:29 -05:00
The default value is `Smart` , which uses [Smartcrop] image analysis to determine the optimal placement of the crop box. You may override the default value in the [site configuration].
2021-06-18 11:49:54 -04:00
2022-03-26 05:04:57 -04:00
For example, if you have a 400x200 image with a bird in the upper left quadrant, you can create a 200x100 thumbnail containing the bird:
2021-04-20 14:21:45 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image.Crop "200x100 TopLeft" }}
```
2022-11-17 10:14:29 -05:00
If you apply [rotation ](#rotation ) when using the [`Crop`] or [`Fill`] method, specify the anchor relative to the rotated image.
2022-03-26 05:04:57 -04:00
### Target Format
By default, Hugo encodes the image in the source format. You may convert the image to another format by specifying `bmp` , `gif` , `jpeg` , `jpg` , `png` , `tif` , `tiff` , or `webp` .
```go-html-template
{{ $image.Resize "600x webp" }}
```
2021-04-20 14:21:45 -04:00
2022-03-26 05:04:57 -04:00
To convert an image without scaling, use the dimensions of the original image:
2021-04-20 14:21:45 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ with .Resources.GetMatch "sunset.jpg" }}
{{ with .Resize (printf "%dx%d webp" .Width .Height) }}
< img src = "{{ .RelPermalink }}" width = "{{ .Width }}" height = "{{ .Height }}" >
{{ end }}
{{ end }}
2021-04-20 14:21:45 -04:00
```
2022-03-26 05:04:57 -04:00
### Quality
Applicable to JPEG and WebP images, the `q` value determines the quality of the converted image. Higher values produce better quality images, while lower values produce smaller files. Set this value to a whole number between 1 and 100, inclusive.
2020-03-31 16:10:45 -04:00
2022-11-17 10:14:29 -05:00
The default value is 75. You may override the default value in the [site configuration].
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image.Resize "600x webp q50" }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
### Hint
<!-- Specifies a libwebp preset, not a libwebp image hint. -->
2020-03-31 16:10:45 -04:00
2022-11-17 10:14:29 -05:00
Applicable to WebP images, this option corresponds to a set of predefined encoding parameters.
2020-05-31 06:43:23 -04:00
2022-03-26 05:04:57 -04:00
Value|Example
:--|:--
`drawing` |Hand or line drawing with high-contrast details
`icon` |Small colorful image
`photo` |Outdoor photograph with natural lighting
`picture` |Indoor photograph such as a portrait
`text` |Image that is primarily text
2020-05-31 06:43:23 -04:00
2022-11-17 10:14:29 -05:00
The default value is `photo` . You may override the default value in the [site configuration].
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image.Resize "600x webp picture" }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
### Background Color
2020-03-31 16:10:45 -04:00
2022-03-26 05:04:57 -04:00
When converting an image from a format that supports transparency (e.g., PNG) to a format that does _not_ support transparency (e.g., JPEG), you may specify the background color of the resulting image.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
Use either a 3-digit or a 6-digit hexadecimal color code (e.g., `#00f` or `#0000ff` ).
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
The default value is `#ffffff` (white). You may override the default value in the [site configuration].
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image.Resize "600x jpg #b31280 " }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
### Resampling Filter
You may specify the resampling filter used when resizing an image. Commonly used resampling filters include:
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
Filter|Description
:--|:--
`Box` |Simple and fast averaging filter appropriate for downscaling
`Lanczos` |High-quality resampling filter for photographic images yielding sharp results
`CatmullRom` |Sharp cubic filter that is faster than the Lanczos filter while providing similar results
`MitchellNetravali` |Cubic filter that produces smoother results with less ringing artifacts than CatmullRom
`Linear` |Bilinear resampling filter, produces smooth output, faster than cubic filters
`NearestNeighbor` |Fastest resampling filter, no antialiasing
2019-10-21 04:22:28 -04:00
2022-11-17 10:14:29 -05:00
The default value is `Box` . You may override the default value in the [site configuration].
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
```go-html-template
{{ $image.Resize "600x400 Lanczos" }}
2019-10-21 04:22:28 -04:00
```
2022-03-26 05:04:57 -04:00
See [github.com/disintegration/imaging] for the complete list of resampling filters. If you wish to improve image quality at the expense of performance, you may wish to experiment with the alternative filters.
2021-06-08 12:46:58 -04:00
2019-10-21 04:22:28 -04:00
## Image Processing Examples
_The photo of the sunset used in the examples below is Copyright [Bjørn Erik Pedersen ](https://commons.wikimedia.org/wiki/User:Bep ) (Creative Commons Attribution-Share Alike 4.0 International license)_
{{< imgproc sunset Resize " 300x " / > }}
{{< imgproc sunset Fill " 90x120 left " / > }}
{{< imgproc sunset Fill " 90x120 right " / > }}
{{< imgproc sunset Fit " 90x90 " / > }}
2022-03-08 13:37:17 -05:00
{{< imgproc sunset Crop " 250x250 center " / > }}
2019-10-21 04:22:28 -04:00
{{< imgproc sunset Resize " 300x q10 " / > }}
2022-03-26 05:04:57 -04:00
This is the shortcode used to generate the examples above:
2019-10-21 04:22:28 -04:00
{{< code file = "layouts/shortcodes/imgproc.html" > }}
2022-11-17 10:14:29 -05:00
{{< readfile file = "layouts/shortcodes/imgproc.html" > }}
2019-10-21 04:22:28 -04:00
{{< / code > }}
2022-11-17 10:14:29 -05:00
Call the shortcode from your Markdown like this:
2019-10-21 04:22:28 -04:00
```go-html-template
{{< /* imgproc sunset Resize "300x" /*/>}}
```
{{% note %}}
2022-03-26 05:04:57 -04:00
Note the self-closing shortcode syntax above. You may call the `imgproc` shortcode with or without **inner content** .
2019-10-21 04:22:28 -04:00
{{% /note %}}
2022-03-26 05:04:57 -04:00
## Imaging Configuration
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
### Processing Options
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
Define an `imaging` section in your site configuration to set the default [image processing options ](#image-processing-options ).
2019-10-21 04:22:28 -04:00
2023-05-27 10:59:59 -04:00
{{< code-toggle file = "hugo" copy = true > }}
2022-03-26 05:04:57 -04:00
[imaging]
resampleFilter = "Box"
2019-10-21 04:22:28 -04:00
quality = 75
2021-04-20 14:21:45 -04:00
hint = "photo"
2022-03-26 05:04:57 -04:00
anchor = "Smart"
2019-10-21 04:22:28 -04:00
bgColor = "#ffffff"
2022-03-26 05:04:57 -04:00
{{< / code-toggle > }}
anchor
: See image processing options: [anchor ](#anchor ).
bgColor
: See image processing options: [background color ](#background-color ).
hint
: See image processing options: [hint ](#hint ).
quality
: See image processing options: [quality ](#quality ).
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
resampleFilter
: See image processing options: [resampling filter ](#resampling-filter ).
### Exif Data
Define an `imaging.exif` section in your site configuration to control the availability of Exif data.
2023-05-27 10:59:59 -04:00
{{< code-toggle file = "hugo" copy = true > }}
2019-10-21 04:22:28 -04:00
[imaging.exif]
includeFields = ""
excludeFields = ""
disableDate = false
disableLatLong = false
2022-03-26 05:04:57 -04:00
{{< / code-toggle > }}
disableDate
: Hugo extracts the image creation date/time into `.Date` . Set this to `true` to disable. Default is `false` .
disableLatLong
: Hugo extracts the GPS latitude and longitude into `.Lat` and `.Long` . Set this to `true` to disable. Default is `false` .
excludeFields
: Regular expression matching the Exif tags to exclude from the `.Tags` collection. Default is `""`.
includeFields
: Regular expression matching the Exif tags to include in the `.Tags` collection. Default is `""`. To include all available tags, set this value to `".*"`.
{{% note %}}
To improve performance and decrease cache size, if you set neither `excludeFields` nor `includeFields` , Hugo excludes the following tags: `ColorSpace` , `Contrast` , `Exif` , `Exposure[M|P|B]` , `Flash` , `GPS` , `JPEG` , `Metering` , `Resolution` , `Saturation` , `Sensing` , `Sharp` , and `WhiteBalance` .
{{% /note %}}
2019-10-21 04:22:28 -04:00
## Smart Cropping of Images
2022-03-26 05:04:57 -04:00
By default, Hugo uses the [Smartcrop] library when cropping images with the `Crop` or`Fill` methods. You can set the anchor point manually, but in most cases the `Smart` option will make a good choice.
2019-10-21 04:22:28 -04:00
2022-03-08 13:37:17 -05:00
Examples using the sunset image from above:
2019-10-21 04:22:28 -04:00
{{< imgproc sunset Fill " 200x200 smart " / > }}
2022-03-08 13:37:17 -05:00
{{< imgproc sunset Crop " 200x200 smart " / > }}
2019-10-21 04:22:28 -04:00
## Image Processing Performance Consideration
2022-03-26 05:04:57 -04:00
Hugo caches processed images in the `resources` directory. If you include this directory in source control, Hugo will not have to regenerate the images in a CI/CD workflow (e.g., GitHub Pages, GitLab Pages, Netlify, etc.). This results in faster builds.
2019-10-21 04:22:28 -04:00
2022-03-26 05:04:57 -04:00
If you change image processing methods or options, or if you rename or remove images, the `resources` directory will contain unused images. To remove the unused images, perform garbage collection with:
2019-10-21 04:22:28 -04:00
```bash
hugo --gc
```
2023-05-22 10:43:12 -04:00
[time.Format]: /functions/dateformat
[`anchor`]: /content-management/image-processing#anchor
[mounted]: /hugo-modules/configuration#module-config-mounts
[page bundle]: /content-management/page-bundles
[`lang.FormatNumber`]: /functions/lang
[filters]: /functions/images
2022-03-26 05:04:57 -04:00
[github.com/disintegration/imaging]: < https: / / github . com / disintegration / imaging # image-resizing >
[Smartcrop]: < https: / / github . com / muesli / smartcrop # smartcrop >
2023-05-22 10:43:12 -04:00
[Exif]: < https: / / en . wikipedia . org / wiki / Exif >
2022-11-17 10:14:29 -05:00
[`Colors`]: #colors
[`Crop`]: #crop
[`Exif`]: #exif
[`Fill`]: #fill
[`Filter`]: #filter
[`Fit`]: #fit
[`Resize`]: #resize
[site configuration]: #processing -options
[`with`]: /functions/with/