Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ImageDraw2.py 5.9KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. import warnings
  23. from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
  24. from ._deprecate import deprecate
  25. class Pen:
  26. """Stores an outline color and width."""
  27. def __init__(self, color, width=1, opacity=255):
  28. self.color = ImageColor.getrgb(color)
  29. self.width = width
  30. class Brush:
  31. """Stores a fill color"""
  32. def __init__(self, color, opacity=255):
  33. self.color = ImageColor.getrgb(color)
  34. class Font:
  35. """Stores a TrueType font and color"""
  36. def __init__(self, color, file, size=12):
  37. # FIXME: add support for bitmap fonts
  38. self.color = ImageColor.getrgb(color)
  39. self.font = ImageFont.truetype(file, size)
  40. class Draw:
  41. """
  42. (Experimental) WCK-style drawing interface
  43. """
  44. def __init__(self, image, size=None, color=None):
  45. if not hasattr(image, "im"):
  46. image = Image.new(image, size, color)
  47. self.draw = ImageDraw.Draw(image)
  48. self.image = image
  49. self.transform = None
  50. def flush(self):
  51. return self.image
  52. def render(self, op, xy, pen, brush=None):
  53. # handle color arguments
  54. outline = fill = None
  55. width = 1
  56. if isinstance(pen, Pen):
  57. outline = pen.color
  58. width = pen.width
  59. elif isinstance(brush, Pen):
  60. outline = brush.color
  61. width = brush.width
  62. if isinstance(brush, Brush):
  63. fill = brush.color
  64. elif isinstance(pen, Brush):
  65. fill = pen.color
  66. # handle transformation
  67. if self.transform:
  68. xy = ImagePath.Path(xy)
  69. xy.transform(self.transform)
  70. # render the item
  71. if op == "line":
  72. self.draw.line(xy, fill=outline, width=width)
  73. else:
  74. getattr(self.draw, op)(xy, fill=fill, outline=outline)
  75. def settransform(self, offset):
  76. """Sets a transformation offset."""
  77. (xoffset, yoffset) = offset
  78. self.transform = (1, 0, xoffset, 0, 1, yoffset)
  79. def arc(self, xy, start, end, *options):
  80. """
  81. Draws an arc (a portion of a circle outline) between the start and end
  82. angles, inside the given bounding box.
  83. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc`
  84. """
  85. self.render("arc", xy, start, end, *options)
  86. def chord(self, xy, start, end, *options):
  87. """
  88. Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points
  89. with a straight line.
  90. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord`
  91. """
  92. self.render("chord", xy, start, end, *options)
  93. def ellipse(self, xy, *options):
  94. """
  95. Draws an ellipse inside the given bounding box.
  96. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse`
  97. """
  98. self.render("ellipse", xy, *options)
  99. def line(self, xy, *options):
  100. """
  101. Draws a line between the coordinates in the ``xy`` list.
  102. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line`
  103. """
  104. self.render("line", xy, *options)
  105. def pieslice(self, xy, start, end, *options):
  106. """
  107. Same as arc, but also draws straight lines between the end points and the
  108. center of the bounding box.
  109. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice`
  110. """
  111. self.render("pieslice", xy, start, end, *options)
  112. def polygon(self, xy, *options):
  113. """
  114. Draws a polygon.
  115. The polygon outline consists of straight lines between the given
  116. coordinates, plus a straight line between the last and the first
  117. coordinate.
  118. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon`
  119. """
  120. self.render("polygon", xy, *options)
  121. def rectangle(self, xy, *options):
  122. """
  123. Draws a rectangle.
  124. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle`
  125. """
  126. self.render("rectangle", xy, *options)
  127. def text(self, xy, text, font):
  128. """
  129. Draws the string at the given position.
  130. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text`
  131. """
  132. if self.transform:
  133. xy = ImagePath.Path(xy)
  134. xy.transform(self.transform)
  135. self.draw.text(xy, text, font=font.font, fill=font.color)
  136. def textsize(self, text, font):
  137. """
  138. .. deprecated:: 9.2.0
  139. Return the size of the given string, in pixels.
  140. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
  141. """
  142. deprecate("textsize", 10, "textbbox or textlength")
  143. with warnings.catch_warnings():
  144. warnings.filterwarnings("ignore", category=DeprecationWarning)
  145. return self.draw.textsize(text, font=font.font)
  146. def textbbox(self, xy, text, font):
  147. """
  148. Returns bounding box (in pixels) of given text.
  149. :return: ``(left, top, right, bottom)`` bounding box
  150. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`
  151. """
  152. if self.transform:
  153. xy = ImagePath.Path(xy)
  154. xy.transform(self.transform)
  155. return self.draw.textbbox(xy, text, font=font.font)
  156. def textlength(self, text, font):
  157. """
  158. Returns length (in pixels) of given text.
  159. This is the amount by which following text should be offset.
  160. .. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`
  161. """
  162. return self.draw.textlength(text, font=font.font)