rotate 旋转
根据EXIFOrientation标记,以明确的角度或自动定向旋转输出图像。
如果提供一个角度,则将其转换为有效的正角度旋转。例如,-450将产生270度旋转。
当旋转角度不是90的倍数时,可以使用该background选项提供背景颜色。
如果未提供角度,则根据EXIF数据确定角度。支持镜像,并且可以推断出翻转操作的使用。
使用rotate暗指删除EXIFOrientation标签(如果有)。
当旋转区域和提取区域时,方法顺序都很重要,例如,rotate(x).extract(y)将产生与extract(y).rotate(x)不同结果。
参数
anglenumber 旋转角度。(可选,默认auto)optionsObject ?如果存在,则为具有可选属性的Object。options.background(string|Object)由色彩模块解析,以提取红色,绿色,蓝色和Alpha的值。(可选,默认"#000000")
例子
const pipeline = sharp().rotate().resize(null, 200).toBuffer(function (err, outputBuffer, info) {// outputBuffer contains 200px high JPEG image data,// auto-rotated using EXIF Orientation tag// info.width and info.height contain the dimensions of the resized image});readableStream.pipe(pipeline);
- 无效参数将引发错误
返回Sharp实例对象
flip 翻转
围绕垂直Y轴翻转图像。这总是在旋转后发生(如果有)。使用flip暗指删除EXIFOrientation标签(如果有)。
参数
flipboolean (可选,默认true)
例子
const output = await sharp(input).flip().toBuffer();
返回sharp实例对象
flop 翻牌
围绕水平X轴翻转图像。这总是在旋转后发生(如果有)。使用flop暗指删除EXIFOrientation标签(如果有)。
参数
flop布尔值 (可选,默认true)
例子
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)是左上角
参数
matrix(Array <Array <number >> | Array <number >) 仿射变换矩阵optionsObject ? 如果存在 Object? ,则为具有可选属性的 Object。options.background(String 字符串 | Object 对象 ) 由color 颜色模块来提取红色、绿色、蓝色和 alpha 的值(可选,默认"#000000")options.idxNumber 数目 输入水平偏移量(可选,默认0)options.idyNumber 数目 输入垂直偏移量(可选,默认0)options.odxNumber 数目 输出水平偏移量(可选,默认值0)options.odyNumber 数目 输出垂直偏移量(可选,默认值0)options.interpolatorString 字符串 插值器(可选,默认值sharp.interpolators.bicubic)
例子
const pipeline = sharp().affine([[1, 0.3], [0.1, 0.7]], {background: 'white',interpolate: sharp.interpolators.nohalo}).toBuffer((err, outputBuffer, info) => {// outputBuffer contains the transformed image// info.width and info.height contain the new dimensions});inputStream.pipe(pipeline);
- 无效参数将引发错误
返回sharp实例对象
Meta
- since: 0.27.0
sharpen 锐化
锐化图像。不带参数使用时,对输出图像进行快速,温和的锐化。如果提供参数sigma,则在LAB颜色空间中对L通道进行较慢,更准确的锐化。可对“平坦”和“锯齿状”区域中的锐化程度进行单独控制。
参数
options(Object | number )? 带有属性的对象或options.sigma的数字(不推荐),(可选)flatnumber ? (已弃用) 改用options.m1.jaggednumber ? (已弃用) 改用options.m2.
例子
const data = await sharp(input).sharpen().toBuffer();
const data = await sharp(input).sharpen({ sigma: 2 }).toBuffer();
const data = await sharp(input).sharpen({sigma: 2,m1: 0m2: 3,x1: 3,y2: 15,y3: 15,}).toBuffer();
- 无效参数将引发错误
返回sharp实例对象
median 中位数
应用中值过滤器。不带参数使用时,默认窗口为3x3。
参数
sizenumber 平方掩码大小:大小x大小(可选,默认3)
例子
const output = await sharp(input).median().toBuffer();
const output = await sharp(input).median(5).toBuffer();
- 无效参数引发错误
返回sharp实例对象
blur 模糊
模糊图像。不带参数使用时,对输出图像进行快速,温和的模糊处理。当sigma被提供,执行更慢,更准确的高斯模糊。
参数
sigmanumber ?介于0.3到1000之间的值,代表高斯蒙版的sigma,其中sigma = 1 + radius / 2。
例子
const boxBlurred = await sharp(input).blur().toBuffer();
const gaussianBlurred = await sharp(input).blur(5).toBuffer();
- 无效参数将引发错误
返回Sharp实例对象
flatten 展平
合并Alpha透明通道(如果有),变成一个背景颜色。
参数
optionsObject ?options.background(string|Object)背景颜色,由色彩模块解析,默认为黑色。(可选,默认{r:0,g:0,b:0})
例子
await sharp(rgbaInput).flatten({ background: '#F0A703' }).toBuffer();
返回sharp实例对象
gamma 伽玛
通过将编码(变暗)的预调整大小减小一个系数1/gamma, 然后再将编码(变亮)的后调整大小增大一个系数,来应用伽玛校正gamma。这可以改善在非线性颜色空间中调整大小后的图像的感知亮度。JPEG和WebP输入图像在应用伽玛校正时将不会利用负载收缩性能优化。
提供第二个参数以使用不同的输出伽玛值,否则在两种情况下都使用第一个值。
参数
gammanumber 介于1.0和3.0之间的值。(可选,默认2.2)gammaOutnumber ?值介于1.0和3.0之间。(可选,默认与gamma相同)- 无效参数将引发错误
返回sharp实例对象
negate 负
产生图像的“负”版本。
参数
negateboolean (可选,默认true)
返回sharp实例对象
normalise 归一化
通过扩展其亮度以覆盖整个动态范围来增强输出图像的对比度。
参数
normaliseboolean (可选,默认true)
例子
const output = await sharp(input).normalise().toBuffer();
返回sharp实例对象
normalize 归一化
normalise的替代拼写。
参数
normalizeboolean (可选,默认true)
例子
const output = await sharp(input).normalize().toBuffer();
返回sharp实例对象
clahe 直方图均衡化
执行限制对比的适应直方图均衡化。
一般来说,这样可以通过突出更深的细节来提高图像的清晰度。
参数
optionsObjectoptions.widthNumber 以像素为单位的区域整数宽度options.heightNumber 以像素为单位的区域整数高度options.maxSlopeNumber 累积直方图斜率的最大值。值为0时禁用对比度限制。有效值是0-100(包含)范围内的整数(可选的,默认值3)
例子
const output = await sharp(input).clahe({width: 3,height: 3,}).toBuffer();
- 无效参数将引发错误
返回sharp实例对象
convolve 卷积
将图像与指定的内核卷积。
参数
kernelObject
例子
sharp(input).convolve({width: 3,height: 3,kernel: [-1, 0, 1, -2, 0, 2, -1, 0, 1]}).raw().toBuffer(function(err, data, info) {// data contains the raw pixel data representing the convolution// of the input image with the horizontal Sobel operator});
- 无效参数将引发错误
返回sharp实例对象
threshold 阈
任何大于或等于阈值的像素值将被设置为255,否则它将被设置为0。
参数
thresholdnumber 0-255范围内的值,表示将应用阈值的级别。(可选,默认128)optionsObject ?options.greyscaleboolean 转换为单通道灰度。(可选,默认true)options.grayscalebooleangreyscale的替代拼写。(可选,默认true)
- 无效参数将引发错误
返回sharp实例对象
boolean 布尔计算
对操作数映像执行按位布尔运算。
此操作将创建一个输出图像,其中每个像素是输入图像的相应像素之间选择的按位布尔值operation的结果。
参数
operand(Buffer | string )包含图像数据的缓冲区或包含图像文件路径的字符串。operatorstringand,or或eor中的一个执行该位操作,象C逻辑运算&,|和^。optionsObject ?options.rawObject ?描述使用原始像素数据时的操作数。options.raw.widthnumber ?options.raw.heightnumber ?options.raw.channelsnumber ?
- 无效参数将引发错误
返回sharp实例对象
linear 线性运算
将线性公式a * input+ b应用于图像(级别调整)
参数
anumber 乘数(可选,默认1.0)bnumber 偏移量(可选,默认0.0)- 无效参数将引发错误
返回sharp实例对象
recomb 重组
用指定的矩阵重新排列图像。
参数
inputMatrixArray<number>3x3重组矩阵
例子
sharp(input).recomb([[0.3588, 0.7044, 0.1368],[0.2990, 0.5870, 0.1140],[0.2392, 0.4696, 0.0912],]).raw().toBuffer(function(err, data, info) {// data contains the raw pixel data after applying the recomb// With this example input, a sepia filter has been applied});
- 无效参数将引发错误
返回sharp实例对象
注意
- 从:0.21.1版本开始支持
modulate 调制
使用亮度,饱和度和色相旋转来变换图像。
参数
optionsObject ?options.brightnessnumber ?亮度倍增options.saturationnumber ?饱和度倍增器options.huenumber ?色相旋转度options.lightnessnumber ? 亮度增加
例子
// increase brightness by a factor of 2const output = await sharp(input).modulate({brightness: 2}).toBuffer();
// hue-rotate by 180 degreesconst output = await sharp(input).modulate({hue: 180}).toBuffer();
// increase lightness by +50const output = await sharp(input).modulate({lightness: 50}).toBuffer();
// decreate brightness and saturation while also hue-rotating by 90 degreesconst output = await sharp(input).modulate({brightness: 0.5,saturation: 0.5,hue: 90,}).toBuffer();
返回sharp实例对象
提醒
- 从:0.22.1版本开始支持
