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

Up your Assets: Batch image scripting with Imag...

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →

Up your Assets: Batch image scripting with ImageMagick

Images on the web need to look sharp on displays with very high DPI, while still small enough to download fast. When you have many assets, it becomes cumbersome to manually resize and export all of them.

With the new iPhone 6+, the need for "retina" alternative images is even bigger. Now you might even want to supply both a "@2x" image and a "@3x" image.

From my examples, you will be able to solve some common problems using ImageMagick's tools `convert`, `mogrify` and `identify`.

Avatar for Björn Söderqvist

Björn Söderqvist

September 18, 2014

Other Decks in Programming

Transcript

  1. Retina Add a folder “WxH@2x” Resize into twice the size

    Let frontend detect whether to use retina images
  2. “3x retina” For iPhone 6+ and other devices with high

    density Resize into three times the size
  3. Sharpening Tip: Resize first, sharpen later Visual comparison: Sharpening methods

    Note: Perceptual (subjective) matter Note: Sharpness demands vary
  4. VERY DIFFERENT SIZE RESULTS METHOD PROS CONS HEIGHT IS KNOWN

    (CSS) BY MAX HEIGHT MOGRIFY -RESIZE 999X160
  5. EXTREMELY HARD TO KNOW DIMENSIONS METHOD PROS CONS AVATARS VERY

    EVEN IN SIZE BY TOTAL NUMBER OF PIXELS MOGRIFY -RESIZE @20000
  6. NOT PERFECT DIMENSION-WISE METHOD PROS CONS EASY TO FIT (CSS)

    BY DIMENSIONS CONSTRAINT MOGRIFY -RESIZE 120X160
  7. Identify: HTML identify -format “FORMAT” <p>
 <span>%w x %h</span>
 <img

    src=“%f”>
 </p> • Generate a size report for an export
  8. Identify: JSON identify -format “FORMAT” {
 “filename": "%f",
 “width": "%w",


    “height": "%h"
 }, • Which image is widest?
 images.sort(function(a, b){
 return a.width - b.width;
 });
 • Which image has most pixels?
 images.sort(function(a, b) {
 return a.width * a.height -
 b.width * b.height;
 });
  9. VISUAL ARTEFACTS WITH LARGER FILES CODEC METHOD CONS REDUCES PALETTE

    8-BIT PNG LOSSLESS ZLIB COMPRESSION original 256 128 64 32
  10. VISUAL ARTEFACTS WITH LARGER FILES CODEC METHOD CONS REDUCES PALETTE

    8-BIT PNG LOSSLESS ZLIB COMPRESSION 118K 38K 31K 26K 21K
  11. LOW CROSS BROWSER SUPPORT CODEC METHOD CONS SEVERAL WEBP LOSSY

    AND LOSSLESS, WITH ALPHA CHANNEL 118K 18K