Skip to content

Commit 350d7f2

Browse files
authored
Merge pull request #7806 from radarhere/imagefont
2 parents 1b6e68e + cc094ca commit 350d7f2

File tree

3 files changed

+72
-8
lines changed

3 files changed

+72
-8
lines changed

docs/deprecations.rst

+36-4
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,10 @@ Previous code::
232232

233233
im = Image.new("RGB", (100, 100))
234234
draw = ImageDraw.Draw(im)
235-
width, height = draw.textsize("Hello world")
235+
width, height = draw.textsize("Hello world", font)
236236

237237
width, height = font.getsize_multiline("Hello\nworld")
238-
width, height = draw.multiline_textsize("Hello\nworld")
238+
width, height = draw.multiline_textsize("Hello\nworld", font)
239239

240240
Use instead::
241241

@@ -247,11 +247,43 @@ Use instead::
247247

248248
im = Image.new("RGB", (100, 100))
249249
draw = ImageDraw.Draw(im)
250-
width = draw.textlength("Hello world")
250+
width = draw.textlength("Hello world", font)
251251

252-
left, top, right, bottom = draw.multiline_textbbox((0, 0), "Hello\nworld")
252+
left, top, right, bottom = draw.multiline_textbbox((0, 0), "Hello\nworld", font)
253253
width, height = right - left, bottom - top
254254

255+
Previously, the ``size`` methods returned a ``height`` that included the vertical
256+
offset of the text, while the new ``bbox`` methods distinguish this as a ``top``
257+
offset.
258+
259+
.. image:: ./example/size_vs_bbox.png
260+
: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.
261+
:align: center
262+
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+
255287
FreeTypeFont.getmask2 fill parameter
256288
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
257289

docs/example/size_vs_bbox.png

12.6 KB
Loading

docs/releasenotes/9.2.0.rst

+36-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,10 @@ Previous code::
6969

7070
im = Image.new("RGB", (100, 100))
7171
draw = ImageDraw.Draw(im)
72-
width, height = draw.textsize("Hello world")
72+
width, height = draw.textsize("Hello world", font)
7373

7474
width, height = font.getsize_multiline("Hello\nworld")
75-
width, height = draw.multiline_textsize("Hello\nworld")
75+
width, height = draw.multiline_textsize("Hello\nworld", font)
7676

7777
Use instead::
7878

@@ -84,11 +84,43 @@ Use instead::
8484

8585
im = Image.new("RGB", (100, 100))
8686
draw = ImageDraw.Draw(im)
87-
width = draw.textlength("Hello world")
87+
width = draw.textlength("Hello world", font)
8888

89-
left, top, right, bottom = draw.multiline_textbbox((0, 0), "Hello\nworld")
89+
left, top, right, bottom = draw.multiline_textbbox((0, 0), "Hello\nworld", font)
9090
width, height = right - left, bottom - top
9191

92+
Previously, the ``size`` methods returned a ``height`` that included the vertical
93+
offset of the text, while the new ``bbox`` methods distinguish this as a ``top``
94+
offset.
95+
96+
.. image:: ../example/size_vs_bbox.png
97+
: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.
98+
:align: center
99+
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+
92124
API Additions
93125
=============
94126

0 commit comments

Comments
 (0)