File: ImageDraw.rst

package info (click to toggle)
pillow 12.0.0-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 72,636 kB
  • sloc: python: 49,518; ansic: 38,787; makefile: 302; sh: 168; javascript: 85
file content (748 lines) | stat: -rw-r--r-- 30,881 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
.. py:module:: PIL.ImageDraw
.. py:currentmodule:: PIL.ImageDraw

:py:mod:`~PIL.ImageDraw` module
===============================

The :py:mod:`~PIL.ImageDraw` module provides simple 2D graphics for
:py:class:`~PIL.Image.Image` objects.  You can use this module to create new
images, annotate or retouch existing images, and to generate graphics on the
fly for web use.

For a more advanced drawing library for PIL, see the `aggdraw module`_.

.. _aggdraw module: https://github.com/pytroll/aggdraw

Example: Draw a gray cross over an image
----------------------------------------

::

    import sys
    from PIL import Image, ImageDraw

    with Image.open("hopper.jpg") as im:

        draw = ImageDraw.Draw(im)
        draw.line((0, 0) + im.size, fill=128)
        draw.line((0, im.size[1], im.size[0], 0), fill=128)

        # write to stdout
        im.save(sys.stdout, "PNG")


Concepts
--------

Coordinates
^^^^^^^^^^^

The graphics interface uses the same coordinate system as PIL itself, with (0,
0) in the upper left corner. Any pixels drawn outside of the image bounds will
be discarded.

Colors
^^^^^^

To specify colors, you can use numbers or tuples just as you would use with
:py:meth:`PIL.Image.new`. See :ref:`colors` for more information.

For palette images (mode “P”), use integers as color indexes. In 1.1.4 and
later, you can also use RGB 3-tuples or color names (see below). The drawing
layer will automatically assign color indexes, as long as you don’t draw with
more than 256 colors.

Color names
^^^^^^^^^^^

See :ref:`color-names` for the color names supported by Pillow.

Alpha channel
^^^^^^^^^^^^^

By default, when drawing onto an existing image, the image's pixel values are simply
replaced by the new color::

    im = Image.new("RGBA", (1, 1), (255, 0, 0))
    d = ImageDraw.Draw(im)
    d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
    assert im.getpixel((0, 0)) == (0, 255, 0, 127)

    # Alpha channel values have no effect when drawing with RGB mode
    im = Image.new("RGB", (1, 1), (255, 0, 0))
    d = ImageDraw.Draw(im)
    d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
    assert im.getpixel((0, 0)) == (0, 255, 0)

If you would like to combine translucent color with an RGB image, then initialize the
ImageDraw instance with the RGBA mode::

    from PIL import Image, ImageDraw
    im = Image.new("RGB", (1, 1), (255, 0, 0))
    d = ImageDraw.Draw(im, "RGBA")
    d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
    assert im.getpixel((0, 0)) == (128, 127, 0)

If you would like to combine translucent color with an RGBA image underneath, you will
need to combine multiple images::

    from PIL import Image, ImageDraw
    im = Image.new("RGBA", (1, 1), (255, 0, 0, 255))
    im2 = Image.new("RGBA", (1, 1))
    d = ImageDraw.Draw(im2)
    d.rectangle((0, 0, 1, 1), (0, 255, 0, 127))
    im.paste(im2.convert("RGB"), mask=im2)
    assert im.getpixel((0, 0)) == (128, 127, 0, 255)

Fonts
^^^^^

PIL can use bitmap fonts or OpenType/TrueType fonts.

Bitmap fonts are stored in PIL's own format, where each font typically consists
of two files, one named .pil and the other usually named .pbm. The former
contains font metrics, the latter raster data.

To load a bitmap font, use the load functions in the :py:mod:`~PIL.ImageFont`
module.

To load a OpenType/TrueType font, use the truetype function in the
:py:mod:`~PIL.ImageFont` module. Note that this function depends on third-party
libraries, and may not available in all PIL builds.

Example: Draw partial opacity text
----------------------------------

