图像操作
rotate¶
Rotate the output image by either an explicit angle
or auto-orient based on the EXIF Orientation
tag.
If an angle is provided, it is converted to a valid 90/180/270deg rotation.
For example, -450
will produce a 270deg rotation.
If no angle is provided, it is determined from the EXIF data. Mirroring is supported and may infer the use of a flip operation.
The use of rotate
implies the removal of the EXIF Orientation
tag, if any.
Method order is important when both rotating and extracting regions,
for example rotate(x).extract(y)
will produce a different result to extract(y).rotate(x)
.
参数
angle
Number angle of rotation, must be a multiple of 90. (optional, defaultauto
)
示例
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);
- Throws Error Invalid parameters
返回 Sharp
extract¶
Extract a region of the image.
- Use
extract
beforeresize
for pre-resize extraction. - Use
extract
afterresize
for post-resize extraction. - Use
extract
before and after for both.
参数
options
Object
示例
sharp(input)
.extract({ left: left, top: top, width: width, height: height })
.toFile(output, function(err) {
// Extract a region of the input image, saving in the same format.
});
sharp(input)
.extract({ left: leftOffsetPre, top: topOffsetPre, width: widthPre, height: heightPre })
.resize(width, height)
.extract({ left: leftOffsetPost, top: topOffsetPost, width: widthPost, height: heightPost })
.toFile(output, function(err) {
// Extract a region, resize, then extract from the resized image
});
- Throws Error Invalid parameters
返回 Sharp
flip¶
Flip the image about the vertical Y axis. This always occurs after rotation, if any.
The use of flip
implies the removal of the EXIF Orientation
tag, if any.
参数
flip
Boolean (optional, defaulttrue
)
返回 Sharp
flop¶
Flop the image about the horizontal X axis. This always occurs after rotation, if any.
The use of flop
implies the removal of the EXIF Orientation
tag, if any.
参数
flop
Boolean (optional, defaulttrue
)
返回 Sharp
sharpen¶
Sharpen the image.
When used without parameters, performs a fast, mild sharpen of the output image.
When a sigma
is provided, performs a slower, more accurate sharpen of the L channel in the LAB colour space.
Separate control over the level of sharpening in "flat" and "jagged" areas is available.
参数
sigma
Number? the sigma of the Gaussian mask, wheresigma = 1 + radius / 2
.flat
Number the level of sharpening to apply to "flat" areas. (optional, default1.0
)-
jagged
Number the level of sharpening to apply to "jagged" areas. (optional, default2.0
) -
Throws Error Invalid parameters
返回 Sharp
blur¶
Blur the image.
When used without parameters, performs a fast, mild blur of the output image.
When a sigma
is provided, performs a slower, more accurate Gaussian blur.
参数
-
sigma
Number? a value between 0.3 and 1000 representing the sigma of the Gaussian mask, wheresigma = 1 + radius / 2
. -
Throws Error Invalid parameters
返回 Sharp
extend¶
Extends/pads the edges of the image with the colour provided to the background
method.
This operation will always occur after resizing and extraction, if any.
参数
示例
// Resize to 140 pixels wide, then add 10 transparent pixels
// to the top, left and right edges and 20 to the bottom edge
sharp(input)
.resize(140)
.background({r: 0, g: 0, b: 0, alpha: 0})
.extend({top: 10, bottom: 20, left: 10, right: 10})
...
- Throws Error Invalid parameters
返回 Sharp
flatten¶
Merge alpha transparency channel, if any, with background
.
参数
flatten
Boolean (optional, defaulttrue
)
返回 Sharp
trim¶
Trim "boring" pixels from all edges that contain values within a percentage similarity of the top-left pixel.
参数
-
tolerance
Number value between 1 and 99 representing the percentage similarity. (optional, default10
) -
Throws Error Invalid parameters
返回 Sharp
gamma¶
Apply a gamma correction by reducing the encoding (darken) pre-resize at a factor of 1/gamma
then increasing the encoding (brighten) post-resize at a factor of gamma
.
This can improve the perceived brightness of a resized image in non-linear colour spaces.
JPEG and WebP input images will not take advantage of the shrink-on-load performance optimisation
when applying a gamma correction.
参数
返回 Sharp
negate¶
Produce the "negative" of the image.
参数
negate
Boolean (optional, defaulttrue
)
返回 Sharp
normalise¶
Enhance output image contrast by stretching its luminance to cover the full dynamic range.
参数
normalise
Boolean (optional, defaulttrue
)
返回 Sharp
normalize¶
Alternative spelling of normalise.
参数
normalize
Boolean (optional, defaulttrue
)
返回 Sharp
convolve¶
Convolve the image with the specified kernel.
参数
kernel
Objectkernel.width
Number width of the kernel in pixels.kernel.height
Number width of the kernel in pixels.kernel.kernel
Array<Number> Array of lengthwidth*height
containing the kernel values.kernel.scale
Number the scale of the kernel in pixels. (optional, defaultsum
)kernel.offset
Number the offset of the kernel in pixels. (optional, default0
)
示例
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
});
- Throws Error Invalid parameters
返回 Sharp
threshold¶
Any pixel value greather than or equal to the threshold value will be set to 255, otherwise it will be set to 0.
参数
threshold
Number a value in the range 0-255 representing the level at which the threshold will be applied. (optional, default128
)-
options
Object? -
Throws Error Invalid parameters
返回 Sharp
boolean¶
Perform a bitwise boolean operation with operand image.
This operation creates an output image where each pixel is the result of
the selected bitwise boolean operation
between the corresponding pixels of the input images.
参数
operand
(Buffer | String) Buffer containing image data or String containing the path to an image file.operator
String one ofand
,or
oreor
to perform that bitwise operation, like the C logic operators&
,|
and^
respectively.-
options
Object? -
Throws Error Invalid parameters
返回 Sharp