Screenshot Compression

作者 zc277584121959680e17acf無授權條款收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Compress PNG and JPEG screenshots in place using pngquant and jpegoptim, keeping the original format for maximum compatibility.

包含腳本Design & Creative
AI 產生的概覽

使用 pngquant 和 jpegoptim 就地壓縮 PNG 與 JPEG 螢幕截圖,並保留原始格式。

功能
此技能在保留原始 PNG 或 JPEG 格式的前提下,就地壓縮螢幕截圖影像。它隨附一支 Python 指令碼,會檢查 pngquant 與 jpegoptim 是否存在,依副檔名自動辨識格式,逐一壓縮檔案,並輸出單一檔案與整體的壓縮摘要。它支援遞迴掃描目錄,以及可調整的 PNG 與 JPEG 品質參數。
適用情境
當螢幕截圖檔案過大,且需要在不變更格式的情況下縮小體積時使用,例如為 README、部落格文章或文件準備圖片、在提交至程式碼儲存庫前縮小圖片,或批次壓縮某個目錄中的螢幕截圖。
執行需求
需要 Python 以及系統工具 pngquant 與 jpegoptim,後兩者必須另外安裝;指令碼會檢查它們是否存在,若缺少則印出安裝說明,不會自行安裝。它隨附一支可執行的 Python 指令碼,僅處理 .png、.jpg 與 .jpeg 檔案。

Skill: Screenshot Compression

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.


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

Why Keep Original Format (Not 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.


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

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
# macOSbrew install pngquant jpegoptim
# Ubuntu / Debiansudo apt install pngquant jpegoptim
# CentOS / RHELsudo yum install pngquant jpegoptim

Script Options

FlagDefaultDescription
paths (positional)requiredImage files or directories to compress
-r, --recursiveoffRecursively process directories
--png-quality80-95pngquant quality range (min-max, 0-100)
--jpeg-quality85jpegoptim max quality (0-100)

Examples

bash
# Compress a single filepython .../compress_screenshots.py screenshot.png
# Compress all images in a directorypython .../compress_screenshots.py ./images/
# Recursive directory scanpython .../compress_screenshots.py ./docs/ --recursive
# High quality for code screenshotspython .../compress_screenshots.py *.png --png-quality 90-100
# Aggressive compression for thumbnailspython .../compress_screenshots.py *.jpg --jpeg-quality 70

Quality Tuning Guide

Scenario--png-quality--jpeg-quality
General screenshots (docs, web pages)80-9585
Code screenshots (need sharp text)90-10090
Thumbnails / previews (size priority)60-8070

How It Works

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%

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%

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.

來源與署名

來源:zc277584121/marketing-skills位於skills/screenshot-compression提交959680e

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架