::

    from PIL import Image, ImageDraw, ImageFont

    # get an image
    with Image.open("Pillow/Tests/images/hopper.png").convert("RGBA") as base:

        # make a blank image for the text, initialized to transparent text color
        txt = Image.new("RGBA", base.size, (255, 255, 255, 0))

        # get a font
        fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
        # get a drawing context
        d = ImageDraw.Draw(txt)

        # draw text, half opacity
        d.text((10, 10), "Hello", font=fnt, fill=(255, 255, 255, 128))
        # draw text, full opacity
        d.text((10, 60), "World", font=fnt, fill=(255, 255, 255, 255))

        out = Image.alpha_composite(base, txt)

        out.show()

Example: Draw multiline text
----------------------------

::

    from PIL import Image, ImageDraw, ImageFont

    # create an image
    out = Image.new("RGB", (150, 100), (255, 255, 255))

    # get a font
    fnt = ImageFont.truetype("Pillow/Tests/fonts/FreeMono.ttf", 40)
    # get a drawing context
    d = ImageDraw.Draw(out)

    # draw multiline text
    d.multiline_text((10, 10), "Hello\nWorld", font=fnt, fill=(0, 0, 0))

    out.show()


Functions
---------

.. py:method:: Draw(im, mode=None)

    Creates an object that can be used to draw in the given image.

    Note that the image will be modified in place.

    :param im: The image to draw in.
    :param mode: Optional mode to use for color values.  For RGB
        images, this argument can be RGB or RGBA (to blend the
        drawing into the image).  For all other modes, this argument
        must be the same as the image mode.  If omitted, the mode
        defaults to the mode of the image.

Attributes
----------

.. py:attribute:: ImageDraw.fill
    :type: bool
    :value: False

    Selects whether :py:attr:`ImageDraw.ink` should be used as a fill or outline color.

.. py:attribute:: ImageDraw.font

    The current default font.

    Can be set per instance::

        from PIL import ImageDraw, ImageFont
        draw = ImageDraw.Draw(image)
        draw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

    Or globally for all future ImageDraw instances::

        from PIL import ImageDraw, ImageFont
        ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")

.. py:attribute:: ImageDraw.fontmode

    The current font drawing mode.

    Set to ``"1"`` to disable antialiasing or ``"L"`` to enable it.

.. py:attribute:: ImageDraw.ink
    :type: int

    The internal representation of the current default color.

Methods
-------

.. py:method:: ImageDraw.getfont()

    Get the current default font, :py:attr:`ImageDraw.font`.

    If the current default font is ``None``,
    it is initialized with :py:func:`.ImageFont.load_default`.

    :returns: An image font.

