removeAlpha 删除管道
删除Alpha通道(如果有)。如果图像没有Alpha通道,则为空。
参数
rgb(string|Object)由色彩模块解析以提取色度值。
例子
sharp('rgba.png').removeAlpha().toFile('rgb.png', function(err, info) {// rgb.png is a 3 channel image without an alpha channel});
返回sharp实例对象
ensureAlpha
确保缺少Alpha通道。添加的Alpha通道将完全不透明。如果图像已经具有Alpha通道,则为无操作状态。
参数
alphanumber alpha 透明度级别(0=完全透明,1=完全不透明)(可选,默认为1)
例子
// rgba.png will be a 4 channel image with a fully-opaque alpha channelawait sharp('rgb.jpg').ensureAlpha().toFile('rgba.png')
// rgba is a 4 channel image with a fully-transparent alpha channelconst rgba = await sharp(rgb).ensureAlpha(0).toBuffer();
返回sharp实例对象
Meta
- since: 0.21.2
extractChannel
从多通道图像中提取一个通道。
参数
channel(number |string)导出零索引的信道/带编号,或red,green,blue或alpha。
例子
// green.jpg is a greyscale image containing the green channel of the inputawait sharp(input).extractChannel('green').toFile('green.jpg');
// red1 is the red value of the first pixel, red2 the second pixel etc.const [red1, red2, ...] = await sharp(input).extractChannel(0).raw().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)一个或多个图像(文件路径,缓冲区)。optionsObject 图像选项,请参见sharp()构造函数。- 无效参数将引发错误
返回sharp实例对象
bandbool
对所有输入图像通道(带)执行按位布尔运算,以生成单个通道输出图像。
参数
boolOpstringand,or或eor中的一个执行该位操作,像C逻辑运算&,|和^。
例子
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`.});
- 无效参数将引发错误
