Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved documentation of ImageDraw return values #6556

Merged
merged 6 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions docs/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,40 @@ Deprecated Use
:py:meth:`.ImageDraw2.Draw.textsize` :py:meth:`.ImageDraw2.Draw.textbbox` and :py:meth:`.ImageDraw2.Draw.textlength`
=========================================================================== =============================================================================================================

Previous code:

.. code-block:: python

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
width, height = font.getsize("Hello world")
left, top = font.getoffset("Hello world")

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

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

Use instead:

.. code-block:: python

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
left, top, right, bottom = font.getbbox("Hello world")
width, height = right - left, bottom - top

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

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

Removed features
----------------

Expand Down
13 changes: 13 additions & 0 deletions docs/reference/ImageDraw.rst
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ Methods

.. deprecated:: 9.2.0

See https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods
for more information.
hugovk marked this conversation as resolved.
Show resolved Hide resolved
hugovk marked this conversation as resolved.
Show resolved Hide resolved

Use :py:meth:`textlength()` to measure the offset of following text with
1/64 pixel precision.
Use :py:meth:`textbbox()` to get the exact bounding box based on an anchor.
Expand Down Expand Up @@ -489,10 +492,15 @@ Methods

.. versionadded:: 6.2.0

:return: (width, height)

.. py:method:: ImageDraw.multiline_textsize(text, font=None, spacing=4, direction=None, features=None, language=None, stroke_width=0)

.. deprecated:: 9.2.0

See https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#font-size-and-offset-methods
for more information.
hugovk marked this conversation as resolved.
Show resolved Hide resolved

Use :py:meth:`.multiline_textbbox` instead.

Return the size of the given string, in pixels.
Expand Down Expand Up @@ -541,6 +549,8 @@ Methods

.. versionadded:: 6.2.0

:return: (width, height)

.. py:method:: ImageDraw.textlength(text, font=None, direction=None, features=None, language=None, embedded_color=False)

Returns length (in pixels with 1/64 precision) of given text when rendered
Expand Down Expand Up @@ -608,6 +618,7 @@ Methods
It should be a `BCP 47 language code`_.
Requires libraqm.
:param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
:return: Width for horizontal, height for vertical text.

.. py:method:: ImageDraw.textbbox(xy, text, font=None, anchor=None, spacing=4, align="left", direction=None, features=None, language=None, stroke_width=0, embedded_color=False)

Expand Down Expand Up @@ -657,6 +668,7 @@ Methods
Requires libraqm.
:param stroke_width: The width of the text stroke.
:param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
:return: ``(left, top, right, bottom)`` bounding box

.. py:method:: ImageDraw.multiline_textbbox(xy, text, font=None, anchor=None, spacing=4, align="left", direction=None, features=None, language=None, stroke_width=0, embedded_color=False)

Expand Down Expand Up @@ -700,6 +712,7 @@ Methods
Requires libraqm.
:param stroke_width: The width of the text stroke.
:param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
:return: ``(left, top, right, bottom)`` bounding box

.. py:method:: getdraw(im=None, hints=None)

Expand Down
34 changes: 34 additions & 0 deletions docs/releasenotes/9.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,40 @@ Deprecated Use
:py:meth:`.ImageDraw2.Draw.textsize` :py:meth:`.ImageDraw2.Draw.textbbox` and :py:meth:`.ImageDraw2.Draw.textlength`
=========================================================================== =============================================================================================================

Previous code:

.. code-block:: python

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
width, height = font.getsize("Hello world")
left, top = font.getoffset("Hello world")

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

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

Use instead:

.. code-block:: python

from PIL import Image, ImageDraw, ImageFont

font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
left, top, right, bottom = font.getbbox("Hello world")
width, height = right - left, bottom - top

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

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

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

Expand Down
15 changes: 15 additions & 0 deletions src/PIL/ImageFont.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ def getsize(self, text, *args, **kwargs):
"""
.. deprecated:: 9.2.0

For more information, see
https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#deprecations.
hugovk marked this conversation as resolved.
Show resolved Hide resolved

Use :py:meth:`.getbbox` or :py:meth:`.getlength` instead.

Returns width and height (in pixels) of given text.
Expand Down Expand Up @@ -428,6 +431,9 @@ def getsize(
"""
.. deprecated:: 9.2.0

For more information, see
https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#deprecations.

Use :py:meth:`getlength()` to measure the offset of following text with
1/64 pixel precision.
Use :py:meth:`getbbox()` to get the exact bounding box based on an anchor.
Expand Down Expand Up @@ -498,6 +504,9 @@ def getsize_multiline(
"""
.. deprecated:: 9.2.0

For more information, see
https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#deprecations.

Use :py:meth:`.ImageDraw.multiline_textbbox` instead.

Returns width and height (in pixels) of given text if rendered in font
Expand Down Expand Up @@ -557,6 +566,9 @@ def getoffset(self, text):
"""
.. deprecated:: 9.2.0

For more information, see
https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#deprecations.

Use :py:meth:`.getbbox` instead.

Returns the offset of given text. This is the gap between the
Expand Down Expand Up @@ -851,6 +863,9 @@ def getsize(self, text, *args, **kwargs):
"""
.. deprecated:: 9.2.0

For more information, see
https://pillow.readthedocs.io/en/stable/releasenotes/9.2.0.html#deprecations.

Use :py:meth:`.getbbox` or :py:meth:`.getlength` instead.
"""
deprecate("getsize", 10, "getbbox or getlength")
Expand Down