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.

108 lines
3.1 KiB

4 years ago
  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. from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
  19. class Pen(object):
  20. def __init__(self, color, width=1, opacity=255):
  21. self.color = ImageColor.getrgb(color)
  22. self.width = width
  23. class Brush(object):
  24. def __init__(self, color, opacity=255):
  25. self.color = ImageColor.getrgb(color)
  26. class Font(object):
  27. def __init__(self, color, file, size=12):
  28. # FIXME: add support for bitmap fonts
  29. self.color = ImageColor.getrgb(color)
  30. self.font = ImageFont.truetype(file, size)
  31. class Draw(object):
  32. def __init__(self, image, size=None, color=None):
  33. if not hasattr(image, "im"):
  34. image = Image.new(image, size, color)
  35. self.draw = ImageDraw.Draw(image)
  36. self.image = image
  37. self.transform = None
  38. def flush(self):
  39. return self.image
  40. def render(self, op, xy, pen, brush=None):
  41. # handle color arguments
  42. outline = fill = None
  43. width = 1
  44. if isinstance(pen, Pen):
  45. outline = pen.color
  46. width = pen.width
  47. elif isinstance(brush, Pen):
  48. outline = brush.color
  49. width = brush.width
  50. if isinstance(brush, Brush):
  51. fill = brush.color
  52. elif isinstance(pen, Brush):
  53. fill = pen.color
  54. # handle transformation
  55. if self.transform:
  56. xy = ImagePath.Path(xy)
  57. xy.transform(self.transform)
  58. # render the item
  59. if op == "line":
  60. self.draw.line(xy, fill=outline, width=width)
  61. else:
  62. getattr(self.draw, op)(xy, fill=fill, outline=outline)
  63. def settransform(self, offset):
  64. (xoffset, yoffset) = offset
  65. self.transform = (1, 0, xoffset, 0, 1, yoffset)
  66. def arc(self, xy, start, end, *options):
  67. self.render("arc", xy, start, end, *options)
  68. def chord(self, xy, start, end, *options):
  69. self.render("chord", xy, start, end, *options)
  70. def ellipse(self, xy, *options):
  71. self.render("ellipse", xy, *options)
  72. def line(self, xy, *options):
  73. self.render("line", xy, *options)
  74. def pieslice(self, xy, start, end, *options):
  75. self.render("pieslice", xy, start, end, *options)
  76. def polygon(self, xy, *options):
  77. self.render("polygon", xy, *options)
  78. def rectangle(self, xy, *options):
  79. self.render("rectangle", xy, *options)
  80. def text(self, xy, text, font):
  81. if self.transform:
  82. xy = ImagePath.Path(xy)
  83. xy.transform(self.transform)
  84. self.draw.text(xy, text, font=font.font, fill=font.color)
  85. def textsize(self, text, font):
  86. return self.draw.textsize(text, font=font.font)