Resizing of images for Anki with Hazel and ImageMagick

I use Anki to study foreign language vocabulary. It’s the de facto spaced repetition software for memorization.[1] When making flashcards for language learnings, I try to use imagery as much as possible. So a card may have a Russian word on one side and just an image on the opposite side. (Since I already know the English word that the image represents, why not try to engage a different part of the brain to help with memorization?)

If you use Anki on multiple devices, then synchronization is a key step. However, image size becomes a limiting factor for sync speed. Since only a small image is often necessary to convey the intended meaning, we can improve the sync efficiency by using them while not sacrificing any meaning. Bulk, efficient resizing of images for Anki cards is an important part of the process for me.

Here I’ll describe a process of automatically processing images for use on Anki cards using Hazel and ImageMagick. Sorry, PC and Linux users, this is OS X only.

Hazel

Hazel is an indispensable tool for OS X automation. It’s a little hard to describe all of the things that it can do, but suffice it to say that it is a background application that watches folders and then performs rules on the contents of those folders. You will need to buy it for this process to work.

ImageMagick

ImageMagick is a well-known Swiss army knife of image process. You will need to install it which you can do using Homebrew or this installer

The process

Image handling directories

I created two directories on the Desktop: ankibound and ankidone. Incoming images go into ankibound. The rule we create in Hazel will watch ankibound, convert the image to a smaller size, adjust the quality slightly, strip the unused metadata and move the processed file to ankidone.

Hazel rule to process incoming images

Hazel rule list

If you are not familiar with creating Hazel rules, you first add the folder to be watched (ankibound) and add a new rules which we’ve cleverly named “Resize images for Anki.” Then we just need to add the criteria and the steps for the rule.

Hazel criteria and actions

We’ve specified the type of file to be processed inside of ankibound and added two actions:

  • resize incoming images using ImageMagick on the command line, and
  • move the processed images to ankidone.

The code for image scaling is simple, it’s just:

1
2
/usr/local/bin/convert "$1" -adaptive-resize 150x150 -quality 80\
-density 72 -strip "$1"

Now, any image that you save to ankibound will get resized to 150px in its largest dimension and moved to ankidone, ready to import into your Anki cards. Of course, you could also use the ImageResizer add-on for Anki but I like being in control of my own process and being able to deal with images without having to get them onto the clipboard. Either way works.

References


  1. Yes, I'm aware that others exist. I've tried many but always have come back to Anki.