Writing documentation using
mermaid for version controlled diagrams that are turned into full documents via
pandoc and a mermaid filter. A lot of the time the complex mermaid diagrams end up with far too much space around the diagram, blasting out any PDF or Word output. But there's a simple fix.
magick mogrify -trim *.png
It uses imagemagick, a very common library for image manipulation. Specifically the
mogrify command, which edits the image in place, and uses
trim. Trim in this instance crops the image based on guessing the background colour is the same colour as the corner pixels. You can even do fuzzy matching, specific edges or hard-specify the background colour. That's just one of a quintillion options that imagemagick has to offer. Back pocketing this one.
Now with a quick filter script to do this for me
#!/usr/bin/env python3 import os """ Trim all mermaid images """ from pandocfilters import toJSONFilter, Emph, Para def behead(key, value, format, meta): pass if __name__ == "__main__": os.chdir('mermaid-images') os.system("magick mogrify -trim *.png") toJSONFilter(behead)