Write output image data to a file.
If an explicit output format is not selected, it will be inferred from the extension, with JPEG, PNG, WebP, TIFF, DZI, and libvips' V format supported. Note that raw pixel data is only supported for buffer output.
A Promise
is returned when callback
is not provided.
Parameters
-
fileOut
String the path to write the image data to. -
callback
Function? called on completion with two arguments(err, info)
.info
contains the output imageformat
,size
(bytes),width
,height
,channels
andpremultiplied
(indicating if premultiplication was used). When using a crop strategy also containscropOffsetLeft
andcropOffsetTop
. -
Throws Error Invalid parameters
Returns Promise<Object> when no callback is provided
Write output to a Buffer. JPEG, PNG, WebP, TIFF and RAW output are supported. By default, the format will match the input image, except GIF and SVG input which become PNG output.
callback
, if present, gets three arguments (err, data, info)
where:
err
is an error, if any.data
is the output image data.info
contains the output imageformat
,size
(bytes),width
,height
,channels
andpremultiplied
(indicating if premultiplication was used). When using a crop strategy also containscropOffsetLeft
andcropOffsetTop
.
A Promise
is returned when callback
is not provided.
Parameters
options
Object?options.resolveWithObject
Boolean? Resolve the Promise with an Object containingdata
andinfo
properties instead of resolving only withdata
.
callback
Function?
Returns Promise<Buffer> when no callback is provided
Include all metadata (EXIF, XMP, IPTC) from the input image in the output image.
The default behaviour, when withMetadata
is not used, is to strip all metadata and convert to the device-independent sRGB colour space.
This will also convert to and add a web-friendly sRGB ICC profile.
Parameters
-
withMetadata
Object?withMetadata.orientation
Number? value between 1 and 8, used to update the EXIFOrientation
tag.
-
Throws Error Invalid parameters
Returns Sharp
Use these JPEG options for output image.
Parameters
-
options
Object? output optionsoptions.quality
Number quality, integer 1-100 (optional, default80
)options.progressive
Boolean use progressive (interlace) scan (optional, defaultfalse
)options.chromaSubsampling
String set to '4:4:4' to prevent chroma subsampling when quality <= 90 (optional, default'4:2:0'
)options.trellisQuantisation
Boolean apply trellis quantisation, requires mozjpeg (optional, defaultfalse
)options.overshootDeringing
Boolean apply overshoot deringing, requires mozjpeg (optional, defaultfalse
)options.optimiseScans
Boolean optimise progressive scans, forces progressive, requires mozjpeg (optional, defaultfalse
)options.optimizeScans
Boolean alternative spelling of optimiseScans (optional, defaultfalse
)options.force
Boolean force JPEG output, otherwise attempt to use input format (optional, defaulttrue
)
-
Throws Error Invalid options
Returns Sharp
Use these PNG options for output image.
Parameters
-
options
Object?options.progressive
Boolean use progressive (interlace) scan (optional, defaultfalse
)options.compressionLevel
Number zlib compression level, 0-9 (optional, default9
)options.adaptiveFiltering
Boolean use adaptive row filtering (optional, defaultfalse
)options.force
Boolean force PNG output, otherwise attempt to use input format (optional, defaulttrue
)
-
Throws Error Invalid options
Returns Sharp
Use these WebP options for output image.
Parameters
-
options
Object? output optionsoptions.quality
Number quality, integer 1-100 (optional, default80
)options.alphaQuality
Number quality of alpha layer, integer 0-100 (optional, default100
)options.lossless
Boolean use lossless compression mode (optional, defaultfalse
)options.nearLossless
Boolean use near_lossless compression mode (optional, defaultfalse
)options.force
Boolean force WebP output, otherwise attempt to use input format (optional, defaulttrue
)
-
Throws Error Invalid options
Returns Sharp
Use these TIFF options for output image.
Parameters
-
options
Object? output optionsoptions.quality
Number quality, integer 1-100 (optional, default80
)options.force
Boolean force TIFF output, otherwise attempt to use input format (optional, defaulttrue
)options.compression
Boolean compression options: lzw, deflate, jpeg (optional, default'jpeg'
)options.predictor
Boolean compression predictor options: none, horizontal, float (optional, default'horizontal'
)options.xres
Number horizontal resolution in pixels/mm (optional, default1.0
)options.yres
Number vertical resolution in pixels/mm (optional, default1.0
)options.squash
Boolean squash 8-bit images down to 1 bit (optional, defaultfalse
)
-
Throws Error Invalid options
Returns Sharp
Force output to be raw, uncompressed uint8 pixel data.
Returns Sharp
Force output to a given format.
Parameters
-
format
(String | Object) as a String or an Object with an 'id' attribute -
options
Object output options -
Throws Error unsupported format or options
Returns Sharp
Use tile-based deep zoom (image pyramid) output.
Set the format and options for tile images via the toFormat
, jpeg
, png
or webp
functions.
Use a .zip
or .szi
file extension with toFile
to write to a compressed archive file format.
Parameters
tile
Object?tile.size
Number tile size in pixels, a value between 1 and 8192. (optional, default256
)tile.overlap
Number tile overlap in pixels, a value between 0 and 8192. (optional, default0
)tile.container
String tile container, with valuefs
(filesystem) orzip
(compressed file). (optional, default'fs'
)tile.layout
String filesystem layout, possible values aredz
,zoomify
orgoogle
. (optional, default'dz'
)
Examples
sharp('input.tiff')
.png()
.tile({
size: 512
})
.toFile('output.dz', function(err, info) {
// output.dzi is the Deep Zoom XML definition
// output_files contains 512x512 tiles grouped by zoom level
});
- Throws Error Invalid parameters
Returns Sharp