python字体大小设置苹果电脑_在Mac上的Python /枕头中加载字体

在Mac上使用Pillow库时,需要额外步骤来加载系统字体。本文提供了一个示例,展示如何加载并使用"Chalkduster"和"Keyboard"字体,并调整它们的大小。要查看所有系统字体,可以检查`/System/Library/Fonts`和`/Library/Fonts`目录。此外,可以通过Terminal命令查找所有ttf文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

解决方案

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")

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值