A small, fast image shrinker for AI coding hooks. Drop it into a Claude Code
PreToolUse hook to keep oversized screenshots and tool output images from
blowing your token budget — without losing the pixels.
- Format-preserving: PNG → PNG, JPEG → JPEG, GIF → GIF, WebP → WebP.
- Right-sized for Anthropic: defaults to a 1568px long edge and 5 MB cap (the values Claude's vision pipeline is happy with).
- Single Rust binary: ~7 MB stripped, no Node or Python runtime needed at invocation time.
- Safe
--inplace: writes to a sibling temp file, swaps via atomicrename, and parks the original at<file>.orig.
big.png (3200 × 1800, 761 KB)
│
▼ parvix big.png --inplace
big.png (1568 × 882, 297 KB)
big.png.orig (untouched original)
npm install -g parvix
# or, no install
npx parvix big.png -o small.pngThe npm package resolves a prebuilt platform binary via optionalDependencies
— no postinstall script and no network access at install time.
cargo install parvixparvix [OPTIONS] <INPUT>
Arguments:
<INPUT> Input image path. Use "-" for stdin.
Options:
-o, --output <OUTPUT> Output path. Defaults to stdout when neither
this nor --inplace is set.
--max-edge <MAX_EDGE> Maximum long-edge dimension in pixels
[default: 1568].
--max-bytes <MAX_BYTES> Maximum output size in bytes
[default: 5242880].
--max-decode-dim <PX> Reject inputs whose header declares either
dimension above this before allocating any
pixel buffer [default: 16384].
--max-decode-alloc <N> Cap on bytes the decoder may allocate
[default: 268435456, i.e. 256 MiB].
--inplace Overwrite the input file. The original is
moved to <input>.orig first.
Exit codes:
0— image was within budget (passed through unchanged), or shrunk successfully.- non-zero — non-image input, decode failure, or
--max-bytescannot be reached for a non-JPEG format (PNG/GIF/WebP have no quality knob in v0.1).
- The original file is renamed to
<file>.origbefore the replacement is installed; an existing.origis overwritten. - File mode (Unix permissions) is preserved across the replacement.
- The replacement uses a sibling temp file and atomic
rename, so a crash mid-operation leaves either the original or the new file in place — never a missing one. - Single-writer per file:
parvix --inplacedoes not take a lock. Running two concurrent--inplaceinvocations against the same path is unsupported and may corrupt the.origbackup. Hook scripts should serialize per-path access (the supplied Claude Code hook does so implicitly because Read calls within one session run sequentially).
Wire examples/claude-code-hook.sh into ~/.claude/settings.json:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Read",
"hooks": [
{ "type": "command", "command": "/abs/path/to/claude-code-hook.sh", "timeout": 30 }
]
}
]
}
}The hook only fires for Read calls on .png, .jpg, .jpeg, .gif, and
.webp files. When a shrink is needed it returns
hookSpecificOutput.updatedInput.file_path pointing at the resized copy in
$TMPDIR/parvix-claude-<session_id>/. When the image is already within
budgets it emits nothing and Claude Code reads the original unchanged.
Dependencies for the hook script: bash, jq, and parvix on PATH.
| Alternative | Why parvix instead |
|---|---|
Image-to-text summary hooks (e.g. the justi gist) |
They destroy visual information. UI/layout/OCR work needs pixels. |
Drag-and-drop tools (claude-img-opt) |
macOS-only, manual workflow, no hook integration. |
In-IDE plugins (Claude-Image-Production-Plugin) |
Slash commands, not automatic. |
sharp / libvips scripts |
Require a Node or Python runtime per invocation; awkward for a hook script that fires before every Read. |
v0.1 ships Claude Code support only. See docs/adr/0001-project-scope.md
for what is in and out of scope, and docs/adr/0002-rust-core-npm-shell.md
for the distribution model.
Codex CLI support is on the v0.2 roadmap once updatedInput semantics are
verified in a real Codex run. Cursor's beforeReadFile is permission-only
and cannot rewrite paths, so it needs a different architecture (file
watcher) — also deferred.