Editing images from code
Woodcarving with a hammer, blindfolded
October 16, 2018 — November 18, 2024
The command line is not the right tool for images. Even I need to admit that. OTOH it is way easier to script things from the command line than via a GUI and sometimes even images work.
1 vips
vips
:
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.
Moreover, it seems to optimally pipeline operations and stream them from disk, which is much better for typical use cases. Documentation for libs 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 it does from API documentation, but I’ve guessed right every time so far.
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 a user perspective. 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
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.