16-bit PNG in sharp silently becomes 8-bit: bytes and banding
DEV Community

16-bit PNG in sharp silently becomes 8-bit: bytes and banding

sharp converts a 16-bit PNG to 8-bit unless you ask for 16 bits. In my test, a 10,358,752-byte 16-bit PNG came back as a 66,111-byte 8-bit PNG from a plain .png() call. The green channel dropped from 11,496 distinct levels to 46.

To keep 16 bits, add .toColourspace('rgb16') before .png(). That gave 9,605,628 bytes. Whether the loss matters depends on the image. Here is what it costs and how to check.

How do I make a real 16-bit test file?

Most "16-bit" files I could find were 8-bit photos widened to 16 bits, which tests nothing. I wrote a PNG by hand: a dim, smooth sky (green from 1,080 to 12,595 of 65,535) with 2 levels of noise, 1920 × 1280. This is a synthetic gradient, not a photo.

// make16.cjs: write a true 16-bit RGB PNG
const zlib = require('node:zlib');
const fs = require('node:fs');
const W = 1920, H = 1280, row = W * 6 + 1;
const raw = Buffer.alloc(row * H);
let seed = 12345;
const rnd = () => ((seed = (seed * 1664525 + 1013904223) >>> 0) / 2 ** 32);
for (let y = 0; y < H; y++) {
  raw[y * row] = 0; // filter type 0
  for (let x = 0; x < W; x++) {
    const base = 1200 + ((y / H) * 0.7 + (x / W) * 0.3) * 12800;
    const o = y * row + 1 + x * 6;
    raw.writeUInt16BE(Math.round(base * 0.8 + rnd() * 3), o);
    raw.writeUInt16BE(Math.round(base * 0.9 + rnd() * 3), o + 2);
    raw.writeUInt16BE(Math.round(base + rnd() * 3), o + 4);
  }
}
const chunk = (type, data) => {
  const len = Buffer.alloc(4);
  len.writeUInt32BE(data.length);
  const body = Buffer.concat([Buffer.from(type), data]);
  const crc = Buffer.alloc(4);
  crc.writeUInt32BE(zlib.crc32(body));
  return Buffer.concat([len, body, crc]);
};
const ihdr = Buffer.alloc(13);
ihdr.writeUInt32BE(W, 0);
ihdr.writeUInt32BE(H, 4);
ihdr[8] = 16;
ihdr[9] = 2; // 16 bits per sample, colour type 2 (RGB)
fs.writeFileSync('sky16.png', Buffer.concat([
  Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
  chunk('IHDR', ihdr),
  chunk('IDAT', zlib.deflateSync(raw, { level: 9 })),
  chunk('IEND', Buffer.alloc(0)),
]));

zlib.crc32 needs Node 22 or newer. Bit depth and colour type live at bytes 24 and 25 of any PNG (the IHDR chunk comes first, per the PNG spec), so a one-line ihdr(buf) helper can tell you what a file really is.

What does a plain sharp round trip do?

const src = fs.readFileSync('sky16.png');
const m = await sharp(src).metadata();
const same = await sharp(src).png({ compressionLevel: 9 }).toBuffer();
const keep = await sharp(src).toColourspace('rgb16').png({ compressionLevel: 9 }).toBuffer();
  • source {"bytes":10358752,"bitDepth":16,"colourType":2,"sharpDepth":"ushort","bitsPerSample":16}
  • default re-encode {"bytes":66111,"bitDepth":8,"colourType":2}
  • toColourspace srgb {"bytes":66111,"bitDepth":8,"colourType":2}
  • keep16 {"bytes":9605628,"bitDepth":16,"colourType":2}

sharp reads the file as 16-bit (depth: 'ushort') and still writes 8-bit. The docs say it plainly: "By default, PNG output is full colour at 8 bits per pixel," and for 16 bits "convert to rgb16 via toColourspace" (output options). The toColourspace entry shows the same rgb16 example (colour). I only found that paragraph after the file got smaller.

Note: the same default hides in other calls. .raw({ depth: 'ushort' }) on my 16-bit file gave green values with a maximum of 49 instead of 12,595, because the pipeline had already gone to 8-bit. Add .toColourspace('rgb16') before you read raw 16-bit data too.

What did the conversion cost?

  • 16-bit PNG source: 10,358,752 bytes
  • 16-bit re-encode with rgb16: 9,605,628 bytes
  • 8-bit PNG (default): 66,111 bytes
  • 8-bit palette PNG (palette: true, quality: 90): 142,786 bytes
  • JPEG q90 (mozjpeg): 16,188 bytes
  • WebP q90: 11,562 bytes
  • AVIF q60: 3,142 bytes

The 16-bit file is 157 times larger than the 8-bit one. The reason is the noise. My 2 levels of noise are real information at 16 bits and invisible at 8, so the 8-bit file compresses to almost nothing.

Counting levels in the green channel shows what was thrown away: distinct green 16-bit 11496 range 1080 12595 8-bit 46. The 8-bit file has 46 green levels for the whole sky. On the middle row, the longest run of identical green values is 143 pixels. That is the banding you see in dark skies and vignettes after an 8-bit save.

Is it always worth keeping 16 bits?

No. I also tested the 1920 × 1280 sample photo. It is an 8-bit JPEG, so widening it adds no information:

photo {"png16":{"bytes":4999854,"bitDepth":16,"colourType":2},"png8":{"bytes":3944916,"bitDepth":8,"colourType":2}}

The 16-bit copy is 4,999,854 bytes against 3,944,916, 26.7% larger, with no new information in it. Keep 16 bits when the source really has them: scans, RAW exports, HDR renders, grading work. For the web, convert to 8-bit on purpose, and dither or add grain first if the image has smooth gradients.

What should I do in a pipeline?

  • Read metadata().depth. ushort means a 16-bit source.
  • If you serve it on the web, the 8-bit default is fine, but log it so it is a decision.
  • If it is a master you keep, write it with toColourspace('rgb16') and test with ihdr(buf) that byte 24 is 16.
  • Never judge an 8-bit result by file size alone. 66,111 bytes looked like a success until I counted the levels.

More format background is in the PNG guide.

How I measured

  • Node v24.22.0, sharp 0.35.4 (libvips 8.18.6, libpng 1.6.58), Apple M1 Pro, 2026-10-18.
  • Inputs: the hand-written 1920 × 1280 sky above and jpg-sample-photo-original.jpg.
  • Single runs; encoding is deterministic. Levels counted over every pixel.
  • Raw numbers: measured.json in this folder.

What I did not test

Real 16-bit photographs. Grayscale and 16-bit RGBA PNGs. TIFF 16-bit output. Visual banding on a monitor; I counted levels, I did not view the file on a screen.

I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.