合成图像

composite

在已处理(调整大小,提取等)图像上的合成图像。

要合成的图像必须与处理后的图像相同或更小。如果同时提供topleft选项,则它们优先于gravity

blend 选项可以是其中的一个:clear, source, over, in, out, atop, dest, dest-over, dest-in, dest-out, dest-atop, xor, add, saturate, multiply, screen, overlay, darken, lighten, colour-dodge, color-dodge, colour-burn,color-burn, hard-light, soft-light, difference, exclusion.

有关混合模式的更多信息,请参见 https://libvips.github.io/libvips/API/current/libvips-conversion.html#VipsBlendModehttps://www.cairographics.org/operators/

参数

  • images Array <Object > 要合成的图像的有序列表
    • images[].input (Buffer | String )? 包含图像数据的缓冲区,包含图像文件路径的String或Create对象(请参见下文)
    • images[].blend String 如何将此图像与下面的图像混合。(可选,默认 'over')
    • images[].gravity String 放置叠加层的位置。 (可选,默认 'centre')
    • images[].top Number ? 距上边缘的像素偏移量。
    • images[].left Number ? 距左边缘的像素偏移量。
    • images[].tile Boolean 设置为true的可使用给定的gravity在整个图像上重复覆盖图像。(可选,默认为 false)
    • images[].premultiplied Boolean 设置为true以避免预乘下图。等同于--premultiplied vips选项。(可选,默认为 false)
    • images[].density Number 表示DPI为向量叠加图像数 (可选,默认 72)
    • images[].raw Object ? 描述使用原始像素数据时的覆盖。
    • images[].animated boolean 动画布尔值设置为 true 以读取动画图像的所有帧/页(可选,默认 false)
    • images[].failOnError boolean 请查看构造函数 constructor parameters (可选,默认 true)
    • images[].limitInputPixels (number | boolean ) 请查看构造函数 constructor parameters (可选,默认 268402689)

例子

  1. await sharp(background)
  2. .composite([
  3. { input: layer1, gravity: 'northwest' },
  4. { input: layer2, gravity: 'southeast' },
  5. ])
  6. .toFile('combined.png');
  1. const output = await sharp('input.gif', { animated: true })
  2. .composite([
  3. { input: 'overlay.png', tile: true, blend: 'saturate' }
  4. ])
  5. .toBuffer();
  1. sharp('input.png')
  2. .rotate(180)
  3. .resize(300)
  4. .flatten( { background: '#ff6600' } )
  5. .composite([{ input: 'overlay.png', gravity: 'southeast' }])
  6. .sharpen()
  7. .withMetadata()
  8. .webp( { quality: 90 } )
  9. .toBuffer()
  10. .then(function(outputBuffer) {
  11. // outputBuffer contains upside down, 300px wide, alpha channel flattened
  12. // onto orange background, composited with overlay.png with SE gravity,
  13. // sharpened, with metadata, 90% quality WebP image data. Phew!
  14. });
  • 无效参数时抛出错误 Error

返回 Sharp 对象

Meta

  • since: 0.22.0