screenshot-compression

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Skill: Screenshot Compression

Skill: 截图压缩

Compress screenshot images (PNG/JPEG) in place while keeping the original format. Uses
pngquant
for PNG and
jpegoptim
for JPEG — both are highly effective for screenshot content (UI elements, text, flat colors).
Prerequisites:
pngquant
and
jpegoptim
must be installed on the system. The script will not install them automatically — it checks for their presence and prints install instructions if missing.

就地压缩PNG/JPEG格式的截图,同时保留原始格式。PNG格式使用
pngquant
处理,JPEG格式使用
jpegoptim
处理——两者对于截图内容(UI元素、文字、平涂色彩)的压缩效果都非常出色。
前提条件:系统必须已安装
pngquant
jpegoptim
。本脚本不会自动安装这两个工具——它会检查工具是否存在,若缺失则打印安装指引。

When to Use

适用场景

The user has screenshot files that are too large and wants to reduce file size without changing format. Common scenarios:
  • Preparing images for GitHub READMEs, blog posts, or documentation
  • Reducing image sizes before committing to a repository
  • Batch compressing a directory of screenshots

用户有体积过大的截图文件,希望在不改变格式的前提下缩小文件体积。常见场景:
  • 为GitHub README、博客文章或文档准备图片
  • 提交代码仓库前缩小图片体积
  • 批量压缩某个目录下的截图

Why Keep Original Format (Not WebP)

为什么保留原始格式(不使用WebP)

WebP has better compression, but poor compatibility in some contexts:
ContextWebP Support
Browsers (Chrome/Firefox/Safari/Edge)Yes
GitHub Issues/PRsYes
WeChat editorNo
Word / PowerPointNo
Some forums/blog backendsVaries
Keeping PNG/JPEG ensures the compressed images work everywhere.

WebP的压缩率更高,但在部分场景下兼容性较差:
场景WebP支持情况
浏览器(Chrome/Firefox/Safari/Edge)
GitHub Issues/PRs
微信编辑器
Word / PowerPoint
部分论坛/博客后台视情况而定
保留PNG/JPEG格式可以确保压缩后的图片在所有场景下都能正常使用。

Default Workflow

默认使用流程

bash
python /path/to/skills/screenshot-compression/scripts/compress_screenshots.py <files-or-directories>
The script will:
  1. Check that
    pngquant
    and
    jpegoptim
    are installed — if not, print install instructions and exit
  2. Auto-detect file format by extension
  3. Compress each file in place (overwrites the original)
  4. Print per-file and total compression summary

bash
python /path/to/skills/screenshot-compression/scripts/compress_screenshots.py <files-or-directories>
脚本会执行以下操作:
  1. 检查
    pngquant
    jpegoptim
    是否已安装——若未安装则打印安装指引后退出
  2. 通过扩展名自动检测文件格式
  3. 就地压缩每个文件(覆盖原文件)
  4. 打印单文件及总压缩情况汇总

Dependency Check

依赖检查

The script requires two system tools. If either is missing, it exits with install instructions instead of proceeding. Do not install them on behalf of the user — just relay the error message so the user can install them.
Install commands:
bash
undefined
脚本需要两个系统工具。如果任意一个缺失,脚本会打印安装指引后退出,不会继续执行。不要替用户安装这些工具——只需转发错误信息,让用户自行安装即可。
安装命令:
bash
undefined

macOS

macOS

brew install pngquant jpegoptim
brew install pngquant jpegoptim

Ubuntu / Debian

Ubuntu / Debian

sudo apt install pngquant jpegoptim
sudo apt install pngquant jpegoptim

CentOS / RHEL

CentOS / RHEL

sudo yum install pngquant jpegoptim

---
sudo yum install pngquant jpegoptim

---

Script Options

脚本选项

FlagDefaultDescription
paths
(positional)
requiredImage files or directories to compress
-r
,
--recursive
offRecursively process directories
--png-quality
80-95
pngquant quality range (min-max, 0-100)
--jpeg-quality
85
jpegoptim max quality (0-100)

参数默认值描述
paths
(位置参数)
必填要压缩的图片文件或目录
-r
,
--recursive
关闭递归处理目录
--png-quality
80-95
pngquant质量范围(最小值-最大值,0-100)
--jpeg-quality
85
jpegoptim最高质量(0-100)

Examples

使用示例

bash
undefined
bash
undefined

Compress a single file

Compress a single file

python .../compress_screenshots.py screenshot.png
python .../compress_screenshots.py screenshot.png

Compress all images in a directory

Compress all images in a directory

python .../compress_screenshots.py ./images/
python .../compress_screenshots.py ./images/

Recursive directory scan

Recursive directory scan

python .../compress_screenshots.py ./docs/ --recursive
python .../compress_screenshots.py ./docs/ --recursive

High quality for code screenshots

High quality for code screenshots

python .../compress_screenshots.py *.png --png-quality 90-100
python .../compress_screenshots.py *.png --png-quality 90-100

Aggressive compression for thumbnails

Aggressive compression for thumbnails

python .../compress_screenshots.py *.jpg --jpeg-quality 70

---
python .../compress_screenshots.py *.jpg --jpeg-quality 70

---

Quality Tuning Guide

质量调整指南

Scenario
--png-quality
--jpeg-quality
General screenshots (docs, web pages)
80-95
85
Code screenshots (need sharp text)
90-100
90
Thumbnails / previews (size priority)
60-80
70

场景
--png-quality
--jpeg-quality
通用截图(文档、网页)
80-95
85
代码截图(需要清晰文字)
90-100
90
缩略图/预览图(优先控制体积)
60-80
70

How It Works

工作原理

PNG (pngquant)

PNG (pngquant)

  • Quantizes 24-bit true color (16M colors) down to an 8-bit palette (256 colors)
  • Uses Floyd-Steinberg dithering for smooth gradients
  • Screenshots are ideal candidates — UI colors are typically well under 256 unique values
  • Typical reduction: 60-80%
  • 将24位真彩色(1600万色)量化为8位调色板(256色)
  • 使用Floyd-Steinberg抖动算法实现平滑渐变
  • 截图是理想的处理对象——UI颜色的唯一值通常远低于256种
  • 典型压缩率:60-80%

JPEG (jpegoptim)

JPEG (jpegoptim)

  • Re-encodes at the specified quality level
  • Strips metadata (EXIF, ICC profiles, thumbnails) via
    --strip-all
  • Optimizes Huffman tables
  • Typical reduction: 20-50%

  • 按指定质量等级重新编码
  • 通过
    --strip-all
    参数移除元数据(EXIF、ICC配置文件、缩略图)
  • 优化哈夫曼表
  • 典型压缩率:20-50%

Important Notes

注意事项

  • Files are compressed in place — the original is overwritten. Back up files first if needed.
  • Only
    .png
    ,
    .jpg
    , and
    .jpeg
    files are processed. Other formats are silently skipped.
  • The script never installs dependencies. If tools are missing, it prints install instructions and exits.
  • 文件为就地压缩——会覆盖原文件,如有需要请先备份文件。
  • 仅处理
    .png
    .jpg
    .jpeg
    格式的文件,其他格式会被静默跳过。
  • 脚本永远不会安装依赖,如果工具缺失,会打印安装指引后退出。