.. py:method:: ImageDraw.arc(xy, start, end, fill=None, width=0)

    Draws an arc (a portion of a circle outline) between the start and end
    angles, inside the given bounding box.

    :param xy: Two points to define the bounding box. Sequence of ``[(x0, y0),
        (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and ``y1 >=
        y0``.
    :param start: Starting angle, in degrees. Angles are measured from 3
        o'clock, increasing clockwise.
    :param end: Ending angle, in degrees.
    :param fill: Color to use for the arc.
    :param width: The line width, in pixels.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.bitmap(xy, bitmap, fill=None)

    Draws a bitmap (mask) at the given position, using the current fill color
    for the non-zero portions. The bitmap should be a valid transparency mask
    (mode “1”) or matte (mode “L” or “RGBA”).

    This is equivalent to doing ``image.paste(xy, color, bitmap)``.

    To paste pixel data into an image, use the
    :py:meth:`~PIL.Image.Image.paste` method on the image itself.

.. py:method:: ImageDraw.chord(xy, start, end, fill=None, outline=None, width=1)

    Same as :py:meth:`~PIL.ImageDraw.ImageDraw.arc`, but connects the end points
    with a straight line.

    :param xy: Two points to define the bounding box. Sequence of ``[(x0, y0),
        (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and ``y1 >=
        y0``.
    :param outline: Color to use for the outline.
    :param fill: Color to use for the fill.
    :param width: The line width, in pixels.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.circle(xy, radius, fill=None, outline=None, width=1)

    Draws a circle with a given radius centering on a point.

    .. versionadded:: 10.4.0

    :param xy: The point for the center of the circle, e.g. ``(x, y)``.
    :param radius: Radius of the circle.
    :param outline: Color to use for the outline.
    :param fill: Color to use for the fill.
    :param width: The line width, in pixels.

.. py:method:: ImageDraw.ellipse(xy, fill=None, outline=None, width=1)

    Draws an ellipse inside the given bounding box.

    :param xy: Two points to define the bounding box. Sequence of either
        ``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0``
        and ``y1 >= y0``.
    :param outline: Color to use for the outline.
    :param fill: Color to use for the fill.
    :param width: The line width, in pixels.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.line(xy, fill=None, width=0, joint=None)

    Draws a line between the coordinates in the ``xy`` list.
    The coordinate pixels are included in the drawn line.

    :param xy: Sequence of either 2-tuples like ``[(x, y), (x, y), ...]`` or
               numeric values like ``[x, y, x, y, ...]``.
    :param fill: Color to use for the line.
    :param width: The line width, in pixels.

        .. versionadded:: 1.1.5

        .. note:: This option was broken until version 1.1.6.
    :param joint: Joint type between a sequence of lines. It can be ``"curve"``, for rounded edges, or :data:`None`.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.pieslice(xy, start, end, fill=None, outline=None, width=1)

    Same as arc, but also draws straight lines between the end points and the
    center of the bounding box.

    :param xy: Two points to define the bounding box. Sequence of ``[(x0, y0),
        (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and ``y1 >=
        y0``.
    :param start: Starting angle, in degrees. Angles are measured from 3
        o'clock, increasing clockwise.
    :param end: Ending angle, in degrees.
    :param fill: Color to use for the fill.
    :param outline: Color to use for the outline.
    :param width: The line width, in pixels.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.point(xy, fill=None)

    Draws points (individual pixels) at the given coordinates.

    :param xy: Sequence of either 2-tuples like ``[(x, y), (x, y), ...]`` or
               numeric values like ``[x, y, x, y, ...]``.
    :param fill: Color to use for the point.

.. py:method:: ImageDraw.polygon(xy, fill=None, outline=None, width=1)

    Draws a polygon.

    The polygon outline consists of straight lines between the given
    coordinates, plus a straight line between the last and the first
    coordinate. The coordinate pixels are included in the drawn polygon.

    :param xy: Sequence of either 2-tuples like ``[(x, y), (x, y), ...]`` or
               numeric values like ``[x, y, x, y, ...]``.
    :param fill: Color to use for the fill.
    :param outline: Color to use for the outline.
    :param width: The line width, in pixels.


.. py:method:: ImageDraw.regular_polygon(bounding_circle, n_sides, rotation=0, fill=None, outline=None, width=1)

    Draws a regular polygon inscribed in ``bounding_circle``,
    with ``n_sides``, and rotation of ``rotation`` degrees.

    :param bounding_circle: The bounding circle is a tuple defined
        by a point and radius.
        (e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``).
        The polygon is inscribed in this circle.
    :param n_sides: Number of sides
        (e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon).
    :param rotation: Apply an arbitrary rotation to the polygon
        (e.g. ``rotation=90``, applies a 90 degree rotation).
    :param fill: Color to use for the fill.
    :param outline: Color to use for the outline.
    :param width: The line width, in pixels.


.. py:method:: ImageDraw.rectangle(xy, fill=None, outline=None, width=1)

    Draws a rectangle.

    :param xy: Two points to define the bounding box. Sequence of either
            ``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and
            ``y1 >= y0``. The bounding box is inclusive of both endpoints.
    :param fill: Color to use for the fill.
    :param outline: Color to use for the outline.
    :param width: The line width, in pixels.

        .. versionadded:: 5.3.0

.. py:method:: ImageDraw.rounded_rectangle(xy, radius=0, fill=None, outline=None, width=1, corners=None)

    Draws a rounded rectangle.

    :param xy: Two points to define the bounding box. Sequence of either
            ``[(x0, y0), (x1, y1)]`` or ``[x0, y0, x1, y1]``, where ``x1 >= x0`` and
            ``y1 >= y0``. The bounding box is inclusive of both endpoints.
    :param radius: Radius of the corners.
    :param fill: Color to use for the fill.
    :param outline: Color to use for the outline.
    :param width: The line width, in pixels.
    :param corners: A tuple of whether to round each corner,
                    ``(top_left, top_right, bottom_right, bottom_left)``.
                    Keyword-only argument.

    .. versionadded:: 8.2.0

.. py:method:: ImageDraw.shape(shape, fill=None, outline=None)

    .. warning:: This method is experimental.

    Draw a shape.

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

    Draws the string at the given position.

    :param xy: The anchor coordinates of the text.
    :param text: String to be drawn. If it contains any newline characters,
                 the text is passed on to
                 :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_text`.
    :param fill: Color to use for the text.
    :param font: An :py:class:`~PIL.ImageFont.ImageFont` instance.
    :param anchor: The text anchor alignment. Determines the relative location of
                   the anchor to the text. The default alignment is top left,
                   specifically ``la`` for horizontal text and ``lt`` for
                   vertical text. See :ref:`text-anchors` for details.
                   This parameter is ignored for non-TrueType fonts.

                    .. note:: This parameter was present in earlier versions
                              of Pillow, but implemented only in version 8.0.0.

    :param spacing: If the text is passed on to
                    :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_text`,
                    the number of pixels between lines.
    :param align: If the text is passed on to
                  :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_text`,
                  ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines
                  the relative alignment of lines. Use the ``anchor`` parameter to
                  specify the alignment to ``xy``.

                  .. versionadded:: 11.2.1 ``"justify"``
    :param direction: Direction of the text. It can be ``"rtl"`` (right to
                      left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom).
                      Requires libraqm.

                      .. versionadded:: 4.2.0

    :param features: A list of OpenType font features to be used during text
                     layout. This is usually used to turn on optional
                     font features that are not enabled by default,
                     for example ``"dlig"`` or ``"ss01"``, but can be also
                     used to turn off default font features, for
                     example ``"-liga"`` to disable ligatures or ``"-kern"``
                     to disable kerning.  To get all supported
                     features, see `OpenType docs`_.
                     Requires libraqm.

                     .. versionadded:: 4.2.0

    :param language: Language of the text. Different languages may use
                     different glyph shapes or ligatures. This parameter tells
                     the font which language the text is in, and to apply the
                     correct substitutions as appropriate, if available.
                     It should be a `BCP 47 language code`_.
                     Requires libraqm.

                     .. versionadded:: 6.0.0

    :param stroke_width: The width of the text stroke.

                     .. versionadded:: 6.2.0

    :param stroke_fill: Color to use for the text stroke. If not given, will default to
        the ``fill`` parameter.

        .. versionadded:: 6.2.0

    :param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).

                    .. versionadded:: 8.0.0

    :param font_size: If ``font`` is not provided, then the size to use for the default
                      font.
                      Keyword-only argument.

                    .. versionadded:: 10.1.0


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

    Draws the string at the given position.

    :param xy: The anchor coordinates of the text.
    :param text: String to be drawn.
    :param fill: Color to use for the text.
    :param font: An :py:class:`~PIL.ImageFont.ImageFont` instance.

    :param anchor: The text anchor alignment. Determines the relative location of
                   the anchor to the text. The default alignment is top left,
                   specifically ``la`` for horizontal text and ``lt`` for
                   vertical text. See :ref:`text-anchors` for details.
                   This parameter is ignored for non-TrueType fonts.

                    .. note:: This parameter was present in earlier versions
                              of Pillow, but implemented only in version 8.0.0.

    :param spacing: The number of pixels between lines.
    :param align: ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines
                  the relative alignment of lines. Use the ``anchor`` parameter to
                  specify the alignment to ``xy``.

                  .. versionadded:: 11.2.1 ``"justify"``
    :param direction: Direction of the text. It can be ``"rtl"`` (right to
                      left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom).
                      Requires libraqm.

                      .. versionadded:: 4.2.0

    :param features: A list of OpenType font features to be used during text
                     layout. This is usually used to turn on optional
                     font features that are not enabled by default,
                     for example ``"dlig"`` or ``"ss01"``, but can be also
                     used to turn off default font features, for
                     example ``"-liga"`` to disable ligatures or ``"-kern"``
                     to disable kerning.  To get all supported
                     features, see `OpenType docs`_.
                     Requires libraqm.

                     .. versionadded:: 4.2.0

    :param language: Language of the text. Different languages may use
                     different glyph shapes or ligatures. This parameter tells
                     the font which language the text is in, and to apply the
                     correct substitutions as appropriate, if available.
                     It should be a `BCP 47 language code`_.
                     Requires libraqm.

                     .. versionadded:: 6.0.0

    :param stroke_width: The width of the text stroke.

                     .. versionadded:: 6.2.0

    :param stroke_fill: Color to use for the text stroke. If not given, will default to
                        the ``fill`` parameter.

                     .. versionadded:: 6.2.0

    :param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).

                     .. versionadded:: 8.0.0

    :param font_size: If ``font`` is not provided, then the size to use for the default
                      font.
                      Keyword-only argument.

                    .. versionadded:: 10.1.0

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

    Returns length (in pixels with 1/64 precision) of given text when rendered
    in font with provided direction, features, and language.

    This is the amount by which following text should be offset.
    Text bounding box may extend past the length in some fonts,
    e.g. when using italics or accents.

    The result is returned as a float; it is a whole number if using basic layout.

    Note that the sum of two lengths may not equal the length of a concatenated
    string due to kerning. If you need to adjust for kerning, include the following
    character and subtract its length.

    For example, instead of ::

        hello = draw.textlength("Hello", font)
        world = draw.textlength("World", font)
        hello_world = hello + world  # not adjusted for kerning
        assert hello_world == draw.textlength("HelloWorld", font)  # may fail

    use ::

        hello = draw.textlength("HelloW", font) - draw.textlength(
            "W", font
        )  # adjusted for kerning
        world = draw.textlength("World", font)
        hello_world = hello + world  # adjusted for kerning
        assert hello_world == draw.textlength("HelloWorld", font)  # True

    or disable kerning with (requires libraqm) ::

        hello = draw.textlength("Hello", font, features=["-kern"])
        world = draw.textlength("World", font, features=["-kern"])
        hello_world = hello + world  # kerning is disabled, no need to adjust
        assert hello_world == draw.textlength("HelloWorld", font, features=["-kern"])  # True

    .. seealso:: :py:meth:`PIL.ImageText.Text.get_length`

    .. versionadded:: 8.0.0

    :param text: Text to be measured. May not contain any newline characters.
    :param font: An :py:class:`~PIL.ImageFont.ImageFont` instance.
    :param direction: Direction of the text. It can be ``"rtl"`` (right to
                      left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom).
                      Requires libraqm.
    :param features: A list of OpenType font features to be used during text
                     layout. This is usually used to turn on optional
                     font features that are not enabled by default,
                     for example ``"dlig"`` or ``"ss01"``, but can be also
                     used to turn off default font features, for
                     example ``"-liga"`` to disable ligatures or ``"-kern"``
                     to disable kerning.  To get all supported
                     features, see `OpenType docs`_.
                     Requires libraqm.
    :param language: Language of the text. Different languages may use
                     different glyph shapes or ligatures. This parameter tells
                     the font which language the text is in, and to apply the
                     correct substitutions as appropriate, if available.
                     It should be a `BCP 47 language code`_.
                     Requires libraqm.
    :param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
    :param font_size: If ``font`` is not provided, then the size to use for the default
                      font.
                      Keyword-only argument.

                    .. versionadded:: 10.1.0

    :return: Either width for horizontal text, or 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, font_size=None)

    Returns bounding box (in pixels) of given text relative to given anchor
    when rendered in font with provided direction, features, and language.
    Only supported for TrueType fonts.

    Use :py:meth:`textlength` to get the offset of following text with
    1/64 pixel precision. The bounding box includes extra margins for
    some fonts, e.g. italics or accents.

    .. versionadded:: 8.0.0

    :param xy: The anchor coordinates of the text.
    :param text: Text to be measured. If it contains any newline characters,
                 the text is passed on to
                 :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_textbbox`.
    :param font: A :py:class:`~PIL.ImageFont.FreeTypeFont` instance.
    :param anchor: The text anchor alignment. Determines the relative location of
                   the anchor to the text. The default alignment is top left,
                   specifically ``la`` for horizontal text and ``lt`` for
                   vertical text. See :ref:`text-anchors` for details.
                   This parameter is ignored for non-TrueType fonts.
    :param spacing: If the text is passed on to
                    :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_textbbox`,
                    the number of pixels between lines.
    :param align: If the text is passed on to
                  :py:meth:`~PIL.ImageDraw.ImageDraw.multiline_textbbox`,
                  ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines
                  the relative alignment of lines. Use the ``anchor`` parameter to
                  specify the alignment to ``xy``.

                  .. versionadded:: 11.2.1 ``"justify"``
    :param direction: Direction of the text. It can be ``"rtl"`` (right to
                      left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom).
                      Requires libraqm.
    :param features: A list of OpenType font features to be used during text
                     layout. This is usually used to turn on optional
                     font features that are not enabled by default,
                     for example ``"dlig"`` or ``"ss01"``, but can be also
                     used to turn off default font features, for
                     example ``"-liga"`` to disable ligatures or ``"-kern"``
                     to disable kerning.  To get all supported
                     features, see `OpenType docs`_.
                     Requires libraqm.
    :param language: Language of the text. Different languages may use
                     different glyph shapes or ligatures. This parameter tells
                     the font which language the text is in, and to apply the
                     correct substitutions as appropriate, if available.
                     It should be a `BCP 47 language code`_.
                     Requires libraqm.
    :param stroke_width: The width of the text stroke.
    :param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
    :param font_size: If ``font`` is not provided, then the size to use for the default
                      font.
                      Keyword-only argument.

                    .. versionadded:: 10.1.0

    :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, font_size=None)

    Returns bounding box (in pixels) of given text relative to given anchor
    when rendered in font with provided direction, features, and language.
    Only supported for TrueType fonts.

    Use :py:meth:`textlength` to get the offset of following text with
    1/64 pixel precision. The bounding box includes extra margins for
    some fonts, e.g. italics or accents.

    .. seealso:: :py:meth:`PIL.ImageText.Text.get_bbox`

    .. versionadded:: 8.0.0

    :param xy: The anchor coordinates of the text.
    :param text: Text to be measured.
    :param font: A :py:class:`~PIL.ImageFont.FreeTypeFont` instance.
    :param anchor: The text anchor alignment. Determines the relative location of
                   the anchor to the text. The default alignment is top left,
                   specifically ``la`` for horizontal text and ``lt`` for
                   vertical text. See :ref:`text-anchors` for details.
                   This parameter is ignored for non-TrueType fonts.
    :param spacing: The number of pixels between lines.
    :param align: ``"left"``, ``"center"``, ``"right"`` or ``"justify"``. Determines
                  the relative alignment of lines. Use the ``anchor`` parameter to
                  specify the alignment to ``xy``.

                  .. versionadded:: 11.2.1 ``"justify"``
    :param direction: Direction of the text. It can be ``"rtl"`` (right to
                      left), ``"ltr"`` (left to right) or ``"ttb"`` (top to bottom).
                      Requires libraqm.
    :param features: A list of OpenType font features to be used during text
                     layout. This is usually used to turn on optional
                     font features that are not enabled by default,
                     for example ``"dlig"`` or ``"ss01"``, but can be also
                     used to turn off default font features, for
                     example ``"-liga"`` to disable ligatures or ``"-kern"``
                     to disable kerning.  To get all supported
                     features, see `OpenType docs`_.
                     Requires libraqm.
    :param language: Language of the text. Different languages may use
                     different glyph shapes or ligatures. This parameter tells
                     the font which language the text is in, and to apply the
                     correct substitutions as appropriate, if available.
                     It should be a `BCP 47 language code`_.
                     Requires libraqm.
    :param stroke_width: The width of the text stroke.
    :param embedded_color: Whether to use font embedded color glyphs (COLR, CBDT, SBIX).
    :param font_size: If ``font`` is not provided, then the size to use for the default
                      font.
                      Keyword-only argument.

                    .. versionadded:: 10.1.0

    :return: ``(left, top, right, bottom)`` bounding box

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

    .. warning:: This method is experimental.

    A more advanced 2D drawing interface for PIL images,
    based on the WCK interface.

    :param im: The image to draw in.
    :param hints: An optional list of hints.
    :returns: A (drawing context, drawing resource factory) tuple.

.. autofunction:: PIL.ImageDraw.floodfill

.. _BCP 47 language code: https://www.w3.org/International/articles/language-tags/
.. _OpenType docs: https://learn.microsoft.com/en-us/typography/opentype/spec/featurelist