or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

color-channels.mdcomposition.mdconstructor-input.mdindex.mdmetadata-stats.mdoperations-filters.mdoutput-formats.mdresize-geometry.mdutilities-performance.md
tile.json

tessl/npm-sharp

High performance Node.js image processing library for resizing JPEG, PNG, WebP, GIF, AVIF and TIFF images

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/sharp@0.34.x

To install, run

npx @tessl/cli install tessl/npm-sharp@0.34.0

index.mddocs/

Sharp

Sharp is a high-performance Node.js image processing library that provides fast image transformations including resizing, format conversion, rotation, extraction, compositing, and color space management. Built on the libvips image processing library, it delivers 4x-5x faster performance than ImageMagick while maintaining quality through Lanczos resampling and proper handling of color spaces, ICC profiles, and alpha channels.

Package Information

  • Package Name: sharp
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install sharp

Core Imports

const sharp = require('sharp');

For TypeScript:

import sharp from 'sharp';

Basic Usage

const sharp = require('sharp');

// Basic resize operation
await sharp('input.jpg')
  .resize(300, 200)
  .toFile('output.jpg');

// Pipeline with multiple operations  
const result = await sharp('input.png')
  .rotate(90)
  .resize({ width: 800, fit: 'contain' })
  .jpeg({ quality: 90 })
  .toBuffer();

// Stream processing
const transformer = sharp()
  .resize(400)
  .webp({ quality: 80 });

readableStream
  .pipe(transformer)
  .pipe(writableStream);

Architecture

Sharp is built around several key components:

  • Fluent API: All operations return Sharp instances for method chaining
  • Stream Interface: Implements Node.js Duplex streams for pipeline processing
  • Format Support: Native support for JPEG, PNG, WebP, GIF, AVIF, TIFF, HEIF, SVG, and RAW formats
  • Performance Controls: Multi-threading, SIMD optimizations, and caching management
  • Metadata Preservation: Comprehensive EXIF, ICC, XMP, and IPTC metadata handling
  • Memory Management: Configurable limits and monitoring for production use

Capabilities

Constructor and Input Handling

Create Sharp instances from various input sources including files, buffers, streams, and generated content.

function sharp(input?: SharpInput | SharpInput[], options?: SharpOptions): Sharp;

type SharpInput = Buffer | ArrayBuffer | Uint8Array | Uint8ClampedArray | 
  Int8Array | Uint16Array | Int16Array | Uint32Array | Int32Array | 
  Float32Array | Float64Array | string;

interface SharpOptions {
  autoOrient?: boolean;
  failOn?: 'none' | 'truncated' | 'error' | 'warning';
  limitInputPixels?: number | boolean;
  density?: number;
  pages?: number;
  page?: number;
  animated?: boolean;
  raw?: CreateRaw;
  create?: Create;
  text?: CreateText;
  join?: Join;
}

Constructor and Input

Image Resizing and Geometry

Resize, crop, extend, and extract regions from images with multiple fit strategies and interpolation methods.

resize(width?: number | ResizeOptions, height?: number, options?: ResizeOptions): Sharp;
extract(region: Region): Sharp;
extend(extend: number | ExtendOptions): Sharp;
trim(options?: TrimOptions): Sharp;

interface ResizeOptions {
  width?: number;
  height?: number;
  fit?: 'cover' | 'contain' | 'fill' | 'inside' | 'outside';
  position?: string | number;
  background?: string | object;
  kernel?: 'nearest' | 'cubic' | 'mitchell' | 'lanczos2' | 'lanczos3';
  withoutEnlargement?: boolean;
  withoutReduction?: boolean;
}

Resizing and Geometry

Image Operations and Filters

Apply filters, effects, and transformations including rotation, sharpening, blurring, and color adjustments.

rotate(angle?: number, options?: RotateOptions): Sharp;
sharpen(options?: SharpenOptions): Sharp;
blur(sigma?: number | boolean | BlurOptions): Sharp;
gamma(gamma?: number, gammaOut?: number): Sharp;
negate(negate?: boolean | NegateOptions): Sharp;
normalise(options?: NormaliseOptions): Sharp;
modulate(options?: ModulateOptions): Sharp;

