screenshot-compression
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseSkill: Screenshot Compression
Skill: 截图压缩
Compress screenshot images (PNG/JPEG) in place while keeping the original format. Uses for PNG and for JPEG — both are highly effective for screenshot content (UI elements, text, flat colors).
pngquantjpegoptimPrerequisites:andpngquantmust be installed on the system. The script will not install them automatically — it checks for their presence and prints install instructions if missing.jpegoptim
就地压缩PNG/JPEG格式的截图,同时保留原始格式。PNG格式使用处理,JPEG格式使用处理——两者对于截图内容(UI元素、文字、平涂色彩)的压缩效果都非常出色。
pngquantjpegoptim前提条件:系统必须已安装和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:
| Context | WebP Support |
|---|---|
| Browsers (Chrome/Firefox/Safari/Edge) | Yes |
| GitHub Issues/PRs | Yes |
| WeChat editor | No |
| Word / PowerPoint | No |
| Some forums/blog backends | Varies |
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:
- Check that and
pngquantare installed — if not, print install instructions and exitjpegoptim - Auto-detect file format by extension
- Compress each file in place (overwrites the original)
- Print per-file and total compression summary
bash
python /path/to/skills/screenshot-compression/scripts/compress_screenshots.py <files-or-directories>脚本会执行以下操作:
- 检查和
pngquant是否已安装——若未安装则打印安装指引后退出jpegoptim - 通过扩展名自动检测文件格式
- 就地压缩每个文件(覆盖原文件)
- 打印单文件及总压缩情况汇总
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
undefinedmacOS
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
脚本选项
| Flag | Default | Description |
|---|---|---|
| required | Image files or directories to compress |
| off | Recursively process directories |
| | pngquant quality range (min-max, 0-100) |
| | jpegoptim max quality (0-100) |
| 参数 | 默认值 | 描述 |
|---|---|---|
| 必填 | 要压缩的图片文件或目录 |
| 关闭 | 递归处理目录 |
| | pngquant质量范围(最小值-最大值,0-100) |
| | jpegoptim最高质量(0-100) |
Examples
使用示例
bash
undefinedbash
undefinedCompress 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 | | |
|---|---|---|
| General screenshots (docs, web pages) | | |
| Code screenshots (need sharp text) | | |
| Thumbnails / previews (size priority) | | |
| 场景 | | |
|---|---|---|
| 通用截图(文档、网页) | | |
| 代码截图(需要清晰文字) | | |
| 缩略图/预览图(优先控制体积) | | |
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%
- 按指定质量等级重新编码
- 通过参数移除元数据(EXIF、ICC配置文件、缩略图)
--strip-all - 优化哈夫曼表
- 典型压缩率:20-50%
Important Notes
注意事项
- Files are compressed in place — the original is overwritten. Back up files first if needed.
- Only ,
.png, and.jpgfiles are processed. Other formats are silently skipped..jpeg - The script never installs dependencies. If tools are missing, it prints install instructions and exits.
- 文件为就地压缩——会覆盖原文件,如有需要请先备份文件。
- 仅处理、
.png和.jpg格式的文件,其他格式会被静默跳过。.jpeg - 脚本永远不会安装依赖,如果工具缺失,会打印安装指引后退出。