Skip to content

Commit 87ecd01

Browse files
authored
Merge pull request #6484 from radarhere/imagedraw_font
2 parents 5a087cc + 520fa19 commit 87ecd01

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

Tests/test_imagedraw.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,23 @@ def test_stroke_multiline():
13141314
assert_image_similar_tofile(im, "Tests/images/imagedraw_stroke_multiline.png", 3.3)
13151315

13161316

1317+
def test_setting_default_font():
1318+
# Arrange
1319+
im = Image.new("RGB", (100, 250))
1320+
draw = ImageDraw.Draw(im)
1321+
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf", 120)
1322+
1323+
# Act
1324+
ImageDraw.ImageDraw.font = font
1325+
1326+
# Assert
1327+
try:
1328+
assert draw.getfont() == font
1329+
finally:
1330+
ImageDraw.ImageDraw.font = None
1331+
assert isinstance(draw.getfont(), ImageFont.ImageFont)
1332+
1333+
13171334
def test_same_color_outline():
13181335
# Prepare shape
13191336
x0, y0 = 5, 5

docs/reference/ImageDraw.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ Fonts
6464

6565
PIL can use bitmap fonts or OpenType/TrueType fonts.
6666

67-
Bitmap fonts are stored in PILs own format, where each font typically consists
67+
Bitmap fonts are stored in PIL's own format, where each font typically consists
6868
of two files, one named .pil and the other usually named .pbm. The former
6969
contains font metrics, the latter raster data.
7070

@@ -146,6 +146,11 @@ Methods
146146
147147
Get the current default font.
148148

149+
To set the default font for all future ImageDraw instances::
150+
151+
from PIL import ImageDraw, ImageFont
152+
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
153+
149154
:returns: An image font.
150155

151156
.. py:method:: ImageDraw.arc(xy, start, end, fill=None, width=0)

docs/releasenotes/9.3.0.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ TODO
2626
API Additions
2727
=============
2828

29+
Allow default ImageDraw font to be set
30+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
31+
32+
Rather than specifying a font when calling text-related ImageDraw methods, or
33+
setting a font on each ImageDraw instance, the default font can now be set for
34+
all future ImageDraw operations::
35+
36+
from PIL import ImageDraw, ImageFont
37+
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
38+
2939
Saving multiple MPO frames
3040
^^^^^^^^^^^^^^^^^^^^^^^^^^
3141

src/PIL/ImageDraw.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646

4747

4848
class ImageDraw:
49+
font = None
50+
4951
def __init__(self, im, mode=None):
5052
"""
5153
Create a drawing instance.
@@ -86,12 +88,16 @@ def __init__(self, im, mode=None):
8688
else:
8789
self.fontmode = "L" # aliasing is okay for other modes
8890
self.fill = 0
89-
self.font = None
9091

9192
def getfont(self):
9293
"""
9394
Get the current default font.
9495
96+
To set the default font for all future ImageDraw instances::
97+
98+
from PIL import ImageDraw, ImageFont
99+
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
100+
95101
:returns: An image font."""
96102
if not self.font:
97103
# FIXME: should add a font repository

0 commit comments

Comments
 (0)