Skip to content

Commit

Permalink
Merge pull request #7806 from radarhere/imagefont
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 11, 2024
2 parents 1b6e68e + cc094ca commit 350d7f2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
40 changes: 36 additions & 4 deletions docs/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ Previous code::

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world")
width, height = draw.textsize("Hello world", font)

width, height = font.getsize_multiline("Hello\nworld")
width, height = draw.multiline_textsize("Hello\nworld")
width, height = draw.multiline_textsize("Hello\nworld", font)

Use instead::

Expand All @@ -247,11 +247,43 @@ Use instead::

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width = draw.textlength("Hello world")
width = draw.textlength("Hello world", font)

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

Previously, the ``size`` methods returned a ``height`` that included the vertical
offset of the text, while the new ``bbox`` methods distinguish this as a ``top``
offset.

.. image:: ./example/size_vs_bbox.png
: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.
:align: center

If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
which avoid issues that can occur with non-English text or unusual fonts.
For example, instead of the following code::

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world", font)
x, y = (100 - width) / 2, (100 - height) / 2
draw.text((x, y), "Hello world", font=font)

Use instead::

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")

FreeTypeFont.getmask2 fill parameter
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down
Binary file added docs/example/size_vs_bbox.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 36 additions & 4 deletions docs/releasenotes/9.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ Previous code::

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world")
width, height = draw.textsize("Hello world", font)

width, height = font.getsize_multiline("Hello\nworld")
width, height = draw.multiline_textsize("Hello\nworld")
width, height = draw.multiline_textsize("Hello\nworld", font)

Use instead::

Expand All @@ -84,11 +84,43 @@ Use instead::

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width = draw.textlength("Hello world")
width = draw.textlength("Hello world", font)

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

Previously, the ``size`` methods returned a ``height`` that included the vertical
offset of the text, while the new ``bbox`` methods distinguish this as a ``top``
offset.

.. image:: ../example/size_vs_bbox.png
: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.
:align: center

If you are using these methods for aligning text, consider using :ref:`text-anchors` instead
which avoid issues that can occur with non-English text or unusual fonts.
For example, instead of the following code::

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
width, height = draw.textsize("Hello world", font)
x, y = (100 - width) / 2, (100 - height) / 2
draw.text((x, y), "Hello world", font=font)

Use instead::

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

im = Image.new("RGB", (100, 100))
draw = ImageDraw.Draw(im)
draw.text((100 / 2, 100 / 2), "Hello world", font=font, anchor="mm")

API Additions
=============

Expand Down

0 comments on commit 350d7f2

Please sign in to comment.