Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Lossy PNG for true-color images (Velocity Conf ...

Kornel Lesiński
November 18, 2014

Lossy PNG for true-color images (Velocity Conf EU 2014)

Kornel Lesiński

November 18, 2014
Tweet

More Decks by Kornel Lesiński

Other Decks in Design

Transcript

  1. PNG PNG JPEG Lossless Lossy PNG Opaque Alpha Channel WebP

    JPEG 2000 JPEG XR JPEG XT if you need lossy and alpha, it becomes complicated. You may need to serve 4 different files for every image!
  2. 00000000 00000001 00000010 00000011 00000100 00000101 00000110 00000111 00001000 00001001

    00001010 0 10 110 1110 11110 111110 1111110 11111110 111111110 1111111110 11111111110 → → → → → → → → → → → Instead of storing 8 bits per byte, gzip uses variable-length sequences. Most popular bytes are given shortest sequences.
  3. 0 1 2 3 4 5 6 7 8 9

    0 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 = = Instead of storing uncompressible absolute values PNG can store difference between neighboring pixels
  4. 5 6 4 6 5 5 6 4 5 4

    5 + 1 -2 +2 - 1 0 + 1 -2 + 1 - 1 = = 5 0 0 0 0 0 0 0 0 0 0 Noisy images won’t compress well… unless you fake the data and remove the noise
  5. Pixels Math gzip Lies Lossy PNG trick is about replacing

    math with lies. Only the encoder changes, so it’s 100% compatible.
  6. ? When PNG “guesses” what the next pixel will be,

    it can make the guess based on average of 2 neighbors, which in lossy PNG causes blur
  7. • Works everywhere—fully backwards-compatible • No color limit—supports 24 and

    32-bit PNG • Preserves edges—per-pixel quality control • Very fast—small tweak in the encoder • Streamable—only needs 2 lines of the image
  8. pngquant -Q 75-100 --skip-if-larger \
 -o out.png in.png || posterize

    -b -Q 75 in.jpg out.jpg pngquant is often more efficient, but can’t guarantee quality. You can combine the two for best results.