跳转到内容

常用操作跨绑定映射

复制本页

同一产品意图在不同语言绑定(binding)或入口中的 API 命名与参数单位并不相同。govips 是本手册的参考实现;sharp、pyvips、CLI 与 Image 的入口用于对照,不代表与 govips 语义逐项一致。 Image 列只填写 image 0.72.0 文档里核对过、或本手册已经执行过的函数,其余标为未覆盖。

比较边界:下表基线对应 govips v2.19.0、sharp 0.35.5、pyvips 3.2.0、vips CLI 8.17.3 与 image 0.72.0(Vix 0.42.0,链接 libvips 8.17.3)。具体参数默认行为需按对应文档与手册实测边界核对。

意图 govips (v2.19.0) sharp (0.35.5) pyvips (3.2.0) vips CLI (8.17.3) Image (0.72.0)
从文件加载 NewImageFromFile / LoadImageFromFile sharp(path, options) Image.new_from_file vips ... file Image.open/2
从压缩内存 Buffer 加载 NewImageFromBuffer / LoadImageFromBuffer sharp(buf, options) Image.new_from_buffer — 未覆盖
从未压缩 Raw 像素加载 NewImageFromMemory(buf, w, h, bands, fmt, interp) sharp(buf, { raw: { width, height, channels } }) Image.new_from_memory(buf, w, h, bands, fmt) vips rawload 未覆盖
与宿主图像/数组互转 NewImageFromGoImage / ToGoImage Uint8Array.buffer + raw Image.new_from_array / np.asarray(img) — 未覆盖
流式读取与写出 NewImageFromReader + SaveToWriter* sharp() (Duplex Stream) SourceCustom + TargetCustom [descriptor=0] / .ext[descriptor=1] 未覆盖
捕获全部动画帧 / 页 importParams.NumPages.Set(-1) sharp(input, { animated: true }) new_from_file(path, n=-1) 'file.gif[n=-1]' 未覆盖
导出到内存缓冲区 ExportJpeg / ExportPng / ExportWebp / ExportAvif 等 .jpeg().toBuffer() 等 write_to_buffer(".webp[Q=82]") — 未覆盖
头部格式与元数据探测 DetermineImageType + Width/Height/Bands/Pages await sharp(input).metadata() Image.new_from_file 属性与 get_typeof vipsheader -a 未覆盖
剥离元数据(保留 ICC) RemoveMetadata("icc-profile-data") 默认剥离(保留用 .keepIccProfile()) write_to_file(..., keep="icc") --keep icc 未覆盖
像素统计与单点采样 Stats() / Average() / GetPoint(x, y) await sharp(input).stats() stats() / avg() / getpoint(x, y) vips stats / vips avg / vips getpoint 未覆盖
意图 govips (v2.19.0) sharp (0.35.5) pyvips (3.2.0) vips CLI (8.17.3) Image (0.72.0)
解码期收缩缩略图 NewThumbnailWithSizeFromFile / LoadThumbnailFromBuffer .resize(w, h, { fit }) Image.thumbnail / Image.thumbnail_buffer vips thumbnail Image.thumbnail/3
已加载图像的内存缩略图 ThumbnailWithSize(w, h, crop, size) .resize(w, h, { fit }) image.thumbnail_image(w, height=h) vips thumbnail_image Image.thumbnail/3
按相对比例系数缩放 Resize(scale, kernel) / ResizeWithVScale(h, v, kernel) .resize(...)(按像素宽高) resize(scale, vscale=...) vips resize Image.resize/3
矩形区域裁剪 ExtractArea(left, top, width, height) .extract({ left, top, width, height }) crop(left, top, width, height) vips crop Image.crop/5
内容感知智能裁剪 SmartCrop(width, height, InterestingEntropy | InterestingAttention) .resize(w, h, { fit: 'cover', position: sharp.strategy.entropy }) smartcrop(w, h, interesting=...) vips smartcrop 未覆盖
自动寻找/裁除纯色边框 FindTrim(threshold, bg) + ExtractArea .trim({ threshold, background }) find_trim(threshold=..., background=...) + crop vips find_trim Image.trim/2
按 EXIF 方向自动扶正 AutoRotate() .autoOrient()(或无参 .rotate()) autorot() vips autorot Image.autorotate/1
固定正交旋转(90°/180°/270°) Rotate(Angle90 | Angle180 | Angle270) .rotate(90 | 180 | 270) rot("d90" | "d180" | "d270") vips rot Image.rotate/3(90° 的整数倍走离散旋转)
任意角度旋转与仿射矩阵 Similarity(scale, angle, bg, ...) .rotate(angle) / .affine(matrix, options) similarity(angle=...) / affine(matrix) vips similarity / vips affine Image.rotate/3;仿射未覆盖
翻转(垂直 / 水平) Flip(DirectionVertical | DirectionHorizontal) .flip()(垂直)/ .flop()(水平) flip("vertical" | "horizontal") vips flip Image.flip/2(:horizontal / :vertical)
画布扩边与边缘延展 Embed(x, y, w, h, extend) / EmbedBackground .extend({ top, bottom, left, right, extendWith }) embed(x, y, w, h, extend=...) vips embed 未覆盖
按方位角对齐扩边 Gravity(direction, w, h) .resize(w, h, { fit: 'contain', position }) gravity(direction, w, h) vips gravity 未覆盖
双图拼接与多图网格 Join / ArrayJoin(images, across) sharp([...], { join: { across, shim } }) join / Image.arrayjoin(images, across=...) vips join / vips arrayjoin 未覆盖
意图 govips (v2.19.0) sharp (0.35.5) pyvips (3.2.0) vips CLI (8.17.3) Image (0.72.0)
图层叠加合成 Composite / CompositeMulti .composite([...]) composite / composite2 vips composite / vips composite2 未覆盖
Pango 文字与 SVG 渲染 Label(...) 或加载 SVG .composite([{ input: { text } }]) 或加载 SVG Image.text(...) 或 svgload vips text / vips svgload 未覆盖
添加/移除/展平 Alpha AddAlpha / ExtractBand(0, 3) / Flatten .ensureAlpha() / .removeAlpha() / .flatten() addalpha() / img[0:3] / flatten() vips addalpha / vips extract_band / vips flatten Image.flatten/2;添加与移除通道未覆盖
Alpha 预乘与反预乘 PremultiplyAlpha() / UnpremultiplyAlpha() composite({ premultiplied }) premultiply() / unpremultiply() vips premultiply / vips unpremultiply 未覆盖
抽取与追加通道 ExtractBand(band, num) / BandJoin / BandJoinConst .extractChannel(ch) / .joinChannel(img) extract_band / bandjoin / bandjoin_const vips extract_band / vips bandjoin 未覆盖
条件遮罩混合 Composite(BlendModeDestIn / BlendModeOver) .composite([{ blend: 'dest-in' }]) mask.ifthenelse(fg, bg, blend=True) vips ifthenelse --blend 未覆盖
色彩空间与 ICC 变换 ToColorSpace / TransformICCProfile .toColourspace() / .withIccProfile() colourspace() / icc_transform() vips colourspace / vips icc_transform 未覆盖
亮度/饱和度/色相调制 Modulate(b, s, h) / ModulateHSV(b, s, h) .modulate({ brightness, saturation, hue }) colourspace("lch") + linear + colourspace("srgb") vips colourspace + vips linear Image.modulate/2
线性变换、3×3 矩阵与反色 Linear / Recomb(matrix) / Invert() .linear(a, b) / .recomb(matrix) / .negate() linear / recomb / invert() vips linear / vips recomb / vips invert 未覆盖
高斯模糊与 USM 锐化 GaussianBlur(sigma) / Sharpen(sigma, x1, m2) .blur(sigma) / .sharpen({ sigma, m1, m2, x1, y2, y3 }) gaussblur(sigma) / sharpen(...) vips gaussblur / vips sharpen Image.blur/2 / Image.sharpen/2
中值滤波、卷积与边缘检测 Rank(w, h, idx) / Sobel()(canny/conv 仅生成内部 vipsGen*) .median(size) / .convolve({ width, height, kernel }) median(size) / rank / conv / sobel / canny vips rank / vips conv / vips sobel / vips canny 未覆盖
直方图均衡与局部对比度(CLAHE) HistogramNormalise() / HistogramFind() .normalise() / .clahe({ width, height, maxSlope }) hist_norm() / hist_equal() / hist_local(...) vips hist_norm / vips hist_local 未覆盖
制作 Deep Zoom 金字塔切片 未封装 dzsave(需用 CLI/sharp/pyvips) .tile({ size, overlap, layout: 'dz' }) dzsave(basename, ...) vips dzsave 未覆盖
  • 单位差异:govips 与 libvips 底层的 Resize 接收的是相对缩放系数(1.0 表示 100%),而 Thumbnail* 和 sharp.resize(w, h) 接收的是目标像素宽高。不要用 sharp.resize(w, h) 的语义去理解 govips 的 Resize。Image.resize/3 同样接收缩放系数;Image.thumbnail/3 接收边长或 "宽x高"。
  • 旋转与翻转入口差异:在 govips 中,90° 倍数的正交旋转使用枚举型的 Rotate,而任意角度旋转通过 Similarity 完成;在 sharp 中,.flip() 表示垂直翻转而 .flop() 表示水平翻转。Image.flip/2 使用 :horizontal 与 :vertical。image 0.72.0 省略 :background 时使用 libvips 自己的填充:带 Alpha 时透明,否则多为黑色。
  • 加载与缩略图路径:Thumbnail* 走 libvips 按目标尺寸收缩解码(shrink-on-load)的路径,与先完整解码整幅大图再调用 Resize 的内存和耗时特征截然不同(详见缩略图与解码期按块收缩)。
  • 动图处理:缩放或旋转动图需先用 NumPages=-1(或 animated: true / n=-1)加载全部帧,详见动画图像与多帧垂直条带模型。