Converting text to image in Python

Converting text to image in Python

Date
Feb 10, 2020
Tags
Programming
If you are into image processing, you may find yourself occassionally want to convert text with specific font type to image in Python. Most of the libraries out there rely on ImageMagick (https://imagemagick.org/index.php) to be installed on your computer. This article is written to guide you how to do that in Mac.
First, you can install that using homebrew in your Mac. If you haven’t got your homebrew installed, please go to https://brew.sh and follow the instructions there.
Once you have homebrew installed on your mac, run the following command to install that.
brew install imagemagick
Then run the following convert command to check the list of font types found in your computer.
convert -list font
If you can see a list of font types then that’s good, congratulations. But if you are like me, see an empty list, please continue to follow this article. What you should do next is to make use of the script here, http://www.imagemagick.org/Usage/scripts/imagick_type_gen . This script will generate a XML file that list out the fonts available in your system for ImageMagick to use. Please run the following command.
# Make a new directory for ImageMagick local settings and cd into it mkdir ~/.magick cd ~/.magick  # Grab script to find all fonts on system and store them in a config file curl http://www.imagemagick.org/Usage/scripts/imagick_type_gen > type_gen  # Run script, telling it where my fonts are and create "type.xml" file with list  find /System/Library/Fonts /Library/Fonts ~/Library/Fonts -name "*.[to]tf" | perl type_gen -f - > type.xml  # Go to ImageMagick config folder - see note at end to find correct folder cd /usr/local/Cellar/imagemagick/6.8.9-1/etc/ImageMagick-6  # Edit system config file called "type.xml" and add line near end to tell IM to look at local file we made in earlier step                          ### THIS LINE ADDED ###
Finally, for where to put your resultant type.xml file, you can put that in any directory that ImageMagick will look at. To get the list of directories, please check below.
convert -debug configure -list font 2>&1 | grep -E "Searching|Loading" Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.5-0/share/ImageMagick-6/type.xml" Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.5-0/lib/ImageMagick//config-Q16/type.xml" Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.5-0/etc/ImageMagick-6/type.xml" Searching for configure file: "/usr/local/Cellar/imagemagick/6.9.5-0/share/doc/ImageMagick-6/type.xml" Searching for configure file: "/Users/mark/.config/ImageMagick/type.xml" Searching for configure file: "/Users/mark/.magick/type.xml" Loading type configure file "/usr/local/Cellar/imagemagick/6.9.5-0/etc/ImageMagick-6/type.xml" ... Loading type configure file "/usr/local/Cellar/imagemagick/6.9.5-0/etc/ImageMagick-6/type-apple.xml" ... Loading type configure file "/usr/local/Cellar/imagemagick/6.9.5-0/etc/ImageMagick-6/type-ghostscript.xml" ... Loading type configure file "/Users/mark/.magick/type.xml" ...