Skip to content

Repository files navigation

sharp-batch-cli

图片批量处理 CLI / batch processing images CLI

基于 sharp 的 Node.js 图片批量处理工具。

安装

pnpm install -g sharp-batch-cli

在本地仓库开发时:

pnpm install
pnpm build
pnpm link --global

用法

sharp-batch "./*.webp" --format jpeg --quality 80 --output overwrite

批量把当前目录的 WebP 图片转成 JPEG,并替换原文件。

参数

参数 说明 默认
<patterns...> 图片 glob,例如 ./*.webpimages/**/*.png 必填
-f, --format <format> 输出格式:jpegpngwebpaviftiff 保留原格式
-q, --quality <number> 输出质量,范围 1-100 sharp 默认值
-o, --output <target> overwrite 或输出目录 overwrite
-w, --width <number> 目标宽度 不缩放
-h, --height <number> 目标高度 不缩放
--fit <fit> 缩放模式:covercontainfillinsideoutside cover
--megapixels <number> 缩放到目标百万像素,小图不放大 不启用
--max-edge <number> 限制最长边,小图不放大 不启用
--prefix <text> 输出文件名前缀
--suffix <text> 输出文件名后缀
--recursive 把普通 glob 扩展到子目录 关闭
-c, --concurrency <number> 并发处理数 4
--dry-run 只列出匹配文件,不写入 关闭
--verbose 输出每个文件的处理结果 关闭
-V, --version 显示版本
-h, --help 显示帮助

--output overwrite 会以源文件同目录、同 basename 的方式写结果;格式变化时扩展名会变为目标格式,并删除旧文件。

示例

# 输出到 out 目录并缩放到 1280 宽
sharp-batch "photos/**/*.webp" --format jpeg --quality 85 --output out --width 1280

# 批量加文件名后缀,保留原格式
sharp-batch "*.png" --suffix "-v2" --output out

# 最长边超过 1024 时等比缩小
sharp-batch "photos/**/*.webp" --max-edge 1024 --output out

# 限制到约 1 百万像素(接近 1024x1024)
sharp-batch "photos/**/*.webp" --megapixels 1 --output out

# 先查看会匹配哪些文件
sharp-batch "*.webp" --format jpeg --dry-run

About

图片批量处理 cli 工具 / batch processing images cli

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages