Skip to content

通道操作

removeAlpha

removeAlpha() ⇒ Sharp

删除 alpha 通道(如果有)。如果图片没有 Alpha 通道,则此操作无效。

¥Remove alpha channels, if any. This is a no-op if the image does not have an alpha channel.

另见 flatten

¥See also flatten.

示例

¥Example

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

ensureAlpha

ensureAlpha([alpha]) ⇒ Sharp

确保输出图片具有 Alpha 透明通道。如果缺失,添加的 Alpha 通道将具有指定的透明度级别,默认为完全不透明 (1)。如果图片已有 Alpha 通道,则此操作无效。

¥Ensure the output image has an alpha transparency channel. If missing, the added alpha channel will have the specified transparency level, defaulting to fully-opaque (1). This is a no-op if the image already has an alpha channel.

抛出:

¥Throws:

  • Error 无效的 Alpha 透明度级别

    ¥Error Invalid alpha transparency level

自从:0.21.2

¥Since: 0.21.2

参数类型默认描述
[alpha]number1Alpha 透明度级别(0=完全透明,1=完全不透明)

示例

¥Example

// rgba.png will be a 4 channel image with a fully-opaque alpha channel
await sharp('rgb.jpg')
.ensureAlpha()
.toFile('rgba.png')

示例

¥Example

// rgba is a 4 channel image with a fully-transparent alpha channel
const rgba = await sharp(rgb)
.ensureAlpha(0)
.toBuffer();

extractChannel

extractChannel(channel) ⇒ Sharp

从多通道图片中提取单个通道。

¥Extract a single channel from a multi-channel image.

抛出:

¥Throws:

  • Error 无效通道

    ¥Error Invalid channel

参数类型描述
channelnumber | string要提取的零索引通道/频段号,或 redgreenbluealpha

示例

¥Example

// green.jpg is a greyscale image containing the green channel of the input
await sharp(input)
.extractChannel('green')
.toFile('green.jpg');

示例

¥Example

// red1 is the red value of the first pixel, red2 the second pixel etc.
const [red1, red2, ...] = await sharp(input)
.extractChannel(0)
.raw()
.toBuffer();

joinChannel

joinChannel(images, options) ⇒ Sharp

将一个或多个通道加入到图片中。添加通道的含义取决于使用 toColourspace() 设置的输出色彩空间。默认情况下,输出图片将为 Web 友好的 sRGB,其中附加通道被解释为 Alpha 通道。通道排序遵循 VIP 惯例:

¥Join one or more channels to the image. The meaning of the added channels depends on the output colourspace, set with toColourspace(). By default the output image will be web-friendly sRGB, with additional channels interpreted as alpha channels. Channel ordering follows vips convention:

  • sRGB:0:红色,1:绿色,2:蓝色,3:Α。

    ¥sRGB: 0: Red, 1: Green, 2: Blue, 3: Alpha.

  • CMYK:0:洋红色,1:青色,2:黄色,3:黑色,4:Α。

    ¥CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.

缓冲区可以是 Sharp 支持的任何图片格式。对于原始像素输入,options 对象应包含 raw 属性,该属性遵循 sharp() 构造函数中同名属性的格式。

¥Buffers may be any of the image formats supported by sharp. For raw pixel input, the options object should contain a raw attribute, which follows the format of the attribute of the same name in the sharp() constructor.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

参数类型描述
imagesArray.<(string|Buffer)> | string | Buffer一张或多张图片(文件路径、缓冲区)。
optionsObject图片选项,请参见 sharp() 构造函数。

bandbool

bandbool(boolOp) ⇒ Sharp

对所有输入图片通道(波段)执行按位布尔运算以生成单通道输出图片。

¥Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

参数类型描述
boolOpstringandoreor 之一执行按位运算,如 C 逻辑运算符 &、`and^` respectively.

示例

¥Example

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