Skip to content

颜色操作

tint

tint(tint) ⇒ Sharp

使用提供的颜色对图片进行着色。Alpha 通道可能存在并且不会被操作改变。

¥Tint the image using the provided colour. An alpha channel may be present and will be unchanged by the operation.

抛出:

¥Throws:

  • Error 参数无效

    ¥Error Invalid parameter

参数类型描述
tintstring | Objectcolor 模块解析。

示例

¥Example

const output = await sharp(input)
.tint({ r: 255, g: 240, b: 16 })
.toBuffer();

greyscale

greyscale([greyscale]) ⇒ Sharp

转换为 8 位灰度;256 种灰度。这是一个线性运算。如果输入图片位于非线性颜色空间(例如 sRGB)中,请使用 gamma()greyscale() 以获得最佳结果。默认情况下,输出图片将是适合网络的 sRGB 并包含三个(相同)颜色通道。这可能会被其他精确操作(如 toColourspace('b-w'))覆盖,这将生成包含一个颜色通道的输出图片。Alpha 通道可能存在,并且不会被操作改变。

¥Convert to 8-bit greyscale; 256 shades of grey. This is a linear operation. If the input image is in a non-linear colour space such as sRGB, use gamma() with greyscale() for the best results. By default the output image will be web-friendly sRGB and contain three (identical) colour channels. This may be overridden by other sharp operations such as toColourspace('b-w'), which will produce an output image containing one colour channel. An alpha channel may be present, and will be unchanged by the operation.

参数类型默认
[greyscale]Booleantrue

示例

¥Example

const output = await sharp(input).greyscale().toBuffer();

grayscale

grayscale([grayscale]) ⇒ Sharp

greyscale 的替代拼写。

¥Alternative spelling of greyscale.

参数类型默认
[grayscale]Booleantrue

pipelineColourspace

pipelineColourspace([colourspace]) ⇒ Sharp

设置管道色彩空间。

¥Set the pipeline colourspace.

输入图片将在管道开始时转换为提供的色彩空间。所有操作都将在转换为输出色彩空间之前使用此色彩空间,如 toColourspace 所定义。

¥The input image will be converted to the provided colourspace at the start of the pipeline. All operations will use this colourspace before converting to the output colourspace, as defined by toColourspace.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

自从:0.29.0

¥Since: 0.29.0

参数类型描述
[colourspace]string管道色彩空间,例如 rgb16scrgblabgrey16

示例

¥Example

// Run pipeline in 16 bits per channel RGB while converting final result to 8 bits per channel sRGB.
await sharp(input)
.pipelineColourspace('rgb16')
.toColourspace('srgb')
.toFile('16bpc-pipeline-to-8bpc-output.png')

pipelineColorspace

pipelineColorspace([colorspace]) ⇒ Sharp

pipelineColourspace 的替代拼写。

¥Alternative spelling of pipelineColourspace.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

参数类型描述
[colorspace]string管道色彩空间。

toColourspace

toColourspace([colourspace]) ⇒ Sharp

设置输出色彩空间。默认情况下,输出图片将为网络友好的 sRGB,附加通道被解释为 Alpha 通道。

¥Set the output colourspace. By default output image will be web-friendly sRGB, with additional channels interpreted as alpha channels.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

参数类型描述
[colourspace]string输出色彩空间,例如 srgbrgbcmyklabb-w

示例

¥Example

// Output 16 bits per pixel RGB
await sharp(input)
.toColourspace('rgb16')
.toFile('16-bpp.png')

toColorspace

toColorspace([colorspace]) ⇒ Sharp

toColourspace 的替代拼写。

¥Alternative spelling of toColourspace.

抛出:

¥Throws:

  • Error 无效参数

    ¥Error Invalid parameters

参数类型描述
[colorspace]string输出色彩空间。