Slide 1

Slide 1 text

David Newton, St. Michael’s Hospital 1 1 Using ImageMagick to resize your Images Toronto Web Performance Group, 2015-03-17 Twitter: @newtron Github: @nwtn Email: david@davidnewton.ca Slides + image credits: https://github.com/nwtn/pres-imagemagick-webperfto

Slide 2

Slide 2 text

2

Slide 3

Slide 3 text

3 Responsive images are a pain in the butt

Slide 4

Slide 4 text

4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

7 ImageMagick, y’all

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9 $ brew install imagemagick

Slide 10

Slide 10 text

10 Condition File size: mean File size: % difference Photoshop CC, with optimization 260,304.70 KB ImageMagick `-resize` 337,665.98 KB 52.39%

Slide 11

Slide 11 text

11 How does image resizing work?

Slide 12

Slide 12 text

12 685,936 pixels 189,739 pixels

Slide 13

Slide 13 text

13 …to 64 pixels? How do we get from 16 pixels…

Slide 14

Slide 14 text

14 != resampling

Slide 15

Slide 15 text

15 …to 64 pixels? How do we get from 16 pixels…

Slide 16

Slide 16 text

16 …to 64 pixels? How do we get from 16 pixels… Background interpolation

Slide 17

Slide 17 text

17 …to 64 pixels? How do we get from 16 pixels… Nearest-neighbour interpolation

Slide 18

Slide 18 text

18 Nearest-neighbour interpolation

Slide 19

Slide 19 text

19 Bicubic interpolation

Slide 20

Slide 20 text

20 Nearest-neighbour vs. bicubic interpolation

Slide 21

Slide 21 text

21 7 colours 1,146 colours

Slide 22

Slide 22 text

22

Slide 23

Slide 23 text

More colours = More bytes* 23

Slide 24

Slide 24 text

24 https://github.com/nwtn/image-resize-tests/

Slide 25

Slide 25 text

25 How do we know which options are best?

Slide 26

Slide 26 text

Quality objective: measured with DSSIM 26

Slide 27

Slide 27 text

27 Control Test Difference

Slide 28

Slide 28 text

28

Slide 29

Slide 29 text

Quality subjective: from me 29

Slide 30

Slide 30 text

Test a variety of images 30

Slide 31

Slide 31 text

31 The best* settings for ImageMagick

Slide 32

Slide 32 text

32 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH With optimization:

Slide 33

Slide 33 text

33 https://imageoptim.com/

Slide 34

Slide 34 text

34 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH Without optimization:

Slide 35

Slide 35 text

35 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 36

Slide 36 text

36 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 37

Slide 37 text

37 Adaptive resize Distort resize Geometry Interpolative resize Liquid rescale Resize Sample Scale Thumbnail

Slide 38

Slide 38 text

38 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 39

Slide 39 text

39 (x1 ,y2 ) (x2 ,y2 ) (x1 ,y1 ) (x2 ,y1 ) (x,y) = + + +

Slide 40

Slide 40 text

40 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 41

Slide 41 text

41

Slide 42

Slide 42 text

42 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH

Slide 43

Slide 43 text

43 Fancy

Slide 44

Slide 44 text

43 Fancy

Slide 45

Slide 45 text

44 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 46

Slide 46 text

45 -unsharp 0.25x0.08+8.3+0.045 radius sigma gain threshold

Slide 47

Slide 47 text

46 -unsharp 0.25x0.08+8.3+0.045 radius sigma gain threshold

Slide 48

Slide 48 text

47 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 49

Slide 49 text

48

Slide 50

Slide 50 text

49

Slide 51

Slide 51 text

50

Slide 52

Slide 52 text

51

Slide 53

Slide 53 text

52 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH

Slide 54

Slide 54 text

53

Slide 55

Slide 55 text

More colours = More bytes* 54

Slide 56

Slide 56 text

55

Slide 57

Slide 57 text

56 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 58

Slide 58 text

57 quality 10 quality 40 quality 70 quality 100

Slide 59

Slide 59 text

58 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH

Slide 60

Slide 60 text

59 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 61

Slide 61 text

60 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH

Slide 62

Slide 62 text

61

Slide 63

Slide 63 text

62 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH

Slide 64

Slide 64 text

63

Slide 65

Slide 65 text

63

Slide 66

Slide 66 text

64

Slide 67

Slide 67 text

64

Slide 68

Slide 68 text

65 Progressive JPEGs Good? Evil?

Slide 69

Slide 69 text

66 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH

Slide 70

Slide 70 text

67

Slide 71

Slide 71 text

68 mogrify -path OUTPUT_PATH -filter Triangle -define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.08+8.3+0.045 -dither None - posterize 136 -quality 82 -define png:preserve-colormap=true - interlace none INPUT_PATH With optimization:

Slide 72

Slide 72 text

69 mogrify -path OUTPUT_PATH -filter Triangle - define filter:support=2.0 -thumbnail OUTPUT_WIDTH -unsharp 0.25x0.25+8.00+0.065 - dither None -posterize 136 -background Black - alpha Background -quality 82 -define jpeg:fancy- upsampling=off -define png:compression-filter=5 -define png:compression-level=9 -define png:compression-strategy=1 -define png:exclude- chunk=all -interlace none -colorspace sRGB - strip INPUT_PATH Without optimization:

Slide 73

Slide 73 text

70 Why even bother?

Slide 74

Slide 74 text

71 Condition File size: mean File size: % difference My settings, optimization 221,583.36 KB My settings, no optimization 260,250.18 KB 17.45% Photoshop CC, optimization 260,304.70 KB 17.48% CodeIgniter / ExpressionEngine 276,112.95 KB 24.61% Photoshop CC, no optimization 299,709.70 KB 35.26% TYPO3.CMS 321,602.95 KB 45.14% WordPress 337,665.98 KB 52.39% Drupal 344,246.42 KB 55.36% Perch 348,735.82 KB 57.38% Craft CMS 372,055.63 KB 67.91% grunt-responsive-images 426,623.28 KB 92.36%

Slide 75

Slide 75 text

72 OK… but it’s still a pain in the butt

Slide 76

Slide 76 text

73 https://www.npmjs.com/package/grunt-respimg

Slide 77

Slide 77 text

74 Resizing with smart defaults SVG to PNG rasterization Optimization

Slide 78

Slide 78 text

75 npm install grunt-respimg --save-dev

Slide 79

Slide 79 text

76

Slide 80

Slide 80 text

77

Slide 81

Slide 81 text

David Newton, St. Michael’s Hospital 78 1 Using ImageMagick to resize your Images Toronto Web Performance Group, 2015-03-17 Twitter: @newtron Github: @nwtn Email: david@davidnewton.ca Slides + image credits: https://github.com/nwtn/pres-imagemagick-webperfto