Adding a New Font to ImageMagick

Ayman H
4 min readMay 25, 2021

--

For the more visually-inclined users, here’s a link to a YouTube video I created for the exact same topic.

ImageMagick is a free and open-source cross-platform software suite for displaying, creating, converting, modifying, and editing raster images. You can think of it as an image processing and management tool.

Some Python packages use ImageMagick behind the scenes to accomplish image-related tasks; one such package is MoviePy. I’m a fan of MoviePy, and I’ve used it in a good number of projects in the past; in some of these projects, I needed to overlay text on top of images and videos.

The list of fonts that come with ImageMagick is rather limited; and if you’re anything like me (very picky about fonts), then you would probably want to install your own font, which I’m going to show you how to do in this blog post.

How can I display the list of available fonts?

Assuming that you have ImageMagick installed, you can use the Terminal to display the list of available ImageMagick fonts. You can do that by running the following command:

identify -list font

This command should display all the available fonts. If you’d like to filter that list (instead of scrolling through it) to search for a specific font, you can do the following:

identify -list font | grep -i 'fontname'

Here’s an example:

As can be seen in the screenshot above, I have 7 different fonts that contain the word ‘helvetica’ (the -i option in the grep command was for case-insensitive search).

Locate the file that contains the list of all fonts

The first step for adding a font to the list of existing fonts, is to locate a file with the name “type.xml” in one of the ImageMagick folders. In my case, I used Homebrew to download and install ImageMagick, and I was able to find “type.xml” in the following path:

/opt/homebrew/Cellar/imagemagick/7.0.11–13_1/etc/ImageMagick-7

Inside “type.xml”, there’s a line of code that “includes” another file with the name “type-ghostscript.xml”:

<typemap>
<include file="type-ghostscript.xml" />
</typemap>

The file “type-ghostscript.xml” should be in the same directory, and it should contain the list of all fonts that can be used in ImageMagick. Each font has its own type tag, along with some attributes. Here’s a sample screenshot of “type-ghostscript.xml”:

If you’d like to add a font to ImageMagick, you’ll need to add a new type tag with that font to the list of tags in “type-ghostscript.xml”; read on to see how we can do that.

Adding a new font to “type-ghostscript.xml”

Let’s start by identifying the path of the font we’d like to add. If the font is already installed on your macOS, you can open Font Book, then show the font in Finder:

Right click on the font, then click on “Show in Finder”

Tip: In Finder, if you right click and hold Alt (or Option), you can copy the path directly, as shown below.

Hold the Option key to “Copy as Path” (very handy tip)

Now that we have everything that we need, let’s add the following type tag to the “type-ghostscript.xml”:

<type format="ttf" name="CMU Normal" glyphs="/Users/aymanhajja/Library/Fonts/cmunrm.ttf"/>

Of course, make sure that you replace the glyphs path with your own path.

Now, if we try to list all the fonts that contain CMU, we’ll notice that the font has been added to the list:

Here’s an image generated with MoviePy using the new font that I just added:

FAQ

1. How can I see all the available fonts in ImageMagick?

As I showed earlier, one way to do that would be to do the following:

identify -list font

Another way would be to use the command convert:

convert -list font

2. What are some of the things we can do with ImageMagick?

I haven’t spent a lot of time poking at ImageMagick directly, but I used Python packages that make use of ImageMagick; that said, some of the things that ImageMagick can do include resizing images, padding images, image layering/composition, image annotating, and image comparing. Here’s a link that shows examples of some of the things that can be done using ImageMagick.

3. Can I create animations using ImageMagick?

Yes, you can create animations using ImageMagick and your Terminal; however, it will probably be much easier to create animations using another (more high-level) package that might use ImageMagick behind the scenes.

--

--

Ayman H
Ayman H

Written by Ayman H

Assistant Professor at the College of Charleston.

No responses yet