Editing images using code

Painting with a hammer, blindfolded

October 16, 2018 — November 18, 2024

computers are awful
generative art
making things
photon choreography

The command line is not the right tool for images. Even I to admit that. OTOH the command line is a better tool to automate things than via a GUI, sometimes even images.

1 vips

vips is my current go-to. I think it is under-rated.

libvips is a demand-driven, horizontally threaded image processing library. Compared to similar libraries, libvips runs quickly and uses little memory. libvips is licensed under the LGPL 2.1+.

It has around 300 operations covering arithmetic, histograms, convolution, morphological operations, frequency filtering, colour, resampling, statistics and others. It supports a large range of numeric formats, from 8-bit int to 128-bit complex. Images can have any number of bands. It supports a good range of image formats, including JPEG, TIFF, PNG, WebP, FITS, Matlab, OpenEXR, PDF, SVG, HDR, PPM, CSV, GIF, Analyze, NIfTI, DeepZoom, and OpenSlide. It can also load images via ImageMagick or GraphicsMagick, letting it load formats like DICOM.

It comes with bindings for C, C++, and the command-line. Full bindings are available for Ruby, Python, PHP, .NET, Go, and Lua. libvips is used as an image processing engine by sharp (on node.js), bimg, sharp for Go, Ruby on Rails, carrierwave-vips, mediawiki, PhotoFlow and others. The official libvips GUI is nip2, a strange combination of a spreadsheet and a photo editor.

It seems to optimally pipeline operations and stream them from disk, which is much better for typical use cases. Documentation for vips is not amazing, but it is bloody good software. There is a CLI interface, which is under-advertised but very useful. Occasionally you need to work out what that does from API documentation, but I’ve guessed right every time so far.

Also notable vips supported the fancy new AVIF format before its competitors, who were still arguing about it long after every browser supported it. That is why vips is part of the build-chain of this very site, generating AVIF thumbnails.

2 graphicsmagic/imagemagick

graphicsmagick and imagemagick are similar but hate each other like competitive siblings. Imagemagick is the “original”, whatever that might mean in the tangled phylogeny of software intellectual property. graphicsmagick is the younger sibling who claims that its more famous and successful elder sold out. FWIW Graphicsmagick seems to have a better API, but most users probably do not care. They are reasonably interchangeable from this user’s perspective. I think imagemagics command names are grandiose and graphicsmagick’s are more sensible, FWIW. From a developer perspective, IM pretends GM does not exist, whereas GM knows IM exists and thinks it is awful:

Here are some reasons to prefer GraphicsMagick over ImageMagick or other popular software:

  • GM is more efficient than ImageMagick so it gets the job done faster using fewer resources.
  • GM is much smaller and lighter than ImageMagick (3-5X smaller installation footprint).
  • GM is used to process billions of files at the world’s largest photo sites (e.g. Flickr and Etsy).
  • GM does not conflict with other installed software.
  • GM suffers from fewer security issues and exploits than ImageMagick.

2.1 imagemagick hacks

Fred Weinhaus’s imagemagick scripts show how to do a great many useful things, e.g. Histogram matching.

Beware the idiosyncratic licensing.

2.2 favicon

Old school:

convert favicon.png  -bordercolor white -border 0 \
      $ -clone 0 -resize 16x16 $ \
      $ -clone 0 -resize 32x32 $ \
      $ -clone 0 -resize 48x48 $ \
      $ -clone 0 -resize 64x64 $ \
      -delete 0 -alpha off -colors 256 favicon.ico

Recent:

convert favicon.png -define icon:auto-resize=64,48,32,16 favicon.ico

3 G’MIC

G'MIC includes a world of image conversion including scriptable image processing pipelines. See, e.g., a hand-rolled fingerpainting filter via diffusion tenor fields. Also has GIMP and Krita plug-in versions.

G'MIC is a full-featured open-source framework for digital image processing It provides several user interfaces to convert / manipulate / filter / visualize generic image datasets, ranging from 1D scalar signals to 3D+t sequences of multi-spectral volumetric images, hence including 2D colour images.

Doing animated GIFs? they are fiddly. Specialised tool gifsicle handles them.