Skip to content

Commit 7490aee

Browse files
committed
Show how to use anchors to align text in imagefont deprecations
1 parent 55d0289 commit 7490aee

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

docs/deprecations.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ offset.
260260
:alt: Demonstration of size height vs bbox top and bottom
261261
:align: center
262262

263+
If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
264+
which avoid issues that can occur with non-English text or unusual fonts.
265+
For example, instead of the following code::
266+
267+
from PIL import Image, ImageDraw, ImageFont
268+
269+
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
270+
271+
im = Image.new("RGB", (100, 100))
272+
draw = ImageDraw.Draw(im)
273+
width, height = draw.textsize("Hello world", font)
274+
x, y = (100 - width) / 2, (100 - height) / 2
275+
draw.text((x, y), "Hello world", font=font)
276+
277+
Use instead::
278+
279+
from PIL import Image, ImageDraw, ImageFont
280+
281+
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
282+
283+
im = Image.new("RGB", (100, 100))
284+
draw = ImageDraw.Draw(im)
285+
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")
286+
263287
FreeTypeFont.getmask2 fill parameter
264288
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
265289

docs/releasenotes/9.2.0.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ offset.
9797
:alt: Demonstration of size height vs bbox top and bottom
9898
:align: center
9999

100+
If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
101+
which avoid issues that can occur with non-English text or unusual fonts.
102+
For example, instead of the following code::
103+
104+
from PIL import Image, ImageDraw, ImageFont
105+
106+
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
107+
108+
im = Image.new("RGB", (100, 100))
109+
draw = ImageDraw.Draw(im)
110+
width, height = draw.textsize("Hello world", font)
111+
x, y = (100 - width) / 2, (100 - height) / 2
112+
draw.text((x, y), "Hello world", font=font)
113+
114+
Use instead::
115+
116+
from PIL import Image, ImageDraw, ImageFont
117+
118+
font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
119+
120+
im = Image.new("RGB", (100, 100))
121+
draw = ImageDraw.Draw(im)
122+
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")
123+
100124
API Additions
101125
=============
102126

0 commit comments

Comments
 (0)