sharp/docs/api-channel.md
2018-06-20 08:14:03 +01:00

111 lines
3.2 KiB
Markdown

<!-- Generated by documentation.js. Update this documentation by updating the source code. -->
### Table of Contents
- [extractChannel][1]
- [Parameters][2]
- [Examples][3]
- [joinChannel][4]
- [Parameters][5]
- [bandbool][6]
- [Parameters][7]
- [Examples][8]
## extractChannel
Extract a single channel from a multi-channel image.
### Parameters
- `channel` **([Number][9] \| [String][10])** zero-indexed band number to extract, or `red`, `green` or `blue` as alternative to `0`, `1` or `2` respectively.
### Examples
```javascript
sharp(input)
.extractChannel('green')
.toFile('input_green.jpg', function(err, info) {
// info.channels === 1
// input_green.jpg contains the green channel of the input image
});
```
- Throws **[Error][11]** Invalid channel
Returns **Sharp**
## joinChannel
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: Red, 1: Green, 2: Blue, 3: Alpha.
- CMYK: 0: Magenta, 1: Cyan, 2: Yellow, 3: Black, 4: Alpha.
Buffers may be any of the image formats supported by sharp: JPEG, PNG, WebP, GIF, SVG, TIFF or raw pixel image data.
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.
### Parameters
- `images` **([Array][12]&lt;([String][10] \| [Buffer][13])> | [String][10] \| [Buffer][13])** one or more images (file paths, Buffers).
- `options` **[Object][14]** image options, see `sharp()` constructor.
- Throws **[Error][11]** Invalid parameters
Returns **Sharp**
## bandbool
Perform a bitwise boolean operation on all input image channels (bands) to produce a single channel output image.
### Parameters
- `boolOp` **[String][10]** one of `and`, `or` or `eor` to perform that bitwise operation, like the C logic operators `&`, `|` and `^` respectively.
### Examples
```javascript
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`.
});
```
- Throws **[Error][11]** Invalid parameters
Returns **Sharp**
[1]: #extractchannel
[2]: #parameters
[3]: #examples
[4]: #joinchannel
[5]: #parameters-1
[6]: #bandbool
[7]: #parameters-2
[8]: #examples-1
[9]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Number
[10]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String
[11]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Error
[12]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array
[13]: https://nodejs.org/api/buffer.html
[14]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object