sharp-apng
v0.1.5
Published
APNG(animated PNG) encoder and decoder for sharp base on upng-js.
Maintainers
Readme
sharp-apng
APNG(animated PNG) encoder and decoder for sharp base on upng-js.
Install
npm install sharp-apngUsage
const sharp = require("sharp");
const apng = require("sharp-apng");
(async () => {
// APNG to GIF
const image = await apng.sharpFromApng("./animated.png");
image.toFile("./output/apng2gif.gif");
// APNG to frames
const frames = apng.framesFromApng("./animated.png");
frames.forEach((frame, index) => {
frame.toFile(`./output/apng-${("000" + index).substr(-4)}.png`);
});
// GIF to APNG
apng.sharpToApng(
sharp("./animated.gif", { animated: true }),
"./output/gif2apng.png"
);
// frames to APNG
apng.framesToApng(
[
sharp("./frames/0000.png"),
sharp("./frames/0001.png"),
sharp("./frames/0002.png"),
],
"./output/animated.png"
);
})();API
apng.framesFromApng(input, resolveWithObject?)
Create instances of sharp from APNG frames.
input(String | Buffer) - A String containing the filesystem path to an APNG image file, or a Buffer containing APNG image data.resolveWithObjectBoolean (optional) - Return an ImageData containingframes(an array of instances of sharp) property and decoding info instead of only an array of instances of sharp. Default byfalse.
Returns Sharp[] | ImageData - Return an array of instance of sharp, or an ImageData containing frames (an array of instances of sharp) property and decoding info.
apng.sharpFromApng(input, options?, resolveWithObject?)
Create an instance of animated sharp from an APNG image.
input(String | Buffer) - A String containing the filesystem path to an APNG image file, or a Buffer containing APNG image data.optionsDecoderOptions - Options for encode animated GIF and create sharp instance.resolveWithObjectBoolean (optional) - Return an ImageData containingimage(an instance of sharp) property and decoding info instead of only an instance of sharp. Default byfalse.
Returns Promise<Sharp | ImageData> - Resolve with an instance of animated sharp, or an ImageData containing image (an instances of sharp) property and decoding info.
DecoderOptions
Options for encode animated GIF and create sharp instance.
sharpOptionsNumber (optional) - Sharp constructor options.delay(Number | Number[]) (optional) - Delay(s) between animation frames (in milliseconds).repeatNumber (optional) - Number of animation iterations, use0for infinite animation. Default by0.transparentBoolean (optional) - Enable 1-bit transparency for the GIF. Default byfalse.maxColorsNumber (optional) - Quantize the total number of colors down to a reduced palette no greater than maxColors. Default by256.format("rgb565" | "rgb444" | "rgba4444") (optional) - Color format. Default byrgb565.rgb565means 5 bits red, 6 bits green, 5 bits blue (better quality, slower)rgb444is 4 bits per channel (lower quality, faster)rgba4444is the same as above but with alpha support
gifEncoderOptionsObject (optional) - gifenc GIFEncoder() options.gifEncoderQuantizeOptionsObject (optional) - gifenc quantize() options.gifEncoderFrameOptionsObject (optional) - gifenc gif.writeFrame() options.
ImageData
Contains the following decoding info:
widthNumber - The width of the image, in pixels.heightNumber - The height of the image, in pixels.depthNumber - Number of bits per channel.ctypeNumber - Color type of the file (Truecolor, Grayscale, Palette ...).pagesNumber - Number of frames contained within the image.delayNumber[] - Delay in ms between each frame.framesSharp[] (apng.framesFromApng()only) - Instances of sharp from APNG frames.imageSharp (apng.sharpFromApng()only) - Animated sharp instance.
apng.framesToApng(images, fileOut, options?)
Write an APNG file from sharps.
imagesSharp[] - An array of instances of sharp.fileOutString - The path to write the image data to.optionsEncoderOptions (optional) - Options for resize frames and encoding APNG.
Returns Promise<Object> - Resolve with an Object containing size, width, height properties.
apng.sharpToApng(image, fileOut, options?)
Write an APNG file from an animated sharp.
imageSharp - An instance of animated sharp.fileOutString - The path to write the image data to.optionsEncoderOptions (optional) - Options for resize frames and encoding APNG.
Returns Promise<Object> - Resolve with an Object containing size, width, height properties.
EncoderOptions
Options for resize frames and encode APNG.
widthNumber (optional) - Width, in pixels, of the APNG to output. If omitted, will useresizeTooption.heightNumber (optional) - Height, in pixels, of the APNG to output. If omitted, will useresizeTooption.cnumNumber (optional) - Number of colors in the result; 0: all colors (lossless PNG)delay(Number | Number[]) (optional) - Delay(s) between animation frames (in milliseconds, only when 2 or more frames)resizeTo("largest" | "smallest") (optional) - Resize all frame to thelargestframe orsmallestframe size. Default bylargest.resizeType("zoom" | "crop") (optional) -zoomuse sharp.resize(),cropuse sharp.extend() and sharp.extract().resizeOptionssharp.ResizeOptions (optional) - Options for sharp.resize().extendBackgroundsharp.Color (optional) - Background option for sharp.extend(). Default by{ r: 0, g: 0, b: 0, alpha: 0 }.
Change Log
0.1.1
- Feature: Remove sharp-gif dependency, use gif-encoder to encode animated GIF buffer directly to improve performance.
0.1.5
- Feature: Use gifenc instead of gif-encoder to encode animated GIF buffer.