interface SharpenOptions {
  sigma: number;
  m1?: number;
  m2?: number;
  x1?: number;
  y2?: number;
  y3?: number;
}

Operations and Filters

Color Space and Channel Management

Control color spaces, manipulate channels, and apply color transformations.

tint(tint: string | object): Sharp;
greyscale(greyscale?: boolean): Sharp;
toColourspace(colourspace?: string): Sharp;
extractChannel(channel: 0 | 1 | 2 | 3 | 'red' | 'green' | 'blue' | 'alpha'): Sharp;
removeAlpha(): Sharp;
ensureAlpha(alpha?: number): Sharp;

interface RGBA {
  r?: number;
  g?: number;
  b?: number;
  alpha?: number;
}

Color and Channels

Image Composition

Composite multiple images with various blend modes and positioning options.

composite(images: OverlayOptions[]): Sharp;

interface OverlayOptions {
  input: string | Buffer | object;
  blend?: 'clear' | 'source' | 'over' | 'multiply' | 'screen' | 'overlay' | 
    'darken' | 'lighten' | 'difference' | 'exclusion';
  gravity?: 'north' | 'northeast' | 'east' | 'southeast' | 'south' |
    'southwest' | 'west' | 'northwest' | 'center' | 'centre';
  top?: number;
  left?: number;
  tile?: boolean;
}

Image Composition

Output and Format Control

Write images to files or buffers with format-specific options and metadata control.

toFile(fileOut: string, callback?: (err: Error, info: OutputInfo) => void): Promise<OutputInfo>;
toBuffer(options?: BufferOptions, callback?: (err: Error, buffer: Buffer, info: OutputInfo) => void): Promise<Buffer>;
jpeg(options?: JpegOptions): Sharp;
png(options?: PngOptions): Sharp;
webp(options?: WebpOptions): Sharp;
avif(options?: AvifOptions): Sharp;
tiff(options?: TiffOptions): Sharp;

interface OutputInfo {
  format: string;
  size: number;
  width: number;
  height: number;
  channels: number;
  premultiplied: boolean;
}

Output and Formats

Metadata and Statistics

Access comprehensive image metadata and pixel-level statistics without decoding image data.

metadata(): Promise<Metadata>;
stats(): Promise<Stats>;

interface Metadata {
  format: string;
  width: number;
  height: number;
  channels: number;
  depth: string;
  space: string;
  hasProfile: boolean;
  hasAlpha: boolean;
  orientation?: number;
  density?: number;
  pages?: number;
}

interface Stats {
  channels: ChannelStats[];
  isOpaque: boolean;
  entropy: number;
  sharpness: number;
  dominant: { r: number; g: number; b: number };
}

Metadata and Stats

Utility Functions and Performance

Control library behavior, performance settings, and access version information.

// Static utility functions
sharp.cache(options?: boolean | CacheOptions): CacheResult;
sharp.concurrency(concurrency?: number): number;
sharp.counters(): SharpCounters;
sharp.simd(enable?: boolean): boolean;

// Library information
sharp.format: FormatEnum;
sharp.versions: VersionInfo;
sharp.interpolators: Interpolators;

Utilities and Performance

Core Types

interface Sharp {
  // Sharp extends Node.js Duplex stream
  clone(): Sharp;
}

interface Region {
  left: number;
  top: number;
  width: number;
  height: number;
}

interface Create {
  width: number;
  height: number;
  channels: 3 | 4;
  background: string | RGBA;
}

interface CreateText {
  text: string;
  font?: string;
  fontfile?: string;
  width?: number;
  height?: number;
  align?: 'left' | 'centre' | 'center' | 'right';
  justify?: boolean;
  dpi?: number;
  rgba?: boolean;
  spacing?: number;
  wrap?: 'word' | 'char' | 'word-char' | 'none';
}