Preview Image

Smaller Jekyll Images with an Automated WebP Workflow

A practical WebP workflow for Jekyll, including dry runs, image backups, reference updates and honest performance measurements.

Smaller Jekyll Images with an Automated WebP Workflow

Large photos were making my Jekyll blog heavier than it needed to be. I wrote a Bash script to resize images, convert them to WebP, and update the references in my posts.

My original comparison was roughly 37 MB before and 1.3 MB after—about 96.5% less image data for that batch. Those rounded figures describe file size, not a controlled page-speed benchmark. Your result will depend on the source images, dimensions, and quality settings.

What the script does

The optimizer in this blog’s repository processes PNG, JPG, and JPEG files under assets/img/posts/:

  1. Back up the original image under assets/img/posts/.originals/, preserving its relative path.
  2. Resize it to fit within 1200 × 800 pixels, preserving aspect ratio and avoiding upscaling.
  3. Write a WebP image at quality 90 by default and remove metadata.
  4. Replace matching filenames in Markdown files under _posts/.
  5. Remove the source image after a successful conversion.

It skips existing WebP files. For different dimensions or compression, pass --width, --height, or --quality.

Before running it

Work from a clean Git checkout and keep a separate copy of valuable originals. The script changes both images and article text. Its reference replacement matches filenames across posts, so repeated names such as image1.png in unrelated folders deserve particular attention in the diff.

Image backups are stored locally; they are not a substitute for a backup of the repository and its Markdown.

Install ImageMagick

1
2
3
4
5
6
# Ubuntu / Debian
sudo apt-get update
sudo apt-get install imagemagick

# macOS with Homebrew
brew install imagemagick

The script supports ImageMagick’s magick command and the older convert command. Check that your installation lists WebP support:

1
2
magick -list format | grep WEBP
# With ImageMagick 6, use: convert -list format | grep WEBP

Preview, optimize, review

From the blog repository root:

1
2
3
4
5
6
7
8
9
10
11
12
# Inspect the supported options
bash tools/optimize-images.sh --help

# Preview without modifying images or posts
bash tools/optimize-images.sh --dry-run

# Convert eligible source images
bash tools/optimize-images.sh

# Inspect all changed references and files
git diff --stat
git diff -- _posts

Build and preview the result before committing:

1
2
JEKYLL_ENV=production bundle exec jekyll build
bundle exec jekyll serve

Check detailed screenshots at full size, as well as thumbnails on the homepage. Text, thin PCB traces, and UI icons can reveal compression artifacts more clearly than photographs.

How resizing and quality work

The conversion uses this general ImageMagick pattern:

1
magick input.jpg -resize '1200x800>' -quality 90 -strip output.webp

The > prevents upscaling; the dimensions describe a bounding box, not a forced crop. Quality 90 is an encoder setting, not a promise to retain 90% of the original image information. -strip removes metadata, so retain the original if you need its camera details or other embedded information.

For a wider image or a different compression setting:

1
bash tools/optimize-images.sh --width 1600 --height 1200 --quality 85

Use those settings while the source PNG/JPEG files are still available. Existing WebP outputs are not inputs to the script, even with --force.

Backups and restoration

The backup layout is:

1
2
3
4
5
6
assets/img/posts/
├── .originals/
│   └── my-project/
│       └── photo.jpg
└── my-project/
    └── photo.webp

To restore images from those backups and reverse matching Markdown references:

1
2
bash tools/optimize-images.sh --restore
git diff -- _posts assets/img/posts

Review the restored references carefully, especially if several source formats shared a basename. For an exact return to an earlier published state, use your Git history together with your original-image backup.

Choosing a format

  • Photos: lossy WebP is a useful starting point; compare output size and appearance.
  • Screenshots: consider lossless WebP or PNG when small text needs to stay crisp. The script does not expose a lossless mode.
  • Logos and line art: SVG can be a better fit when a vector original exists.
  • Transparent images: WebP supports transparency; PNG is not required just because an image has an alpha channel.

Google’s WebP documentation covers its lossy, lossless, and transparency support. ImageMagick documents resize geometry in more detail.

Measure the result

Compare the original and optimized files for the same image set. Then test the same page before and after, using the same device and network conditions. Reduced image bytes help, but a smaller image folder alone does not establish a particular improvement in load time, search ranking, or bounce rate.

PageSpeed Insights report captured after image optimization An example report from this blog. Scores vary between runs and do not isolate image optimization from other changes.

For my workflow, the useful part is repeatability: add the original images, preview the conversion, inspect the changes, and build the site before publishing. The standalone project is also available; consult its own script for the options supported by that version.

This post is licensed under CC BY-NC-SA 4.0 Deed by the author.