File Compression
Compress files with Python-first workflows and Node.js fallback workflows.
Supported File Types
- - PDF: INLINECODE0
- Image:
.jpg, .jpeg, .png, INLINECODE4
What This Skill Can Do
- - Compress PDF with preset quality levels.
- Compress image with quality/resize/format controls.
- Switch backend automatically when dependencies are missing.
- Detect bad compression results and retry with better strategy.
Installation Spec (Before Running)
Required binaries:
- -
python3 (recommended >= 3.8) - INLINECODE7
- INLINECODE8 (Ghostscript, required for PDF Ghostscript paths)
Python install spec:
CODEBLOCK0
Node install spec:
CODEBLOCK1
Ghostscript install examples:
- - macOS: INLINECODE9
- Ubuntu/Debian: INLINECODE10
Safety note:
- - Explain to the user before each install command that third-party packages are being installed.
- If installation fails, report the failing command and switch to available fallback backend.
CLI Options Cheat Sheet
PDF (scripts/compress_pdf.py):
- - INLINECODE12
- INLINECODE13
- INLINECODE14
- INLINECODE15
- INLINECODE16
PDF Node (scripts/compress_pdf_node.mjs):
Image (scripts/compress_image.py):
- - INLINECODE20
- INLINECODE21
- INLINECODE22
- INLINECODE23
- INLINECODE24
- INLINECODE25
Image Node (scripts/compress_image_node.mjs):
- - INLINECODE27
- INLINECODE28
- INLINECODE29
- INLINECODE30
Example Set (Python + Node)
PDF default:
CODEBLOCK2
PDF aggressive:
CODEBLOCK3
PDF with pikepdf:
CODEBLOCK4
PDF via Node:
CODEBLOCK5
Image default:
CODEBLOCK6
Image convert + resize:
CODEBLOCK7
Image force Node backend:
CODEBLOCK8
Image direct Node:
CODEBLOCK9
Environment and Fallback
Check and install in this order:
- 1. Python:
python3 --version (fallback: python --version) - Node: INLINECODE33
- Ghostscript:
gs --version (required for PDF Ghostscript paths) - Python deps when needed:
-
pip install pikepdf
-
pip install pillow
- 5. Node deps when needed:
- INLINECODE37
Fallback policy:
- - PDF:
ghostscript -> pikepdf -> INLINECODE40 - Image:
pillow -> INLINECODE42
If python3.8+ is unavailable, try python3.11/3.10/3.9/3.8; if still blocked, use Node flow when possible.
Execution Transparency
Always communicate each step:
- 1. Tell user what you are checking or running.
- Show the exact command before execution.
- For slow steps (
pip install, npm install, large Ghostscript jobs), say you are waiting. - After each step, report result and next action.
Bad Result Recovery
When output_size >= input_size, do not stop:
- 1. Report exact from/to numbers and compression ratio.
- Explain likely cause:
- PDF: already optimized, scanned-image content, metadata overhead, unsuitable preset.
- Image: unsuitable format conversion, quality too high, small-file overhead.
- 3. Retry with alternate strategy:
- PDF:
ebook -> screen, then switch backend.
- Image: lower quality, switch backend, convert to
webp, optionally resize.
- 4. Return the best attempt and state which command produced it.
Agent Response Contract
After every compression task, always return:
- 1. Output absolute path.
- INLINECODE50 .
- INLINECODE51 .
- Backend used.
文件压缩
使用Python优先工作流和Node.js备用工作流压缩文件。
支持的文件类型
- - PDF:.pdf
- 图片:.jpg、.jpeg、.png、.webp
本技能可执行的操作
- - 使用预设质量级别压缩PDF。
- 使用质量/尺寸/格式控制压缩图片。
- 在缺少依赖项时自动切换后端。
- 检测不良压缩结果并使用更优策略重试。
安装规范(运行前)
所需二进制文件:
- - python3(建议 >= 3.8)
- node
- gs(Ghostscript,PDF Ghostscript路径必需)
Python安装规范:
bash
python3 -m pip install -r {baseDir}/requirements.txt
Node安装规范:
bash
cd {baseDir}
npm install
Ghostscript安装示例:
- - macOS:brew install ghostscript
- Ubuntu/Debian:sudo apt-get update && sudo apt-get install -y ghostscript
安全提示:
- - 在每次安装命令前向用户说明正在安装第三方包。
- 如果安装失败,报告失败的命令并切换到可用的备用后端。
CLI选项速查表
PDF(scripts/compress_pdf.py):
- - --preset screen|ebook|printer|prepress
- --strategy auto|ghostscript|pikepdf
- --remove-metadata
- --no-linearize
- --overwrite
PDF Node(scripts/compresspdfnode.mjs):
- - --preset screen|ebook|printer|prepress
图片(scripts/compress_image.py):
- - --quality <1-100>
- --format keep|jpeg|png|webp
- --max-width
- --max-height
- --strategy auto|pillow|node
- --overwrite
图片 Node(scripts/compressimagenode.mjs):
- - --quality <1-100>
- --format keep|jpeg|png|webp
- --max-width
- --max-height
示例集(Python + Node)
PDF默认:
bash
python {baseDir}/scripts/compress_pdf.py in.pdf out.pdf
PDF激进压缩:
bash
python {baseDir}/scripts/compress_pdf.py in.pdf out.pdf --preset screen --strategy ghostscript
使用pikepdf压缩PDF:
bash
python {baseDir}/scripts/compress_pdf.py in.pdf out.pdf --strategy pikepdf --remove-metadata
通过Node压缩PDF:
bash
node {baseDir}/scripts/compresspdfnode.mjs in.pdf out.pdf --preset ebook
图片默认:
bash
python {baseDir}/scripts/compress_image.py in.jpg out.jpg --quality 75
图片转换+调整尺寸:
bash
python {baseDir}/scripts/compress_image.py in.png out.webp --format webp --quality 72 --max-width 1920
强制使用Node后端处理图片:
bash
python {baseDir}/scripts/compress_image.py in.jpg out.jpg --strategy node --quality 70
直接使用Node处理图片:
bash
node {baseDir}/scripts/compressimagenode.mjs in.jpg out.jpg --quality 70 --max-width 1600
环境与备用机制
按以下顺序检查并安装:
- 1. Python:python3 --version(备用:python --version)
- Node:node --version
- Ghostscript:gs --version(PDF Ghostscript路径必需)
- 需要时安装Python依赖:
- pip install pikepdf
- pip install pillow
- 5. 需要时安装Node依赖:
- npm install
备用策略:
- - PDF:ghostscript -> pikepdf -> node-ghostscript
- 图片:pillow -> node-sharp
如果python3.8+不可用,尝试python3.11/3.10/3.9/3.8;如果仍被阻止,尽可能使用Node流程。
执行透明度
始终沟通每一步:
- 1. 告知用户正在检查或运行的内容。
- 在执行前显示确切的命令。
- 对于慢速步骤(pip install、npm install、大型Ghostscript任务),说明正在等待。
- 每一步后,报告结果和下一步操作。
不良结果恢复
当outputsize >= inputsize时,不要停止:
- 1. 报告精确的压缩前后数值和压缩比。
- 解释可能的原因:
- PDF:已优化、扫描图像内容、元数据开销、预设不合适。
- 图片:格式转换不合适、质量过高、小文件开销。
- 3. 使用替代策略重试:
- PDF:ebook -> screen,然后切换后端。
- 图片:降低质量、切换后端、转换为webp、可选调整尺寸。
- 4. 返回最佳尝试结果并说明由哪个命令生成。
代理响应契约
每次压缩任务后,始终返回:
- 1. 输出绝对路径。
- 从 <压缩前大小> 到 <压缩后大小>。
- 节省 <差值大小>(<比例>%)。
- 使用的后端。