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…

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. ihdr[8] = 16; ihdr[9] = 2; // 16 bits per sample, colour type 2 (RGB) 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? source default re-encode toColourspace srgb keep16 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.

Source disclosure

This is a source-linked briefing. Read the original report from Dev Community .