Automating basic image edits with ImageMagick

A cheatsheet of ImageMagick commands I use to edit vector drawings from my iPad for inclusion in (Quarto markdown) documents, presentations and webpages

Cynthia Huang


February 4, 2024


June 20, 2024

The Task

I’m a very visual thinker, and I often create diagrams, concept maps and “graphical abstracts”1 to think through. Depending on what’s available I might sketch my ideas with pen and paper, on a whiteboard, or using my iPad. With a bit of polish and editing (often using my iPad), I can also use my drawings to communicate my research ideas – i.e. including them in presentations or even journal papers. However, getting my drawings into my documents has always been a bit of a clunky process that goes something like:

  1. Make and polish the drawing. On iPad, I generally use Notability for quick sketches, and Concepts for polishing illustrations.
  2. Export or Scan drawing to image file. Ideally, I’d like to scan/export to png files with a sensible naming convention, but sometimes only pdf is possible (e.g. when I’ve got multiple pages of sketches).
  3. Edit the image file for inclusion. This includes extracting pdf pages as separate png files, cropping, making the background transparent.

That last step is by far the most tedious. Luckily for me, tedium usually means there’s an opportunity for automation! This post documents a few ImageMagick command line tricks for wrangling image files I use to reduce the friction between creating drawings (fun) and plugging them into my Quarto documents via markdown image links2.

Install ImageMagick

TL;DR: ImageMagick is cool tool that you can use via the command line. Here’s a description from the official website

ImageMagick® is a free, open-source software suite, used for editing and manipulating digital images. It can be used to create, edit, compose, or convert bitmap images, and supports a wide range of file formats, including JPEG, PNG, GIF, TIFF, and PDF.

If you are on macOS, you can install imagemagick using homebrew:

brew install imagemagick

The Magick Commands

The next two sections give you commands to take two drawing files, diagram_panel-map-sandwich-maker.png and sketch_tidydata.pdf and generate cropped, transparent background images, all of which can be found in the source directory for this blog post (i.e. on GitHub):

├── images
│   ├*─ diagram_panel-map-sandwich-maker.png
│   ├── diagram_panel-map-sandwich-maker_trim-trans.png
│   ├── diagram_panel-map-sandwich-maker_trimmed.png
│   ├*─ sketch_tidydata.pdf
│   ├── sketch_tidydata-00.png
│   ├── sketch_tidydata-01.png
│   ├── transp_sketch_tidydata-00.png
│   └── transp_sketch_tidydata-01.png
└── index.qmd

Single page vector drawings (png)

Let’s start with a single page export diagram_panel-map-sandwich-maker.png from Notability:

Notice that there’s a lot of whitespace underneath my sketch because the page size is fixed (usually to A4 or letter depending on your in-app settings). Let’s get rid of the extra whitespace (using -trim) and turn background transparent (using -transparent):

zsh: ~/.../images
convert ${filename}.png -trim -transparent white ${filename}_trim-trans.png

This gives us a tightly cropped image with a transparent background (which is only apparent when I make the webpage background red):

Multi-page sketches (pdf)

Sometimes before you can trim your pages, you might need to extract them from a file like sketch_tidydata.pdf:

Here’s the command to extract each page as a separate png file:

zsh: ~/.../images
convert -quality 100 -density 200 ${filename}.pdf ${filename}-%02d.png

We include the two resulting files here using the R code chunk trick for generating inline image links that I wrote about in this previous blog post:

The two images were already trimmed, but what if we wanted to make the background transparent like this:

We could use a loop to achieve this:

zsh: ~/.../images
for filepath in $(ls sketch*.png);
  convert ${filename}.png -fuzz 20% -transparent white transp_${filename}.png;

Notes & Extensions

Single vector page

  • The -transparent flag turns any pixels that match the argument colour (e.g. white) and makes them transparent. If your background colour is not white you can replace the argument with the hexcode of the colour you want to make transparent.3
  • If you want padding around your image, you can always add it back using the relevant options for your document output format (e.g. CSS or LaTex figure options)

Multi-page pdf

  • The -fuzz flag specifies a distance around your target colour within which colours are considered equal. This is handy for scanned sketches that have a “white” background that only slightly differs from the target colour given to the -transparent flag.4
  • If you search “how to split up a pdf using ImageMagick”, you might come across commands that include the -flatten flag. Don’t include this flag when using newer versions ImageMagick since it will just flatten all the pages onto into a single output image.5


  1. According to Wikipedia, graphical abstracts are graphics used to summarise major results or key ideas of a paper – i.e. a visual equivalent of a written abstract.↩︎

  2. This is an inline image link: ![](cool-image.png)↩︎

  3. See:↩︎

  4. See↩︎

  5. See↩︎


BibTeX citation:
  author = {Huang, Cynthia},
  title = {Automating Basic Image Edits with {ImageMagick}},
  date = {2024-02-04},
  url = {},
  langid = {en}
For attribution, please cite this work as:
Huang, Cynthia. 2024. “Automating Basic Image Edits with ImageMagick.” February 4, 2024.