Skip to content

Commit 83433d8

Browse files
authored
Merge pull request #23 from nulano/imagefont
Show how to use anchors to align text in ImageFont deprecations
2 parents 56a02b7 + 7490aee commit 83433d8

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

docs/deprecations.rst

+24
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,30 @@ offset.
260260
:alt: In bbox methods, top measures the vertical distance above the text, while bottom measures that plus the vertical distance of the text itself. In size methods, height also measures the vertical distance above the text plus the vertical distance of the text itself.
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

+24
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,30 @@ offset.
9797
:alt: In bbox methods, top measures the vertical distance above the text, while bottom measures that plus the vertical distance of the text itself. In size methods, height also measures the vertical distance above the text plus the vertical distance of the text itself.
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)