2010-07-31 06:52:47 +04:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
|
|
|
|
2020-06-14 14:47:59 +03:00
|
|
|
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
(Experimental) WCK-style drawing interface operations
|
|
|
|
|
|
|
|
.. seealso:: :py:mod:`PIL.ImageDraw`
|
|
|
|
"""
|
2020-06-14 14:47:59 +03:00
|
|
|
|
|
|
|
|
2022-07-01 13:33:59 +03:00
|
|
|
import warnings
|
|
|
|
|
2017-01-17 16:22:18 +03:00
|
|
|
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
|
2022-05-26 00:00:13 +03:00
|
|
|
from ._deprecate import deprecate
|
2010-07-31 06:52:47 +04:00
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
class Pen:
|
2020-06-14 15:39:48 +03:00
|
|
|
"""Stores an outline color and width."""
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def __init__(self, color, width=1, opacity=255):
|
|
|
|
self.color = ImageColor.getrgb(color)
|
|
|
|
self.width = width
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
class Brush:
|
2020-06-14 15:39:48 +03:00
|
|
|
"""Stores a fill color"""
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def __init__(self, color, opacity=255):
|
|
|
|
self.color = ImageColor.getrgb(color)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
class Font:
|
2020-06-14 15:39:48 +03:00
|
|
|
"""Stores a TrueType font and color"""
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
def __init__(self, color, file, size=12):
|
|
|
|
# FIXME: add support for bitmap fonts
|
|
|
|
self.color = ImageColor.getrgb(color)
|
|
|
|
self.font = ImageFont.truetype(file, size)
|
|
|
|
|
2014-08-26 17:47:10 +04:00
|
|
|
|
2019-09-30 17:56:31 +03:00
|
|
|
class Draw:
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
(Experimental) WCK-style drawing interface
|
|
|
|
"""
|
|
|
|
|
2010-07-31 06:52:47 +04:00
|
|
|
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
|
|
|
|
|
|
|
|
def flush(self):
|
|
|
|
return self.image
|
|
|
|
|
|
|
|
def render(self, op, xy, pen, brush=None):
|
|
|
|
# handle color arguments
|
2014-08-26 17:47:10 +04:00
|
|
|
outline = fill = None
|
|
|
|
width = 1
|
2010-07-31 06:52:47 +04:00
|
|
|
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)
|
|
|
|
|
2012-10-16 06:32:28 +04:00
|
|
|
def settransform(self, offset):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""Sets a transformation offset."""
|
2012-10-16 06:32:28 +04:00
|
|
|
(xoffset, yoffset) = offset
|
2010-07-31 06:52:47 +04:00
|
|
|
self.transform = (1, 0, xoffset, 0, 1, yoffset)
|
|
|
|
|
|
|
|
def arc(self, xy, start, end, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws an arc (a portion of a circle outline) between the start and end
|
|
|
|
angles, inside the given bounding box.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("arc", xy, start, end, *options)
|
|
|
|
|
|
|
|
def chord(self, xy, start, end, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
2020-06-22 10:00:17 +03:00
|
|
|
Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points
|
2020-06-14 15:39:48 +03:00
|
|
|
with a straight line.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("chord", xy, start, end, *options)
|
|
|
|
|
|
|
|
def ellipse(self, xy, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws an ellipse inside the given bounding box.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("ellipse", xy, *options)
|
|
|
|
|
|
|
|
def line(self, xy, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws a line between the coordinates in the ``xy`` list.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("line", xy, *options)
|
|
|
|
|
|
|
|
def pieslice(self, xy, start, end, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Same as arc, but also draws straight lines between the end points and the
|
|
|
|
center of the bounding box.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("pieslice", xy, start, end, *options)
|
|
|
|
|
|
|
|
def polygon(self, xy, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws a polygon.
|
|
|
|
|
|
|
|
The polygon outline consists of straight lines between the given
|
|
|
|
coordinates, plus a straight line between the last and the first
|
|
|
|
coordinate.
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("polygon", xy, *options)
|
|
|
|
|
|
|
|
def rectangle(self, xy, *options):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws a rectangle.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
self.render("rectangle", xy, *options)
|
|
|
|
|
|
|
|
def text(self, xy, text, font):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
|
|
|
Draws the string at the given position.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text`
|
|
|
|
"""
|
2010-07-31 06:52:47 +04:00
|
|
|
if self.transform:
|
|
|
|
xy = ImagePath.Path(xy)
|
|
|
|
xy.transform(self.transform)
|
|
|
|
self.draw.text(xy, text, font=font.font, fill=font.color)
|
|
|
|
|
|
|
|
def textsize(self, text, font):
|
2020-06-14 15:39:48 +03:00
|
|
|
"""
|
2022-06-30 15:40:40 +03:00
|
|
|
.. deprecated:: 9.2.0
|
|
|
|
|
2020-06-14 15:39:48 +03:00
|
|
|
Return the size of the given string, in pixels.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textsize`
|
|
|
|
"""
|
2022-05-26 00:00:13 +03:00
|
|
|
deprecate("textsize", 10, "textbbox or textlength")
|
2022-07-01 13:33:59 +03:00
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.filterwarnings("ignore", category=DeprecationWarning)
|
|
|
|
return self.draw.textsize(text, font=font.font)
|
2022-05-26 00:00:13 +03:00
|
|
|
|
|
|
|
def textbbox(self, xy, text, font):
|
|
|
|
"""
|
|
|
|
Returns bounding box (in pixels) of given text.
|
|
|
|
|
|
|
|
:return: ``(left, top, right, bottom)`` bounding box
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`
|
|
|
|
"""
|
|
|
|
if self.transform:
|
|
|
|
xy = ImagePath.Path(xy)
|
|
|
|
xy.transform(self.transform)
|
|
|
|
return self.draw.textbbox(xy, text, font=font.font)
|
|
|
|
|
|
|
|
def textlength(self, text, font):
|
|
|
|
"""
|
|
|
|
Returns length (in pixels) of given text.
|
|
|
|
This is the amount by which following text should be offset.
|
|
|
|
|
|
|
|
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`
|
|
|
|
"""
|
|
|
|
return self.draw.textlength(text, font=font.font)
|