rotate 旋转

根据EXIFOrientation标记,以明确的角度或自动定向旋转输出图像。

如果提供一个角度,则将其转换为有效的正角度旋转。例如,-450将产生270度旋转。

当旋转角度不是90的倍数时,可以使用该background选项提供背景颜色。

如果未提供角度,则根据EXIF数据确定角度。支持镜像,并且可以推断出翻转操作的使用。

使用rotate暗指删除EXIFOrientation标签(如果有)。

当旋转区域和提取区域时,方法顺序都很重要,例如,rotate(x).extract(y)将产生与extract(y).rotate(x)不同结果。

参数

  • angle number 旋转角度。(可选,默认auto
  • options Object 如果存在,则为具有可选属性的Object。
    • options.background (string|Object)由色彩模块解析,以提取红色,绿色,蓝色和Alpha的值。(可选,默认"#000000"

例子

  1. const pipeline = sharp()
  2. .rotate()
  3. .resize(null, 200)
  4. .toBuffer(function (err, outputBuffer, info) {
  5. // outputBuffer contains 200px high JPEG image data,
  6. // auto-rotated using EXIF Orientation tag
  7. // info.width and info.height contain the dimensions of the resized image
  8. });
  9. readableStream.pipe(pipeline);

返回Sharp实例对象

flip 翻转

围绕垂直Y轴翻转图像。这总是在旋转后发生(如果有)。使用flip暗指删除EXIFOrientation标签(如果有)。

参数

  • flip boolean (可选,默认true

例子

  1. const output = await sharp(input).flip().toBuffer();

返回sharp实例对象

flop 翻牌

围绕水平X轴翻转图像。这总是在旋转后发生(如果有)。使用flop暗指删除EXIFOrientation标签(如果有)。

参数

例子

  1. const output = await sharp(input).flop().toBuffer();

返回sharp实例对象

affine 仿射

对图像执行仿射变换。这个操作总是在调整大小、提取和旋转之后进行(如果有的话)。

必须提供一个长度为4的数组或者一个2x2的仿射变换矩阵。默认情况下,新像素填充黑色背景。您可以提供带有背景选项的背景颜色。还可以指定特定的插值器。将插值器选项设置为 sharp.interpreter 对象的属性,例如 sharp.interpreator.nohalo

对于2x2矩阵,变换是:

  • X = matrix[0, 0] (x + idx) + matrix[0, 1] (y + idy) + odx
  • Y = matrix[1, 0] (x + idx) + matrix[1, 1] (y + idy) + ody

说明:

  • X 和 y 是输入图像中的坐标
  • X 和 y 是输出图像中的坐标
  • (0,0)是左上角

参数

例子

  1. const pipeline = sharp()
  2. .affine([[1, 0.3], [0.1, 0.7]], {
  3. background: 'white',
  4. interpolate: sharp.interpolators.nohalo
  5. })
  6. .toBuffer((err, outputBuffer, info) => {
  7. // outputBuffer contains the transformed image
  8. // info.width and info.height contain the new dimensions
  9. });
  10. inputStream
  11. .pipe(pipeline);

返回sharp实例对象
Meta

  • since: 0.27.0

sharpen 锐化

锐化图像。不带参数使用时,对输出图像进行快速,温和的锐化。如果提供参数sigma,则在LAB颜色空间中对L通道进行较慢,更准确的锐化。可对“平坦”和“锯齿状”区域中的锐化程度进行单独控制。

参数

  • options (Object | number )? 带有属性的对象或options.sigma的数字(不推荐),(可选)
    • options.sigma number ?sigma = 1 + radius / 2 时,高斯模糊
    • options.m1 number 锐化级别适用于“flat”区域。(可选,默认 1.0)
    • options.m2 number 适用于“锯齿状”区域的锐化级别。(可选,默认 2.0)
    • options.x1 number “ flat”和“ jagged”之间的阈值。(可选,默认 2.0)
    • options.y2 number 最大增亮量(可选,默认 10.0)
    • options.y3 number 最大暗化量。(可选,默认 20.0)
  • flat number ? (已弃用) 改用 options.m1.
  • jagged number ? (已弃用) 改用 options.m2.

例子

  1. const data = await sharp(input).sharpen().toBuffer();
  1. const data = await sharp(input).sharpen({ sigma: 2 }).toBuffer();
  1. const data = await sharp(input)
  2. .sharpen({
  3. sigma: 2,
  4. m1: 0
  5. m2: 3,
  6. x1: 3,
  7. y2: 15,
  8. y3: 15,
  9. })
  10. .toBuffer();

返回sharp实例对象

median 中位数

应用中值过滤器。不带参数使用时,默认窗口为3x3。

参数

  • size number 平方掩码大小:大小x大小(可选,默认3

例子

  1. const output = await sharp(input).median().toBuffer();
  1. const output = await sharp(input).median(5).toBuffer();

返回sharp实例对象

blur 模糊

模糊图像。不带参数使用时,对输出图像进行快速,温和的模糊处理。当sigma被提供,执行更慢,更准确的高斯模糊。

参数

  • sigma number 介于0.3到1000之间的值,代表高斯蒙版的sigma,其中sigma = 1 + radius / 2

例子

  1. const boxBlurred = await sharp(input)
  2. .blur()
  3. .toBuffer();
  1. const gaussianBlurred = await sharp(input)
  2. .blur(5)
  3. .toBuffer();

返回Sharp实例对象

flatten 展平

合并Alpha透明通道(如果有),变成一个背景颜色。

参数

  • options Object
    • options.background (string|Object)背景颜色,由色彩模块解析,默认为黑色。(可选,默认{r:0,g:0,b:0}

例子

  1. await sharp(rgbaInput)
  2. .flatten({ background: '#F0A703' })
  3. .toBuffer();

返回sharp实例对象

gamma 伽玛

通过将编码(变暗)的预调整大小减小一个系数1/gamma, 然后再将编码(变亮)的后调整大小增大一个系数,来应用伽玛校正gamma。这可以改善在非线性颜色空间中调整大小后的图像的感知亮度。JPEG和WebP输入图像在应用伽玛校正时将不会利用负载收缩性能优化。

提供第二个参数以使用不同的输出伽玛值,否则在两种情况下都使用第一个值。

参数

  • gamma number 介于1.0和3.0之间的值。(可选,默认2.2
  • gammaOut number 值介于1.0和3.0之间。(可选,默认与gamma相同)
  • 无效参数将引发错误

返回sharp实例对象

negate 负

产生图像的“负”版本。

参数

  • negate boolean (可选,默认true

返回sharp实例对象

normalise 归一化

通过扩展其亮度以覆盖整个动态范围来增强输出图像的对比度。

参数

  • normalise boolean (可选,默认true

例子

  1. const output = await sharp(input).normalise().toBuffer();

返回sharp实例对象

normalize 归一化

normalise的替代拼写。

参数

  • normalize boolean (可选,默认true

例子

  1. const output = await sharp(input).normalize().toBuffer();

返回sharp实例对象

clahe 直方图均衡化

执行限制对比的适应直方图均衡化。

一般来说,这样可以通过突出更深的细节来提高图像的清晰度。

参数

  • options Object
    • options.width Number 以像素为单位的区域整数宽度
    • options.height Number 以像素为单位的区域整数高度
    • options.maxSlope Number 累积直方图斜率的最大值。值为0时禁用对比度限制。有效值是0-100(包含)范围内的整数(可选的,默认值 3)

例子

  1. const output = await sharp(input)
  2. .clahe({
  3. width: 3,
  4. height: 3,
  5. })
  6. .toBuffer();

返回sharp实例对象

convolve 卷积

将图像与指定的内核卷积。

参数

  • kernel Object
    • kernel.width number 内核的宽度(以像素为单位)。
    • kernel.height number 内核的宽度(以像素为单位)。
    • kernel.kernel Array < number > 包含内核值的长度width*height数组。
    • kernel.scale number 以像素为单位核的比例。(可选,默认sum
    • kernel.offset number 以像素为单位内核的偏移量。(可选,默认0

例子

  1. sharp(input)
  2. .convolve({
  3. width: 3,
  4. height: 3,
  5. kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]
  6. })
  7. .raw()
  8. .toBuffer(function(err, data, info) {
  9. // data contains the raw pixel data representing the convolution
  10. // of the input image with the horizontal Sobel operator
  11. });

返回sharp实例对象

threshold 阈

任何大于或等于阈值的像素值将被设置为255,否则它将被设置为0。

参数

  • threshold number 0-255范围内的值,表示将应用阈值的级别。(可选,默认128
  • options Object
    • options.greyscale boolean 转换为单通道灰度。(可选,默认true
    • options.grayscale boolean greyscale 的替代拼写。(可选,默认true
  • 无效参数将引发错误

返回sharp实例对象

boolean 布尔计算

对操作数映像执行按位布尔运算。

此操作将创建一个输出图像,其中每个像素是输入图像的相应像素之间选择的按位布尔值operation的结果。

参数

  • operand Buffer | string 包含图像数据的缓冲区或包含图像文件路径的字符串。
  • operator string andoreor中的一个执行该位操作,象C逻辑运算&|^
  • options Object
    • options.raw Object 描述使用原始像素数据时的操作数。
      • options.raw.width number
      • options.raw.height number
      • options.raw.channels number
  • 无效参数将引发错误

返回sharp实例对象

linear 线性运算

将线性公式a * input+ b应用于图像(级别调整)

参数

  • a number 乘数(可选,默认1.0
  • b number 偏移量(可选,默认0.0
  • 无效参数将引发错误

返回sharp实例对象

recomb 重组

用指定的矩阵重新排列图像。

参数

  • inputMatrix Array<number> 3x3重组矩阵

例子

  1. sharp(input)
  2. .recomb([
  3. [0.3588, 0.7044, 0.1368],
  4. [0.2990, 0.5870, 0.1140],
  5. [0.2392, 0.4696, 0.0912],
  6. ])
  7. .raw()
  8. .toBuffer(function(err, data, info) {
  9. // data contains the raw pixel data after applying the recomb
  10. // With this example input, a sepia filter has been applied
  11. });

返回sharp实例对象

注意

  • :0.21.1版本开始支持

modulate 调制

使用亮度,饱和度和色相旋转来变换图像。

参数

  • options Object
    • options.brightness number 亮度倍增
    • options.saturation number 饱和度倍增器
    • options.hue number 色相旋转度
    • options.lightness number 亮度增加

例子

  1. // increase brightness by a factor of 2
  2. const output = await sharp(input)
  3. .modulate({
  4. brightness: 2
  5. })
  6. .toBuffer();
  1. // hue-rotate by 180 degrees
  2. const output = await sharp(input)
  3. .modulate({
  4. hue: 180
  5. })
  6. .toBuffer();
  1. // increase lightness by +50
  2. const output = await sharp(input)
  3. .modulate({
  4. lightness: 50
  5. })
  6. .toBuffer();
  1. // decreate brightness and saturation while also hue-rotating by 90 degrees
  2. const output = await sharp(input)
  3. .modulate({
  4. brightness: 0.5,
  5. saturation: 0.5,
  6. hue: 90,
  7. })
  8. .toBuffer();

返回sharp实例对象

提醒

  • :0.22.1版本开始支持