
In today’s fast-paced digital ecosystem, image optimization is not just a performance booster—it’s a necessity. Whether you’re optimizing an e-commerce platform or a personal blog, reducing image size without compromising too much on visual quality directly enhances user experience and decreases page load times. One of the most effective formats for this mission is WebP, thanks to its superior compression capabilities and wide browser support. However, when it comes to enforcing strict file size limits—say, compressing every WebP image to under 100KB—it requires more than just clicking “Export” in an image editor. In this article, we’ll dive into how to automate the compression process using scripts, incorporate it into your CI/CD pipeline, and enforce it via robust guardrails.
Why Exactly 100KB?
Setting a file size budget, such as 100KB, is often driven by real-world constraints—mobile bandwidth limitations, performance budgets defined in Lighthouse scores, or simply a UX rule in your product’s design system. Choosing a strict limit like 100KB can help:
- Ensure Quick Load Times: Especially on poor network conditions, smaller images render faster.
- Improve SEO: Google takes page load time into account, and quick-rendering images contribute significantly.
- Standardize Content: Keeps all visual assets in check across multiple contributors and editors.
WebP: The Compression King
WebP is an image format developed by Google that provides superior lossless and lossy compression for images on the web. It supports both transparency (like PNG) and animation (like GIF), making it a versatile choice.
WebP usually yields 25% to 35% smaller file sizes compared to PNG or JPEG equivalents, making it ideal when targeting a strict file size budget like 100KB. However, when multiple people are contributing assets, ensuring that every single image meets that budget requires continuity and automation.

Compressing WebP Images via Scripts
To programmatically compress WebP files under 100KB, you can leverage tools like cwebp
, part of Google’s libwebp suite, or third-party wrappers and libraries in popular languages like Python or Node.js.
Example using cwebp
CLI:
cwebp -q 75 input.png -o output.webp
But what if the output is still over 100KB? Enter the concept of adaptive compression, where scripts iteratively adjust compression quality until the desired limit is met. Here’s a conceptual shell script that does just that:
#!/bin/bash
input=$1
output=$2
target_size=102400
quality=80
while [ $quality -ge 10 ]; do
cwebp -q $quality "$input" -o "$output"
size=$(stat -c %s "$output")
if [ $size -le $target_size ]; then
echo "Success: $output is under 100KB"
break
fi
((quality-=5))
done
if [ $quality -lt 10 ]; then
echo "Failed: Could not compress under 100KB"
exit 1
fi
This script provides a balance between quality and size. Of course, it’s advisable to test different quality thresholds depending on the visual complexity of your assets.
Integrating With CI/CD Pipelines
To enforce consistent image quality and file size control across all assets pushed to your repository, integrate your compression checks into your continuous integration (CI) pipeline. Tools like GitHub Actions, GitLab CI, and CircleCI can easily integrate with such scripts.
Example: GitHub Actions Workflow Snippet
jobs:
image-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install WebP Tools
run: sudo apt-get install webp
- name: Run Compression Script
run: |
find ./images -name '*.webp' -exec ./scripts/compress.sh {} {} \;
In practice, you might want to check newly added/modified images instead of all images every time. This can be done with Git diff commands or using GitHub Actions inputs and event types.
Guardrails: Keeping the Repository Clean
Scripted compression and CI checks are powerful, but for bulletproof workflows, you’ll need to establish guardrails. These should include a few foundational elements:
1. Pre-Commit Hooks
Using tools like pre-commit
or Husky (for JavaScript projects), you can automatically compress images before they’re even committed.
Example using pre-commit
config:
- repo: local
hooks:
- id: compress-webp
name: 'Compress WebP to Under 100KB'
entry: ./scripts/compress.sh
language: script
files: \.(webp)$
2. CI Failures on Oversized Files
Modify your scripts so that they return an exit code of 1 when they fail to compress the image under 100KB. This ensures that pull requests with oversized WebP files cannot be merged until they are optimized properly.
3. GitHub Bot Comments or Warnings
Go a step further by building a bot that leaves a comment on the PR if a submitted image exceeds the threshold, suggesting compression or linking to documentation on image optimization tips.
[h2>4. Documentation and Training
Be sure to educate your team and contributors. A simple markdown guide or wiki page explaining image formats, compression tools, and quality tips goes a long way toward sustainable compliance.
Testing Tradeoffs: Automation vs. Aesthetics
It’s important to note that maximum automation can sometimes introduce degradation in image quality. Purely algorithmic compression can create banding or artifacting in images with gradients, text, or complex patterns.
A good practice is to have fallback conditions. For example:
- If quality drops below 40 and image still exceeds 100KB, skip compression and notify the uploader.
- Whitelist brands or certain image types that must maintain higher quality (e.g., company logos, product samples).
- Incorporate visual diffs using tools like
pixelmatch
orresemble.js
to automate quality thresholds.
This way, you can avoid haunting visual artifacts while maintaining size constraints.

Real-World Scripting Tips
Here’s how you can go from script concept to real-world utility:
- Make it modular: Design your compression script as a module so you can reuse it across projects.
- Cache processed files: Use hashes or file timestamps to skip re-compression during CI builds.
- Use logs: Log the original and compressed sizes for every image. Great for transparency and debugging.
- Provide an override mechanism: Sometimes, rules need to be broken. Allow contributors to bypass compression under critical scenarios with a commit flag or manual approval.
Conclusion: Future-Proofing With Automation
The need to compress WebP images under 100KB is driven by the realities of modern web performance, and achieving this consistently requires more than just good intentions. Through smart scripting, continuous integration, and proactive guardrails, teams can bring structure and discipline to their image management practices.
Remember, automation doesn’t replace human judgment, but it empowers teams to move faster without sacrificing quality. The combination of WebP’s efficiency, intelligent scripts, and robust CI guardrails is your ticket to blazing-fast, visually appealing websites.