removeAlpha
removeAlpha() ⇒ Sharp
删除 alpha 通道(如果有)。如果图像没有 alpha 通道,则此操作为无操作。
另请参阅 flatten。
示例
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 通道,则此操作为无操作。
- 错误 alpha 透明度级别无效
自: 0.21.2
| 参数 | 类型 | 默认 | 描述 |
|---|---|---|---|
| alpha 透明度级别 |
示例
// rgba.png will be a 4 channel image with a fully-opaque alpha channel
await sharp('rgb.jpg')
.ensureAlpha()
.toFile('rgba.png')示例
// rgba is a 4 channel image with a fully-transparent alpha channel
const rgba = await sharp(rgb)
.ensureAlpha(0)
.toBuffer();extractChannel
extractChannel(channel) ⇒ Sharp
从多通道图像中提取单个通道。
- 错误 无效通道
| 参数 | 类型 | 描述 |
|---|---|---|
| 要提取的零索引通道/波段编号,或红色、绿色、蓝色或 alpha。 |
示例
// green.jpg is a greyscale image containing the green channel of the input
await 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();joinChannel
joinChannel(images, options) ⇒ Sharp
将一个或多个通道加入图像。添加的通道的含义取决于使用 toColourspace() 设置的输出色彩空间。默认情况下,输出图像将是适合 Web 的 sRGB,其他通道将解释为 alpha 通道。通道排序遵循 vips 约定:
- sRGB:0:红色,1:绿色,2:蓝色,3:Alpha。
- CMYK:0:洋红色,1:青色,2:黄色,3:黑色,4:Alpha。
缓冲区可以是 sharp 支持的任何图像格式。对于原始像素输入,选项对象应包含 raw 属性,该属性遵循 sharp() 构造函数中同名属性的格式。
- 错误 参数无效
| 参数 | 类型 | 说明 |
|---|---|---|
| 一个或多个图像(文件路径、缓冲区)。 | ||
| 图像选项,请参阅 sharp() 构造函数。 |
bandbool
bandbool(boolOp) ⇒ Sharp
对所有输入图像通道(波段)执行按位布尔运算以生成单通道输出图像。
- 错误 参数无效
| 参数 | 类型 | 说明 |
|---|---|---|
| and、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`.
});