ImageDraw2.py 4.9 KB

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