Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading fonts in Python/Pillow on a Mac

The Pillow ImageFont documentation has a few examples of using different fonts, but they don't work "out of the box" on a Mac. It's probably not too hard to figure out where my system fonts are located, but I'm lazy and would like a simple example that I can just edit.

So: how can I use Pillow on a Mac to write some text in a different font and size from the default?

like image 828
N. Virgo Avatar asked Jan 26 '23 14:01

N. Virgo


1 Answers

Ok, "Lazy One" ;-) Here is an example:

from PIL import Image, ImageFont, ImageDraw

# Make a red canvas and get a drawing context to it
img = Image.new(mode='RGB', size=(500, 200), color=(255,0,0))
draw = ImageDraw.Draw(img)

# Get a "Chalkduster" TTF
chalk = ImageFont.truetype("Chalkduster.ttf",16)

# Get a "Keyboard" TTF
kbd = ImageFont.truetype("Keyboard.ttf",26)

# Make example for Lazy One - showing how to colour cyan too
draw.text((40,40),"Keyboard",font=kbd, fill=(0,255,255))

# And another
draw.text((20,140),"Chalkduster",font=chalk)

img.save("test.png")

enter image description here

If you want a list of all the TTF fonts on your system, you can look in both:

  • /System/Library/Fonts, and
  • /Library/Fonts

And, if you are very lazy, you can check in both at the same time from Terminal:

find {/System,}/Library/Fonts -name \*ttf

Sample Output

/System/Library/Fonts/SFNSDisplay.ttf
/System/Library/Fonts/Symbol.ttf
/System/Library/Fonts/ZapfDingbats.ttf
/System/Library/Fonts/Apple Braille.ttf
/System/Library/Fonts/SFNSText.ttf
/System/Library/Fonts/Apple Braille Outline 6 Dot.ttf
/System/Library/Fonts/Apple Braille Pinpoint 6 Dot.ttf
/System/Library/Fonts/Apple Symbols.ttf
/System/Library/Fonts/SFNSTextItalic.ttf
/System/Library/Fonts/SFNSRounded.ttf
/System/Library/Fonts/Apple Braille Pinpoint 8 Dot.ttf
/System/Library/Fonts/Keyboard.ttf
/System/Library/Fonts/Apple Braille Outline 8 Dot.ttf
/Library/Fonts/Webdings.ttf
/Library/Fonts/Zapfino.ttf
/Library/Fonts/Trebuchet MS Italic.ttf
/Library/Fonts/Georgia.ttf
/Library/Fonts/Verdana Bold.ttf
/Library/Fonts/Bodoni 72 Smallcaps Book.ttf
/Library/Fonts/Times New Roman Bold Italic.ttf
/Library/Fonts/Silom.ttf
/Library/Fonts/Verdana Italic.ttf
/Library/Fonts/Times New Roman Italic.ttf
/Library/Fonts/Bradley Hand Bold.ttf
/Library/Fonts/Arial Narrow Italic.ttf
/Library/Fonts/AppleGothic.ttf
/Library/Fonts/DIN Condensed Bold.ttf
/Library/Fonts/Farisi.ttf
/Library/Fonts/Arial Bold.ttf
/Library/Fonts/Trebuchet MS.ttf
/Library/Fonts/Mishafi.ttf
/Library/Fonts/Trattatello.ttf
/Library/Fonts/BigCaslon.ttf
/Library/Fonts/Courier New Bold.ttf
/Library/Fonts/NISC18030.ttf
/Library/Fonts/Lao Sangam MN.ttf
/Library/Fonts/Luminari.ttf
/Library/Fonts/Times New Roman.ttf
/Library/Fonts/Brush Script.ttf
/Library/Fonts/Georgia Italic.ttf
/Library/Fonts/Courier New Italic.ttf
/Library/Fonts/Arial Unicode.ttf
/Library/Fonts/Chalkduster.ttf
/Library/Fonts/Apple Chancery.ttf
/Library/Fonts/AppleMyungjo.ttf
/Library/Fonts/Arial Narrow Bold Italic.ttf
/Library/Fonts/Arial Narrow.ttf
/Library/Fonts/Courier New.ttf
/Library/Fonts/Wingdings 3.ttf
/Library/Fonts/Wingdings 2.ttf
/Library/Fonts/Hoefler Text Ornaments.ttf
/Library/Fonts/Bodoni Ornaments.ttf
/Library/Fonts/Skia.ttf
/Library/Fonts/Trebuchet MS Bold Italic.ttf
/Library/Fonts/Impact.ttf
/Library/Fonts/Kokonor.ttf
/Library/Fonts/Tahoma Bold.ttf
/Library/Fonts/Arial.ttf
/Library/Fonts/Diwan Thuluth.ttf
/Library/Fonts/Ayuthaya.ttf
/Library/Fonts/Khmer Sangam MN.ttf
/Library/Fonts/Trebuchet MS Bold.ttf
/Library/Fonts/Arial Black.ttf
/Library/Fonts/Courier New Bold Italic.ttf
/Library/Fonts/Comic Sans MS.ttf
/Library/Fonts/DIN Alternate Bold.ttf
/Library/Fonts/Wingdings.ttf
/Library/Fonts/Sathu.ttf
/Library/Fonts/Arial Bold Italic.ttf
/Library/Fonts/Tahoma.ttf
/Library/Fonts/PlantagenetCherokee.ttf
/Library/Fonts/Georgia Bold.ttf
/Library/Fonts/Verdana Bold Italic.ttf
/Library/Fonts/Microsoft Sans Serif.ttf
/Library/Fonts/Georgia Bold Italic.ttf
/Library/Fonts/Arial Rounded Bold.ttf
/Library/Fonts/Times New Roman Bold.ttf
/Library/Fonts/Krungthep.ttf
/Library/Fonts/Gurmukhi.ttf
/Library/Fonts/Andale Mono.ttf
/Library/Fonts/Mishafi Gold.ttf
/Library/Fonts/Herculanum.ttf
/Library/Fonts/Comic Sans MS Bold.ttf
/Library/Fonts/Arial Italic.ttf
/Library/Fonts/Verdana.ttf
/Library/Fonts/Arial Narrow Bold.ttf
like image 140
Mark Setchell Avatar answered Feb 12 '23 11:02

Mark Setchell