Skip to content

颜色操作

tint(tint) ⇒ Sharp

使用提供的颜色给图片上色。可能存在 Alpha 通道,此操作不会改变它。

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

投掷:

  • 错误 无效参数
参数 类型 描述
tint 字符串 |对象 由 [color](https://www.npmjs.org/package/color) 模块解析。

示例

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

greyscale([greyscale]) ⇒ Sharp

转换为 8 位灰度图;256 种灰阶。 这是一个线性操作。如果输入图片处于非线性色彩空间,例如 sRGB,请使用 gamma() 和 greyscale() 以获得最佳效果。 默认情况下,输出图片将是适合网页的 sRGB 并包含三个(相同的)色彩通道。 其他 sharp 操作如 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] 布尔 true

示例

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

grayscale([grayscale]) ⇒ Sharp

greyscale 的另一种拼写方式。

🌐 Alternative spelling of greyscale.

参数 类型 默认值
[grayscale] 布尔 true

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.

投掷:

  • 错误 参数无效

自:0.29.0

参数 类型 描述
[colourspace] 字符串 管道颜色空间,例如 rgb16、scrgb、lab、grey16 …

示例

// 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([colorspace]) ⇒ Sharp

pipelineColourspace 的另一种拼写方式。

🌐 Alternative spelling of pipelineColourspace.

投掷:

  • 错误 参数无效
参数 类型 描述
[colorspace] 字符串 流水线颜色空间。

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.

投掷:

  • 错误 参数无效
参数 类型 描述
[colourspace] string 输出色彩空间,例如 srgb、rgb、cmyk、lab、b-w …

示例

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

toColorspace([colorspace]) ⇒ Sharp

toColourspace 的另一种拼写方式。

🌐 Alternative spelling of toColourspace.

投掷:

  • 错误 参数无效
参数 类型 描述
[colorspace] 字符串 输出色彩空间。