Image / Scaling / Compression

Updated 28-Jun-2024

Size matters, and the smaller the better, when it comes to generation, modification, transmission, and storage of information. The vast amount of unoptimized documents and images on my very own local storage, much less what we send and receive all the time, is astounding. The idea that we need 100gb or 1tb of storage (thank you Dropbox, not) is sheer waste and sloth. I've addressed these issues a bit in the past, but it is time to take a bigger picture approach.

Note that this refers not only to images but essentially collections of images, namely pdf documents and video.


Related documents:


Past Articles on Compression

Maintaining Perceptible Quality

The key to the discussion is a focus on quality (relevance being its proxy in the engineering world). Quality is of course in the mind of the beholder, and so we look at whom that is. Generally we are talking about humans on computers and mobile devices, websites and native apps. For a more sophisticated audience we are talking about display and print formats. Yes, generally more pixels might be considered better, but we are dealing with human eyes. For the moment or decade we can put to the side the audience as not (yet) having machine eyes which have learned to see in some way.

Relatively Lossless Approaches

... MORE NEEDED HERE ... (Actual testing) ... Here are some resources to try...

DPI and PPI

DPI -- dots per inch -- and PPI -- pixels per inch (why not cm?) are meaningful only in relation to a given size (x by y inches), from which one can calculate the digital image size (number of pixels). This is from the world of print, though it now bleeds into digital display as well. Printers and digital platform vendors (e.g., Amazon, Apple, Google, Kobo, Nook) have specific DPI and image pixel size requirements based on what devices and formats they support.

A given image may have a DPI setting, but that is metadata only (which is sometimes ignored, even if present -- we're looking at you, Adobe). It is quite simple to change the DPI metadata of an image. There are drag and drop websites for this.

Some Quick Scripts

PNG Quant

PNGquant can save some space with the simple approach (more complicated available at the website).

find . -name '*.png' -exec pngquant --ext .png --force 256 {} \;

and

find . -name '*.PNG' -exec pngquant --ext .png --force 256 {} \;

FFMPEG

FFMPEG is a fantastic tool. Some basic conversion scripts (and for .mp4 files, changing to new.mp4 is one approach. Then delete the original file and rename the new file.

find -name "*.mp4" -exec bash -c 'ffmpeg -i "{}" "new.mp4"; rm "{}"; mv "new.mp4" "{}";' {} \;

Then

find -name "*.MP4" -exec bash -c 'ffmpeg -i "{}" "new.mp4"; rm "{}"; mv "new.mp4" "${0/.MP4}.mp4";' {} \;

Then

find -name "*.MOV" -exec bash -c 'ffmpeg -i "{}" "${0/.MOV}.mp4"; rm "{}"' {} \;

Then

find -name "*.mov" -exec bash -c 'ffmpeg -i "{}" "${0/.mov}.mp4"; rm "{}"' {} \;

JPEG Optim

Jpeg optim has a built in optimizer, which is a bit lightweight, for the wary.

find . -name '*.jpg' -exec jpegoptim {} \;

And

find . -name '*.JPG' -exec jpegoptim {} \;

GhostScript for PDF Compression

This does the job but is a bit slow. Ghostscript.com appears to be offline, so use the cache. This sets the quality to "display" of about 150ppi.

find -type f -name "*.pdf" -exec bash -c 'gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile="new.pdf" "{}"; rm "{}"; mv "new.pdf" "{}";' {} \;