ImageDraw2.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # WCK-style drawing interface operations
  6. #
  7. # History:
  8. # 2003-12-07 fl created
  9. # 2005-05-15 fl updated; added to PIL as ImageDraw2
  10. # 2005-05-15 fl added text support
  11. # 2005-05-20 fl added arc/chord/pieslice support
  12. #
  13. # Copyright (c) 2003-2005 by Secret Labs AB
  14. # Copyright (c) 2003-2005 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. """
  19. (Experimental) WCK-style drawing interface operations
  20. .. seealso:: :py:mod:`PIL.ImageDraw`
  21. """
  22. from __future__ import annotations
  23. from typing import BinaryIO
  24. from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
  25. from ._typing import StrOrBytesPath
  26. class Pen:
  27. """Stores an outline color and width."""
  28. def __init__(self, color: str, width: int = 1, opacity: int = 255) -> None:
  29. self.color = ImageColor.getrgb(color)
  30. self.width = width
  31. class Brush:
  32. """Stores a fill color"""
  33. def __init__(self, color: str, opacity: int = 255) -> None:
  34. self.color = ImageColor.getrgb(color)
  35. class Font:
  36. """Stores a TrueType font and color"""
  37. def __init__(
  38. self, color: str, file: StrOrBytesPath | BinaryIO, size: float = 12
  39. ) -> None:
  40. # FIXME: add support for bitmap fonts
  41. self.color = ImageColor.getrgb(color)
  42. self.font = ImageFont.truetype(file, size)
  43. class Draw:
  44. """
  45. (Experimental) WCK-style drawing interface
  46. """
  47. def __init__(
  48. self,
  49. image: Image.Image | str,
  50. size: tuple[int, int] | list[int] | None = None,
  51. color: float | tuple[float, ...] | str | None = None,
  52. ) -> None:
  53. if isinstance(image, str):
  54. if size is None:
  55. msg = "If image argument is mode string, size must be a list or tuple"
  56. raise ValueError(msg)
  57. image = Image.new(image, size, color)
  58. self.draw = ImageDraw.Draw(image)
  59. self.image = image
  60. self.transform = None
  61. def flush(self) -> Image.Image:
  62. return self.image
  63. def render(self, op, xy, pen, brush=None):
  64. # handle color arguments
  65. outline = fill = None
  66. width = 1
  67. if isinstance(pen, Pen):
  68. outline = pen.color
  69. width = pen.width
  70. elif isinstance(brush, Pen):
  71. outline = brush.color
  72. width = brush.width
  73. if isinstance(brush, Brush):
  74. fill = brush.color
  75. elif isinstance(pen, Brush):
  76. fill = pen.color
  77. # handle transformation
  78. if self.transform:
  79. xy = ImagePath.Path(xy)
  80. xy.transform(self.transform)
  81. # render the item
  82. if op == "line":
  83. self.draw.line(xy, fill=outline, width=width)
  84. else:
  85. getattr(self.draw, op)(xy, fill=fill, outline=outline)
  86. def settransform(self, offset):
  87. """Sets a transformation offset."""
  88. (xoffset, yoffset) = offset
  89. self.transform = (1, 0, xoffset, 0, 1, yoffset)
  90. def arc(self, xy, start, end, *options):
  91. """
  92. Draws an arc (a portion of a circle outline) between the start and end
  93. angles, inside the given bounding box.
  94. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc`
  95. """
  96. self.render("arc", xy, start, end, *options)
  97. def chord(self, xy, start, end, *options):
  98. """
  99. Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points
  100. with a straight line.
  101. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord`
  102. """
  103. self.render("chord", xy, start, end, *options)
  104. def ellipse(self, xy, *options):
  105. """
  106. Draws an ellipse inside the given bounding box.
  107. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse`
  108. """
  109. self.render("ellipse", xy, *options)
  110. def line(self, xy, *options):
  111. """
  112. Draws a line between the coordinates in the ``xy`` list.
  113. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line`
  114. """
  115. self.render("line", xy, *options)
  116. def pieslice(self, xy, start, end, *options):
  117. """
  118. Same as arc, but also draws straight lines between the end points and the
  119. center of the bounding box.
  120. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice`
  121. """
  122. self.render("pieslice", xy, start, end, *options)
  123. def polygon(self, xy, *options):
  124. """
  125. Draws a polygon.
  126. The polygon outline consists of straight lines between the given
  127. coordinates, plus a straight line between the last and the first
  128. coordinate.
  129. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon`
  130. """
  131. self.render("polygon", xy, *options)
  132. def rectangle(self, xy, *options):
  133. """
  134. Draws a rectangle.
  135. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle`
  136. """
  137. self.render("rectangle", xy, *options)
  138. def text(self, xy, text, font):
  139. """
  140. Draws the string at the given position.
  141. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text`
  142. """
  143. if self.transform:
  144. xy = ImagePath.Path(xy)
  145. xy.transform(self.transform)
  146. self.draw.text(xy, text, font=font.font, fill=font.color)
  147. def textbbox(self, xy, text, font):
  148. """
  149. Returns bounding box (in pixels) of given text.
  150. :return: ``(left, top, right, bottom)`` bounding box
  151. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`
  152. """
  153. if self.transform:
  154. xy = ImagePath.Path(xy)
  155. xy.transform(self.transform)
  156. return self.draw.textbbox(xy, text, font=font.font)
  157. def textlength(self, text, font):
  158. """
  159. Returns length (in pixels) of given text.
  160. This is the amount by which following text should be offset.
  161. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`
  162. """
  163. return self.draw.textlength(text, font=font.font)