发布于 2015-09-11 16:14:08 | 203 次阅读 | 评论: 0 | 来源: 网络整理

#
# The Python Imaging Library
# $Id$
#
# WCK-style drawing interface operations
#
# History:
# 2003-12-07 fl   created
# 2005-05-15 fl   updated; added to PIL as ImageDraw2
# 2005-05-15 fl   added text support
# 2005-05-20 fl   added arc/chord/pieslice support
#
# Copyright (c) 2003-2005 by Secret Labs AB
# Copyright (c) 2003-2005 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#

from PIL import Image, ImageColor, ImageDraw, ImageFont, ImagePath

[docs]class Pen: def __init__(self, color, width=1, opacity=255): self.color = ImageColor.getrgb(color) self.width = width
[docs]class Brush: def __init__(self, color, opacity=255): self.color = ImageColor.getrgb(color)
[docs]class Font: def __init__(self, color, file, size=12): # FIXME: add support for bitmap fonts self.color = ImageColor.getrgb(color) self.font = ImageFont.truetype(file, size)
[docs]class Draw: def __init__(self, image, size=None, color=None): if not hasattr(image, "im"): image = Image.new(image, size, color) self.draw = ImageDraw.Draw(image) self.image = image self.transform = None
[docs] def flush(self): return self.image
[docs] def render(self, op, xy, pen, brush=None): # handle color arguments outline = fill = None; width = 1 if isinstance(pen, Pen): outline = pen.color width = pen.width elif isinstance(brush, Pen): outline = brush.color width = brush.width if isinstance(brush, Brush): fill = brush.color elif isinstance(pen, Brush): fill = pen.color # handle transformation if self.transform: xy = ImagePath.Path(xy) xy.transform(self.transform) # render the item if op == "line": self.draw.line(xy, fill=outline, width=width) else: getattr(self.draw, op)(xy, fill=fill, outline=outline)
[docs] def settransform(self, offset): (xoffset, yoffset) = offset self.transform = (1, 0, xoffset, 0, 1, yoffset)
[docs] def arc(self, xy, start, end, *options): self.render("arc", xy, start, end, *options)
[docs] def chord(self, xy, start, end, *options): self.render("chord", xy, start, end, *options)
[docs] def ellipse(self, xy, *options): self.render("ellipse", xy, *options)
[docs] def line(self, xy, *options): self.render("line", xy, *options)
[docs] def pieslice(self, xy, start, end, *options): self.render("pieslice", xy, start, end, *options)
[docs] def polygon(self, xy, *options): self.render("polygon", xy, *options)
[docs] def rectangle(self, xy, *options): self.render("rectangle", xy, *options)
[docs] def symbol(self, xy, symbol, *options): raise NotImplementedError("not in this version")
[docs] def text(self, xy, text, font): if self.transform: xy = ImagePath.Path(xy) xy.transform(self.transform) self.draw.text(xy, text, font=font.font, fill=font.color)
[docs] def textsize(self, text, font): return self.draw.textsize(text, font=font.font)
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务