Presented at ForwardJS Summit July 2017.
Squeezing PNG & JPEGThe Lazy Person's Guidehttps://unsplash.com/photos/srd-rZ5fcFw
View Slide
ObjectiveDeliver a two-dimensional array of colorsin the smallest possible PNG/JPEG filewithout any noticeable loss in visual quality
OptimalFile sizeVisual qualityIdealUnacceptable
010110010101 010110010101010100 10010101100101010101001001 010110010101010100100101011001010101010010010 101EncodingDecoding
010110010101 010110010101010100 10010101100101010101001001 010110010101010100100101011001010101010010010 101010110010101 010110010101010100 10010101100101010101001001 010110010101010100100101011001010101010010010 101LosslessLossyNot identicalvs the original
.jpg8 megapixelsJPEG Quality 952,210 KBMozJPEGQ = 84879 KB2 seconds.jpgGuetzli5 minutes877 KB
https://unsplash.com/photos/Zh6C3GydNBASqueezing PNG
Portable Network GraphicsLosslessencodingTruecolorRGBRGB+AIndexedColorGrayscaleRFC 2083http://www.w3.org/TR/PNG/Good compression (using DEFLATE)8 bits/channel
DEFLATE = Lempel-Ziv + HuffmanRFC 1951
Lempel-Ziv: De-duplication'Cause the players gonna play, play, play, play, play'Cause the players gonna play, play, play, play, play'Cause the players gonna[5:14], play, play, play, play'Cause the players gonna[5:14], [6:6]play, play, play
Color Deduplication
PNG Color Channels
PNG Compression Efficiencyhttps://encode.ru/threads/1725-pngthermal-pseudo-thermal-view-of-PNG-compression-efficiency
Duplication Search Trade-offs'Cause the players gonna play, play, play, play, play'Cause the players gonna[5:14], [6:6] [6:6] [6:6] [6:6]'Cause the players gonna[5:14], [6:6] [12:12] [6:6]
Zopfli: Exhaustive DEFLATEhttps://ariya.io/2016/06/using-zopfli-to-optimize-png-imagesgithub.com/google/zopfli> zopflipng tiger.png tiger-small.pngOptimizing tiger.pngInput size: 197220 (192K)Result size: 135325 (132K). Percentage oforiginal: 68.616%Result is smaller
0 50 100 150 200OriginalZopfliKBPNG File Size
Quantization of Colorshttps://ariya.io/2016/07/shrinking-png-images-with-quantization9734 distinct colors only 256 colors
9734 distinct colors
only 256 colors
pngquant.org> pngquant --speed 1 --verbose tiger.pngtiger.png:read 193KB filemade histogram...9734 colors foundselecting colors...2%selecting colors...4%selecting colors...17%selecting colors...29%selecting colors...31%selecting colors...44%selecting colors...57%selecting colors...70%selecting colors...82%selecting colors...95%selecting colors...100%moving colormap towards local minimumeliminated opaque tRNS-chunk entries...59 entries transparentmapped image to new colors...MSE=1.048 (Q=96)writing 256-color image as tiger-fs8.pngQuantized 1 image.
0 50 100 150Full colorsReduced colorsKBPNG File Size
https://unsplash.com/photos/5bdKZLqeySUSqueezing JPEG
JPEG Color Space: YCbCrLuminance Chroma
Block Splitting: 8 × 8 Pixels
Spatial → Frequency Domain40 24 15 19 28 24 19 1538 34 35 35 31 28 27 2940 47 49 40 33 29 32 4342 49 50 39 34 30 32 4640 47 46 35 31 32 35 4338 43 42 31 27 27 28 3339 33 25 17 14 15 19 2629 16 6 1 -4 0 7 18239 32 27 -12 3 -5 3 134 -3 -19 6 3 0 -1 1-70 2 8 23 9 6 -1 -15 0 -6 11 -2 0 -1 1-17 -3 6 6 3 -1 0 02 4 2 2 1 -2 0 1-3 0 0 -1 -1 -1 0 01 -1 3 1 0 0 0 0Discrete Cosine Transform (DCT)
Effects of different Quality FactorQ = 60 Q = 20 Q = 5
Quantization of DCT Coefficients92 3 -9 -7 3 -1 0 2-39 -58 12 17 -2 2 4 2-84 62 1 -18 3 4 -5 5-52 -36 -10 14 -10 4 -2 0-86 -40 49 -7 17 -6 -2 5-62 65 -12 -2 3 -8 -2 0-17 14 -36 17 -11 3 3 -1-54 32 -9 -9 22 0 1 390 0 -7 0 0 0 0 0-35 -56 9 11 0 0 0 0-84 54 0 -13 0 0 0 0-45 -33 -0 0 0 0 0 0-77 -39 45 0 0 0 0 0-52 60 0 0 0 0 0 0-15 0 -19 0 0 0 0 0-51 19 0 0 0 0 0 0Affected by the Q factorhttps://cs.stanford.edu/people/eroberts/courses/soco/projects/data-compression/lossy/jpeg/coeff.htm
Reduce Quality, Lose Detailsgithub.com/mozilla/mozjpeg> cjpeg –quality 84 original.jpg > output.jpg2,210 KB → 879 KB8 megapixelsOriginal Q=95
Alternative JPEG Encoder: Guetzli> guetzli –-quality 84 original.jpg output.jpggithub.com/google/guetzli2,210 KB → 877 KB8 megapixelsOriginal Q=95
Pyschovisual Similaritygithub.com/google/butterauglihttps://medium.com/@duhroach/reducing-jpg-file-size-e5b27df3257cMozJPEGButteraugli score: 2.3GuetzliButteraugli score: 1.9
Blurring the BackgroundPhoto by Kaizen Nguyễn on https://unsplash.com/photos/jcLcWL8D7AQ116 KB 105 KB
https://unsplash.com/photos/FCrgmqqvl-wSummary
Identifytrade-offsReducedetailsEncodeexhaustively
File sizeVisual quality
Further Exploration
Thank Youshapesecurity.comSome artworks are from https://unsplash.com/ and http://openclipart.org.speakerdeck.com/ariya@ariyahidayat