removeAlpha 删除管道

删除Alpha通道(如果有)。如果图像没有Alpha通道,则为空。

参数

  • rgb (string|Object)由色彩模块解析以提取色度值。

例子

  1. sharp('rgba.png')
  2. .removeAlpha()
  3. .toFile('rgb.png', function(err, info) {
  4. // rgb.png is a 3 channel image without an alpha channel
  5. });

返回sharp实例对象

ensureAlpha

确保缺少Alpha通道。添加的Alpha通道将完全不透明。如果图像已经具有Alpha通道,则为无操作状态。

参数

  • alpha number alpha 透明度级别(0=完全透明,1=完全不透明)(可选,默认为1

例子

  1. // rgba.png will be a 4 channel image with a fully-opaque alpha channel
  2. await sharp('rgb.jpg')
  3. .ensureAlpha()
  4. .toFile('rgba.png')
  1. // rgba is a 4 channel image with a fully-transparent alpha channel
  2. const rgba = await sharp(rgb)
  3. .ensureAlpha(0)
  4. .toBuffer();

返回sharp实例对象

Meta

  • since: 0.21.2

extractChannel

从多通道图像中提取一个通道。

参数

  • channel (number |string)导出零索引的信道/带编号,或redgreenbluealpha

例子

  1. // green.jpg is a greyscale image containing the green channel of the input
  2. await sharp(input)
  3. .extractChannel('green')
  4. .toFile('green.jpg');
  1. // red1 is the red value of the first pixel, red2 the second pixel etc.
  2. const [red1, red2, ...] = await sharp(input)
  3. .extractChannel(0)
  4. .raw()
  5. .toBuffer();

返回sharp对象实例

joinChannel

将一个或多个通道加入图像。添加通道的含义取决于使用toColourspace()设置的输出色彩空间。默认情况下,输出图像将是Web友好的sRGB,其他通道被解释为alpha通道。频道权重遵循vips惯例:

  • sRGB:0:红色,1:绿色,2:蓝色,3:Alpha。
  • CMYK:0:洋红色,1:青色,2:黄色,3:黑色,4:Alpha。

缓冲区可以是Sharp所支持的任何图像格式:JPEG,PNG,WebP,GIF,SVG,TIFF或原始像素图像数据。对于原始像素输入,options对象应包含一个raw属性,该属性遵循sharp()构造函数中同名属性的格式。

参数

  • images (Array<(string|buffer)> |string|Buffer)一个或多个图像(文件路径,缓冲区)。
  • options Object 图像选项,请参见sharp()构造函数。
  • 无效参数将引发错误

返回sharp实例对象

bandbool

对所有输入图像通道(带)执行按位布尔运算,以生成单个通道输出图像。

参数

  • boolOp string andoreor中的一个执行该位操作,像C逻辑运算&|^

例子

  1. sharp('3-channel-rgb-input.png')
  2. .bandbool(sharp.bool.and)
  3. .toFile('1-channel-output.png', function (err, info) {
  4. // The output will be a single channel image where each pixel `P = R & G & B`.
  5. // If `I(1,1) = [247, 170, 14] = [0b11110111, 0b10101010, 0b00001111]`
  6. // then `O(1,1) = 0b11110111 & 0b10101010 & 0b00001111 = 0b00000010 = 2`.
  7. });