I need to crop white space (margins, etc) from diagrams where the rendering pipeline doesn't generate (reliable) bounding boxes. The margins are reliably white so automated detection is in principle trivial however the only existing tool I've managed to find is the venerable pnmcrop (http://netpbm.sourceforge.net/doc/pnmcrop.html) from the early 1990s which uses the obscure netpbm image formats (https://en.wikipedia.org/wiki/Netpbm_format).
The netpbm tools do include PNG format converters so this triad works:
pngtopnm original.png >original.ppm
pnmcrop original.ppm >cropped.ppm
pnmtopng cropped.ppm >cropped.png
but it's a bit clunky (and with diagrams the intermediate PPM files can inflate over 100-fold: original.png is 165kB, original.ppm is 224MB).
Surely there's a way to coax a modern tool like imagemagick to find and crop borders?
EDIT: netpbm tools support stdin. So as a nicer one-liner without the temp files:
pngtopnm <original.png | pnmcrop | pnmtopng >cropped.png
with appropriate tool substitutions (jpgtopnm, giftopnm, etc - choose from all 71 possibilities!) it's still a bit clumsy.