Pillow/src/PIL/ImageDraw.py

1005 lines
34 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library
# $Id$
#
# drawing interface operations
#
# History:
# 1996-04-13 fl Created (experimental)
# 1996-08-07 fl Filled polygons, ellipses.
# 1996-08-13 fl Added text support
# 1998-06-28 fl Handle I and F images
# 1998-12-29 fl Added arc; use arc primitive to draw ellipses
# 1999-01-10 fl Added shape stuff (experimental)
# 1999-02-06 fl Added bitmap support
# 1999-02-11 fl Changed all primitives to take options
# 1999-02-20 fl Fixed backwards compatibility
# 2000-10-12 fl Copy on write, when necessary
# 2001-02-18 fl Use default ink for bitmap/text also in fill mode
# 2002-10-24 fl Added support for CSS-style color strings
# 2002-12-10 fl Added experimental support for RGBA-on-RGB drawing
# 2002-12-11 fl Refactored low-level drawing API (work in progress)
# 2004-08-26 fl Made Draw() a factory function, added getdraw() support
# 2004-09-04 fl Added width support to line primitive
# 2004-09-10 fl Added font mode handling
# 2006-06-19 fl Added font bearing support (getmask2)
#
# Copyright (c) 1997-2006 by Secret Labs AB
# Copyright (c) 1996-2006 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
2018-09-16 14:29:09 +03:00
import math
import numbers
from . import Image, ImageColor, ImageFont
2010-07-31 06:52:47 +04:00
2016-05-24 10:36:14 +03:00
"""
A simple 2D drawing interface for PIL images.
<p>
Application code should use the <b>Draw</b> factory, instead of
directly.
"""
2010-07-31 06:52:47 +04:00
2015-12-10 01:35:35 +03:00
class ImageDraw:
2010-07-31 06:52:47 +04:00
def __init__(self, im, mode=None):
2016-05-24 10:36:14 +03:00
"""
Create a drawing instance.
:param im: The image to draw in.
:param mode: Optional mode to use for color values. For RGB
2016-05-24 10:36:14 +03:00
images, this argument can be RGB or RGBA (to blend the
drawing into the image). For all other modes, this argument
must be the same as the image mode. If omitted, the mode
defaults to the mode of the image.
"""
2010-07-31 06:52:47 +04:00
im.load()
if im.readonly:
2014-08-26 17:47:10 +04:00
im._copy() # make it writeable
2010-07-31 06:52:47 +04:00
blend = 0
if mode is None:
mode = im.mode
if mode != im.mode:
if mode == "RGBA" and im.mode == "RGB":
blend = 1
else:
raise ValueError("mode mismatch")
if mode == "P":
self.palette = im.palette
else:
self.palette = None
self._image = im
2010-07-31 06:52:47 +04:00
self.im = im.im
self.draw = Image.core.draw(self.im, blend)
self.mode = mode
if mode in ("I", "F"):
2019-03-28 08:41:23 +03:00
self.ink = self.draw.draw_ink(1)
2010-07-31 06:52:47 +04:00
else:
2019-03-28 08:41:23 +03:00
self.ink = self.draw.draw_ink(-1)
2010-07-31 06:52:47 +04:00
if mode in ("1", "P", "I", "F"):
# FIXME: fix Fill2 to properly support matte for I+F images
self.fontmode = "1"
else:
2014-08-26 17:47:10 +04:00
self.fontmode = "L" # aliasing is okay for other modes
2010-07-31 06:52:47 +04:00
self.fill = 0
self.font = None
def getfont(self):
2017-07-16 08:22:46 +03:00
"""
Get the current default font.
:returns: An image font."""
2010-07-31 06:52:47 +04:00
if not self.font:
# FIXME: should add a font repository
from . import ImageFont
2019-03-21 16:28:20 +03:00
2010-07-31 06:52:47 +04:00
self.font = ImageFont.load_default()
return self.font
def _getink(self, ink, fill=None):
if ink is None and fill is None:
if self.fill:
fill = self.ink
else:
ink = self.ink
else:
if ink is not None:
if isinstance(ink, str):
2010-07-31 06:52:47 +04:00
ink = ImageColor.getcolor(ink, self.mode)
if self.palette and not isinstance(ink, numbers.Number):
ink = self.palette.getcolor(ink, self._image)
2019-03-28 08:41:23 +03:00
ink = self.draw.draw_ink(ink)
2010-07-31 06:52:47 +04:00
if fill is not None:
if isinstance(fill, str):
2010-07-31 06:52:47 +04:00
fill = ImageColor.getcolor(fill, self.mode)
if self.palette and not isinstance(fill, numbers.Number):
fill = self.palette.getcolor(fill, self._image)
2019-03-28 08:41:23 +03:00
fill = self.draw.draw_ink(fill)
2010-07-31 06:52:47 +04:00
return ink, fill
2020-06-29 22:49:11 +03:00
def arc(self, xy, start, end, fill=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw an arc."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(fill)
if ink is not None:
self.draw.draw_arc(xy, start, end, ink, width)
2010-07-31 06:52:47 +04:00
def bitmap(self, xy, bitmap, fill=None):
2016-05-24 10:36:14 +03:00
"""Draw a bitmap."""
2010-07-31 06:52:47 +04:00
bitmap.load()
ink, fill = self._getink(fill)
if ink is None:
ink = fill
if ink is not None:
self.draw.draw_bitmap(xy, bitmap.im, ink)
def chord(self, xy, start, end, fill=None, outline=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw a chord."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_chord(xy, start, end, fill, 1)
if ink is not None and ink != fill and width != 0:
self.draw.draw_chord(xy, start, end, ink, 0, width)
2010-07-31 06:52:47 +04:00
def ellipse(self, xy, fill=None, outline=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw an ellipse."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_ellipse(xy, fill, 1)
if ink is not None and ink != fill and width != 0:
self.draw.draw_ellipse(xy, ink, 0, width)
2010-07-31 06:52:47 +04:00
2018-09-16 14:29:09 +03:00
def line(self, xy, fill=None, width=0, joint=None):
2016-05-24 10:36:14 +03:00
"""Draw a line, or a connected sequence of line segments."""
2018-09-16 14:29:09 +03:00
ink = self._getink(fill)[0]
2010-07-31 06:52:47 +04:00
if ink is not None:
self.draw.draw_lines(xy, ink, width)
2018-09-16 14:29:09 +03:00
if joint == "curve" and width > 4:
if not isinstance(xy[0], (list, tuple)):
xy = [tuple(xy[i : i + 2]) for i in range(0, len(xy), 2)]
2019-03-21 16:28:20 +03:00
for i in range(1, len(xy) - 1):
2018-09-16 14:29:09 +03:00
point = xy[i]
angles = [
2019-03-21 16:28:20 +03:00
math.degrees(math.atan2(end[0] - start[0], start[1] - end[1]))
% 360
for start, end in ((xy[i - 1], point), (point, xy[i + 1]))
2018-09-16 14:29:09 +03:00
]
if angles[0] == angles[1]:
# This is a straight line, so no joint is required
continue
def coord_at_angle(coord, angle):
x, y = coord
angle -= 90
2019-03-21 16:28:20 +03:00
distance = width / 2 - 1
return tuple(
p + (math.floor(p_d) if p_d > 0 else math.ceil(p_d))
for p, p_d in (
(x, distance * math.cos(math.radians(angle))),
(y, distance * math.sin(math.radians(angle))),
)
2019-03-21 16:28:20 +03:00
)
flipped = (
angles[1] > angles[0] and angles[1] - 180 > angles[0]
) or (angles[1] < angles[0] and angles[1] + 180 > angles[0])
2018-09-16 14:29:09 +03:00
coords = [
2019-03-21 16:28:20 +03:00
(point[0] - width / 2 + 1, point[1] - width / 2 + 1),
(point[0] + width / 2 - 1, point[1] + width / 2 - 1),
2018-09-16 14:29:09 +03:00
]
if flipped:
start, end = (angles[1] + 90, angles[0] + 90)
else:
start, end = (angles[0] - 90, angles[1] - 90)
self.pieslice(coords, start - 90, end - 90, fill)
if width > 8:
# Cover potential gaps between the line and the joint
if flipped:
gapCoords = [
2019-03-21 16:28:20 +03:00
coord_at_angle(point, angles[0] + 90),
2018-09-16 14:29:09 +03:00
point,
2019-03-21 16:28:20 +03:00
coord_at_angle(point, angles[1] + 90),
2018-09-16 14:29:09 +03:00
]
else:
gapCoords = [
2019-03-21 16:28:20 +03:00
coord_at_angle(point, angles[0] - 90),
2018-09-16 14:29:09 +03:00
point,
2019-03-21 16:28:20 +03:00
coord_at_angle(point, angles[1] - 90),
2018-09-16 14:29:09 +03:00
]
self.line(gapCoords, fill, width=3)
2010-07-31 06:52:47 +04:00
def shape(self, shape, fill=None, outline=None):
2016-05-24 10:36:14 +03:00
"""(Experimental) Draw a shape."""
2010-07-31 06:52:47 +04:00
shape.close()
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_outline(shape, fill, 1)
if ink is not None and ink != fill:
2010-07-31 06:52:47 +04:00
self.draw.draw_outline(shape, ink, 0)
def pieslice(self, xy, start, end, fill=None, outline=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw a pieslice."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_pieslice(xy, start, end, fill, 1)
if ink is not None and ink != fill and width != 0:
self.draw.draw_pieslice(xy, start, end, ink, 0, width)
2010-07-31 06:52:47 +04:00
def point(self, xy, fill=None):
2016-05-24 10:36:14 +03:00
"""Draw one or more individual pixels."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(fill)
if ink is not None:
self.draw.draw_points(xy, ink)
2021-11-16 14:02:54 +03:00
def polygon(self, xy, fill=None, outline=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw a polygon."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_polygon(xy, fill, 1)
2021-11-16 14:02:54 +03:00
if ink is not None and ink != fill and width != 0:
if width == 1:
self.draw.draw_polygon(xy, ink, 0, width)
else:
# To avoid expanding the polygon outwards,
# use the fill as a mask
mask = Image.new("1", self.im.size)
mask_ink = self._getink(1)[0]
fill_im = mask.copy()
draw = Draw(fill_im)
draw.draw.draw_polygon(xy, mask_ink, 1)
ink_im = mask.copy()
draw = Draw(ink_im)
width = width * 2 - 1
draw.draw.draw_polygon(xy, mask_ink, 0, width)
mask.paste(ink_im, mask=fill_im)
im = Image.new(self.mode, self.im.size)
draw = Draw(im)
draw.draw.draw_polygon(xy, ink, 0, width)
self.im.paste(im.im, (0, 0) + im.size, mask.im)
2010-07-31 06:52:47 +04:00
def regular_polygon(
self, bounding_circle, n_sides, rotation=0, fill=None, outline=None
):
2020-08-09 23:43:37 +03:00
"""Draw a regular polygon."""
xy = _compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
self.polygon(xy, fill, outline)
2020-08-09 23:43:37 +03:00
def rectangle(self, xy, fill=None, outline=None, width=1):
2016-05-24 10:36:14 +03:00
"""Draw a rectangle."""
2010-07-31 06:52:47 +04:00
ink, fill = self._getink(outline, fill)
if fill is not None:
self.draw.draw_rectangle(xy, fill, 1)
if ink is not None and ink != fill and width != 0:
2018-04-12 18:15:27 +03:00
self.draw.draw_rectangle(xy, ink, 0, width)
2010-07-31 06:52:47 +04:00
2021-03-08 11:53:59 +03:00
def rounded_rectangle(self, xy, radius=0, fill=None, outline=None, width=1):
"""Draw a rounded rectangle."""
if isinstance(xy[0], (list, tuple)):
(x0, y0), (x1, y1) = xy
else:
x0, y0, x1, y1 = xy
2021-02-21 23:38:04 +03:00
d = radius * 2
full_x = d >= x1 - x0
if full_x:
# The two left and two right corners are joined
d = x1 - x0
full_y = d >= y1 - y0
if full_y:
# The two top and two bottom corners are joined
d = y1 - y0
if full_x and full_y:
# If all corners are joined, that is a circle
return self.ellipse(xy, fill, outline, width)
2021-03-08 11:53:59 +03:00
if d == 0:
2021-02-21 23:38:04 +03:00
# If the corners have no curve, that is a rectangle
2021-03-08 11:53:59 +03:00
return self.rectangle(xy, fill, outline, width)
r = d // 2
2021-03-08 11:53:59 +03:00
ink, fill = self._getink(outline, fill)
2021-02-21 23:38:04 +03:00
def draw_corners(pieslice):
if full_x:
# Draw top and bottom halves
parts = (
((x0, y0, x0 + d, y0 + d), 180, 360),
((x0, y1 - d, x0 + d, y1), 0, 180),
)
elif full_y:
# Draw left and right halves
parts = (
((x0, y0, x0 + d, y0 + d), 90, 270),
((x1 - d, y0, x1, y0 + d), 270, 90),
)
else:
# Draw four separate corners
parts = (
((x1 - d, y0, x1, y0 + d), 270, 360),
((x1 - d, y1 - d, x1, y1), 0, 90),
((x0, y1 - d, x0 + d, y1), 90, 180),
((x0, y0, x0 + d, y0 + d), 180, 270),
)
for part in parts:
if pieslice:
self.draw.draw_pieslice(*(part + (fill, 1)))
else:
self.draw.draw_arc(*(part + (ink, width)))
2021-03-08 11:53:59 +03:00
if fill is not None:
2021-02-21 23:38:04 +03:00
draw_corners(True)
if full_x:
self.draw.draw_rectangle((x0, y0 + r + 1, x1, y1 - r - 1), fill, 1)
2021-02-21 23:38:04 +03:00
else:
self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill, 1)
2021-02-21 23:38:04 +03:00
if not full_x and not full_y:
self.draw.draw_rectangle((x0, y0 + r + 1, x0 + r, y1 - r - 1), fill, 1)
self.draw.draw_rectangle((x1 - r, y0 + r + 1, x1, y1 - r - 1), fill, 1)
2021-03-08 11:53:59 +03:00
if ink is not None and ink != fill and width != 0:
2021-02-21 23:38:04 +03:00
draw_corners(False)
if not full_x:
self.draw.draw_rectangle(
(x0 + r + 1, y0, x1 - r - 1, y0 + width - 1), ink, 1
2021-02-21 23:38:04 +03:00
)
self.draw.draw_rectangle(
(x0 + r + 1, y1 - width + 1, x1 - r - 1, y1), ink, 1
2021-02-21 23:38:04 +03:00
)
if not full_y:
self.draw.draw_rectangle(
(x0, y0 + r + 1, x0 + width - 1, y1 - r - 1), ink, 1
2021-02-21 23:38:04 +03:00
)
self.draw.draw_rectangle(
(x1 - width + 1, y0 + r + 1, x1, y1 - r - 1), ink, 1
2021-02-21 23:38:04 +03:00
)
2021-03-08 11:53:59 +03:00
def _multiline_check(self, text):
2016-05-24 10:36:14 +03:00
"""Draw text."""
split_character = "\n" if isinstance(text, str) else b"\n"
return split_character in text
def _multiline_split(self, text):
split_character = "\n" if isinstance(text, str) else b"\n"
return text.split(split_character)
2019-07-28 23:40:03 +03:00
def text(
self,
xy,
text,
fill=None,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
stroke_width=0,
stroke_fill=None,
2020-03-28 07:58:35 +03:00
embedded_color=False,
2019-07-28 23:40:03 +03:00
*args,
2020-07-15 13:37:43 +03:00
**kwargs,
2019-07-28 23:40:03 +03:00
):
if self._multiline_check(text):
2019-07-28 23:40:03 +03:00
return self.multiline_text(
xy,
text,
fill,
font,
anchor,
spacing,
align,
direction,
features,
language,
stroke_width,
stroke_fill,
2020-03-28 07:58:35 +03:00
embedded_color,
2019-07-28 23:40:03 +03:00
)
2020-03-28 07:58:35 +03:00
if embedded_color and self.mode not in ("RGB", "RGBA"):
raise ValueError("Embedded color supported only in RGB and RGBA modes")
2010-07-31 06:52:47 +04:00
if font is None:
font = self.getfont()
2019-07-28 23:40:03 +03:00
def getink(fill):
ink, fill = self._getink(fill)
if ink is None:
return fill
return ink
def draw_text(ink, stroke_width=0, stroke_offset=None):
2020-03-28 07:58:35 +03:00
mode = self.fontmode
if stroke_width == 0 and embedded_color:
mode = "RGBA"
2019-07-28 23:40:03 +03:00
coord = xy
2010-07-31 06:52:47 +04:00
try:
2019-07-28 23:40:03 +03:00
mask, offset = font.getmask2(
text,
2020-03-28 07:58:35 +03:00
mode,
2019-07-28 23:40:03 +03:00
direction=direction,
features=features,
language=language,
stroke_width=stroke_width,
anchor=anchor,
2020-03-28 07:58:35 +03:00
ink=ink,
2019-07-28 23:40:03 +03:00
*args,
2019-11-02 21:02:07 +03:00
**kwargs,
2019-07-28 23:40:03 +03:00
)
coord = coord[0] + offset[0], coord[1] + offset[1]
2010-07-31 06:52:47 +04:00
except AttributeError:
try:
2019-07-28 23:40:03 +03:00
mask = font.getmask(
text,
2020-03-28 07:58:35 +03:00
mode,
2019-07-28 23:40:03 +03:00
direction,
features,
language,
stroke_width,
anchor,
2020-03-28 07:58:35 +03:00
ink,
2019-07-28 23:40:03 +03:00
*args,
2019-11-02 21:02:07 +03:00
**kwargs,
2019-07-28 23:40:03 +03:00
)
2010-07-31 06:52:47 +04:00
except TypeError:
mask = font.getmask(text)
2019-07-28 23:40:03 +03:00
if stroke_offset:
coord = coord[0] + stroke_offset[0], coord[1] + stroke_offset[1]
2020-03-28 07:58:35 +03:00
if mode == "RGBA":
# font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
# extract mask and set text alpha
color, mask = mask, mask.getband(3)
color.fillband(3, (ink >> 24) & 0xFF)
coord2 = coord[0] + mask.size[0], coord[1] + mask.size[1]
self.im.paste(color, coord + coord2, mask)
else:
self.draw.draw_bitmap(coord, mask, ink)
2019-07-28 23:40:03 +03:00
ink = getink(fill)
if ink is not None:
stroke_ink = None
if stroke_width:
stroke_ink = getink(stroke_fill) if stroke_fill is not None else ink
if stroke_ink is not None:
# Draw stroked text
draw_text(stroke_ink, stroke_width)
2019-07-28 23:40:03 +03:00
# Draw normal text
draw_text(ink, 0)
2019-07-28 23:40:03 +03:00
else:
# Only draw normal text
draw_text(ink)
2010-07-31 06:52:47 +04:00
2019-03-21 16:28:20 +03:00
def multiline_text(
self,
xy,
text,
fill=None,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
2019-07-28 23:40:03 +03:00
stroke_width=0,
stroke_fill=None,
2020-03-28 07:58:35 +03:00
embedded_color=False,
2019-03-21 16:28:20 +03:00
):
if direction == "ttb":
raise ValueError("ttb direction is unsupported for multiline text")
if anchor is None:
anchor = "la"
elif len(anchor) != 2:
raise ValueError("anchor must be a 2 character string")
elif anchor[1] in "tb":
raise ValueError("anchor not supported for multiline text")
2015-11-18 18:02:16 +03:00
widths = []
max_width = 0
lines = self._multiline_split(text)
2019-07-28 23:40:03 +03:00
line_spacing = (
self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
)
for line in lines:
line_width = self.textlength(
line, font, direction=direction, features=features, language=language
2019-03-21 16:28:20 +03:00
)
widths.append(line_width)
max_width = max(max_width, line_width)
top = xy[1]
if anchor[1] == "m":
top -= (len(lines) - 1) * line_spacing / 2.0
elif anchor[1] == "d":
top -= (len(lines) - 1) * line_spacing
for idx, line in enumerate(lines):
left = xy[0]
width_difference = max_width - widths[idx]
# first align left by anchor
if anchor[0] == "m":
left -= width_difference / 2.0
elif anchor[0] == "r":
left -= width_difference
# then align by align parameter
if align == "left":
pass
elif align == "center":
left += width_difference / 2.0
elif align == "right":
left += width_difference
else:
2018-07-04 16:47:48 +03:00
raise ValueError('align must be "left", "center" or "right"')
2019-03-21 16:28:20 +03:00
self.text(
(left, top),
line,
fill,
font,
anchor,
direction=direction,
features=features,
language=language,
2019-07-28 23:40:03 +03:00
stroke_width=stroke_width,
stroke_fill=stroke_fill,
2020-03-28 07:58:35 +03:00
embedded_color=embedded_color,
2019-03-21 16:28:20 +03:00
)
2015-11-18 18:02:16 +03:00
top += line_spacing
2019-03-21 16:28:20 +03:00
def textsize(
2019-07-28 23:40:03 +03:00
self,
text,
font=None,
spacing=4,
direction=None,
features=None,
language=None,
stroke_width=0,
2019-03-21 16:28:20 +03:00
):
2016-05-24 10:36:14 +03:00
"""Get the size of a given string, in pixels."""
if self._multiline_check(text):
2019-03-21 16:28:20 +03:00
return self.multiline_textsize(
2019-07-28 23:40:03 +03:00
text, font, spacing, direction, features, language, stroke_width
2019-03-21 16:28:20 +03:00
)
2010-07-31 06:52:47 +04:00
if font is None:
font = self.getfont()
2019-07-28 23:40:03 +03:00
return font.getsize(text, direction, features, language, stroke_width)
2010-07-31 06:52:47 +04:00
2019-03-21 16:28:20 +03:00
def multiline_textsize(
2019-07-28 23:40:03 +03:00
self,
text,
font=None,
spacing=4,
direction=None,
features=None,
language=None,
stroke_width=0,
2019-03-21 16:28:20 +03:00
):
max_width = 0
lines = self._multiline_split(text)
2019-07-28 23:40:03 +03:00
line_spacing = (
self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
)
for line in lines:
2019-03-21 16:28:20 +03:00
line_width, line_height = self.textsize(
2019-07-28 23:40:03 +03:00
line, font, spacing, direction, features, language, stroke_width
2019-03-21 16:28:20 +03:00
)
max_width = max(max_width, line_width)
2019-03-21 16:28:20 +03:00
return max_width, len(lines) * line_spacing - spacing
def textlength(
self,
text,
font=None,
direction=None,
features=None,
language=None,
embedded_color=False,
):
"""Get the length of a given string, in pixels with 1/64 precision."""
if self._multiline_check(text):
raise ValueError("can't measure length of multiline text")
if embedded_color and self.mode not in ("RGB", "RGBA"):
raise ValueError("Embedded color supported only in RGB and RGBA modes")
if font is None:
font = self.getfont()
mode = "RGBA" if embedded_color else self.fontmode
try:
return font.getlength(text, mode, direction, features, language)
except AttributeError:
size = self.textsize(
text, font, direction=direction, features=features, language=language
)
if direction == "ttb":
return size[1]
return size[0]
def textbbox(
self,
xy,
text,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
stroke_width=0,
embedded_color=False,
):
"""Get the bounding box of a given string, in pixels."""
if embedded_color and self.mode not in ("RGB", "RGBA"):
raise ValueError("Embedded color supported only in RGB and RGBA modes")
if self._multiline_check(text):
return self.multiline_textbbox(
xy,
text,
font,
anchor,
spacing,
align,
direction,
features,
language,
stroke_width,
embedded_color,
)
if font is None:
font = self.getfont()
if not isinstance(font, ImageFont.FreeTypeFont):
raise ValueError("Only supported for TrueType fonts")
mode = "RGBA" if embedded_color else self.fontmode
bbox = font.getbbox(
text, mode, direction, features, language, stroke_width, anchor
)
return bbox[0] + xy[0], bbox[1] + xy[1], bbox[2] + xy[0], bbox[3] + xy[1]
def multiline_textbbox(
self,
xy,
text,
font=None,
anchor=None,
spacing=4,
align="left",
direction=None,
features=None,
language=None,
stroke_width=0,
embedded_color=False,
):
if direction == "ttb":
raise ValueError("ttb direction is unsupported for multiline text")
if anchor is None:
anchor = "la"
elif len(anchor) != 2:
raise ValueError("anchor must be a 2 character string")
elif anchor[1] in "tb":
raise ValueError("anchor not supported for multiline text")
widths = []
max_width = 0
lines = self._multiline_split(text)
line_spacing = (
self.textsize("A", font=font, stroke_width=stroke_width)[1] + spacing
)
for line in lines:
line_width = self.textlength(
line,
font,
direction=direction,
features=features,
language=language,
embedded_color=embedded_color,
)
widths.append(line_width)
max_width = max(max_width, line_width)
top = xy[1]
if anchor[1] == "m":
top -= (len(lines) - 1) * line_spacing / 2.0
elif anchor[1] == "d":
top -= (len(lines) - 1) * line_spacing
bbox = None
for idx, line in enumerate(lines):
left = xy[0]
width_difference = max_width - widths[idx]
# first align left by anchor
if anchor[0] == "m":
left -= width_difference / 2.0
elif anchor[0] == "r":
left -= width_difference
# then align by align parameter
if align == "left":
pass
elif align == "center":
left += width_difference / 2.0
elif align == "right":
left += width_difference
else:
raise ValueError('align must be "left", "center" or "right"')
bbox_line = self.textbbox(
(left, top),
line,
font,
anchor,
direction=direction,
features=features,
language=language,
stroke_width=stroke_width,
embedded_color=embedded_color,
)
if bbox is None:
bbox = bbox_line
else:
bbox = (
min(bbox[0], bbox_line[0]),
min(bbox[1], bbox_line[1]),
max(bbox[2], bbox_line[2]),
max(bbox[3], bbox_line[3]),
)
top += line_spacing
if bbox is None:
return xy[0], xy[1], xy[0], xy[1]
return bbox
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
def Draw(im, mode=None):
2016-05-24 10:36:14 +03:00
"""
A simple 2D drawing interface for PIL images.
:param im: The image to draw in.
:param mode: Optional mode to use for color values. For RGB
2016-05-24 10:36:14 +03:00
images, this argument can be RGB or RGBA (to blend the
drawing into the image). For all other modes, this argument
must be the same as the image mode. If omitted, the mode
defaults to the mode of the image.
"""
2010-07-31 06:52:47 +04:00
try:
return im.getdraw(mode)
except AttributeError:
return ImageDraw(im, mode)
2017-05-16 10:33:38 +03:00
2010-07-31 06:52:47 +04:00
# experimental access to the outline API
try:
Outline = Image.core.outline
2015-05-27 02:15:45 +03:00
except AttributeError:
2010-07-31 06:52:47 +04:00
Outline = None
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
def getdraw(im=None, hints=None):
2016-05-24 10:36:14 +03:00
"""
(Experimental) A more advanced 2D drawing interface for PIL images,
based on the WCK interface.
:param im: The image to draw in.
:param hints: An optional list of hints.
:returns: A (drawing context, drawing resource factory) tuple.
2016-05-24 10:36:14 +03:00
"""
2010-07-31 06:52:47 +04:00
# FIXME: this needs more work!
# FIXME: come up with a better 'hints' scheme.
handler = None
if not hints or "nicest" in hints:
try:
from . import _imagingagg as handler
2010-07-31 06:52:47 +04:00
except ImportError:
pass
if handler is None:
from . import ImageDraw2 as handler
2010-07-31 06:52:47 +04:00
if im:
im = handler.Draw(im)
return im, handler
2014-08-26 17:47:10 +04:00
def floodfill(image, xy, value, border=None, thresh=0):
2016-05-24 10:36:14 +03:00
"""
(experimental) Fills a bounded region with a given color.
:param image: Target image.
:param xy: Seed position (a 2-item coordinate tuple). See
:ref:`coordinate-system`.
:param value: Fill color.
:param border: Optional border value. If given, the region consists of
2016-05-24 10:36:14 +03:00
pixels with a color different from the border color. If not given,
the region consists of pixels having the same color as the seed
pixel.
2017-07-16 08:22:46 +03:00
:param thresh: Optional threshold value which specifies a maximum
tolerable difference of a pixel value from the 'background' in
order for it to be replaced. Useful for filling regions of
non-homogeneous, but similar, colors.
2016-05-24 10:36:14 +03:00
"""
2010-07-31 06:52:47 +04:00
# based on an implementation by Eric S. Raymond
# amended by yo1995 @20180806
2010-07-31 06:52:47 +04:00
pixel = image.load()
x, y = xy
try:
background = pixel[x, y]
if _color_diff(value, background) <= thresh:
2014-08-26 17:47:10 +04:00
return # seed point already has fill color
2010-07-31 06:52:47 +04:00
pixel[x, y] = value
except (ValueError, IndexError):
2014-08-26 17:47:10 +04:00
return # seed point outside image
edge = {(x, y)}
2018-10-21 10:26:08 +03:00
# use a set to keep record of current and previous edge pixels
# to reduce memory consumption
full_edge = set()
2018-09-01 11:30:04 +03:00
while edge:
new_edge = set()
for (x, y) in edge: # 4 adjacent method
2019-03-21 16:28:20 +03:00
for (s, t) in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
# If already processed, or if a coordinate is negative, skip
if (s, t) in full_edge or s < 0 or t < 0:
continue
2018-09-01 11:30:04 +03:00
try:
p = pixel[s, t]
except (ValueError, IndexError):
pass
else:
full_edge.add((s, t))
2018-09-01 11:30:04 +03:00
if border is None:
fill = _color_diff(p, background) <= thresh
2010-07-31 06:52:47 +04:00
else:
2018-09-01 11:30:04 +03:00
fill = p != value and p != border
if fill:
pixel[s, t] = value
new_edge.add((s, t))
2018-09-05 04:15:25 +03:00
full_edge = edge # discard pixels processed
edge = new_edge
2017-07-16 08:22:46 +03:00
def _compute_regular_polygon_vertices(bounding_circle, n_sides, rotation):
2020-08-09 23:43:37 +03:00
"""
Generate a list of vertices for a 2D regular polygon.
:param bounding_circle: The bounding circle is a tuple defined
by a point and radius. The polygon is inscribed in this circle.
(e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)
:param n_sides: Number of sides
(e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)
2020-08-09 23:43:37 +03:00
:param rotation: Apply an arbitrary rotation to the polygon
(e.g. ``rotation=90``, applies a 90 degree rotation)
2020-08-09 23:43:37 +03:00
:return: List of regular polygon vertices
(e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)
2020-08-09 23:43:37 +03:00
How are the vertices computed?
1. Compute the following variables
- theta: Angle between the apothem & the nearest polygon vertex
- side_length: Length of each polygon edge
- centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)
- polygon_radius: Polygon radius (last element of bounding_circle)
2020-08-09 23:43:37 +03:00
- angles: Location of each polygon vertex in polar grid
(e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])
2. For each angle in angles, get the polygon vertex at that angle
The vertex is computed using the equation below.
X= xcos(φ) + ysin(φ)
Y= xsin(φ) + ycos(φ)
Note:
φ = angle in degrees
x = 0
y = polygon_radius
The formula above assumes rotation around the origin.
In our case, we are rotating around the centroid.
To account for this, we use the formula below
X = xcos(φ) + ysin(φ) + centroid_x
Y = xsin(φ) + ycos(φ) + centroid_y
"""
# 1. Error Handling
# 1.1 Check `n_sides` has an appropriate value
if not isinstance(n_sides, int):
raise TypeError("n_sides should be an int")
if n_sides < 3:
raise ValueError("n_sides should be an int > 2")
# 1.2 Check `bounding_circle` has an appropriate value
if not isinstance(bounding_circle, (list, tuple)):
raise TypeError("bounding_circle should be a tuple")
if len(bounding_circle) == 3:
*centroid, polygon_radius = bounding_circle
elif len(bounding_circle) == 2:
centroid, polygon_radius = bounding_circle
else:
2020-08-09 23:43:37 +03:00
raise ValueError(
"bounding_circle should contain 2D coordinates "
"and a radius (e.g. (x, y, r) or ((x, y), r) )"
2020-08-09 23:43:37 +03:00
)
if not all(isinstance(i, (int, float)) for i in (*centroid, polygon_radius)):
raise ValueError("bounding_circle should only contain numeric data")
if not len(centroid) == 2:
raise ValueError(
"bounding_circle centre should contain 2D coordinates (e.g. (x, y))"
)
2020-08-09 23:43:37 +03:00
if polygon_radius <= 0:
raise ValueError("bounding_circle radius should be > 0")
2020-08-09 23:43:37 +03:00
# 1.3 Check `rotation` has an appropriate value
if not isinstance(rotation, (int, float)):
raise ValueError("rotation should be an int or float")
# 2. Define Helper Functions
def _apply_rotation(point, degrees, centroid):
2020-08-09 23:43:37 +03:00
return (
round(
point[0] * math.cos(math.radians(360 - degrees))
- point[1] * math.sin(math.radians(360 - degrees))
+ centroid[0],
2,
),
round(
point[1] * math.cos(math.radians(360 - degrees))
+ point[0] * math.sin(math.radians(360 - degrees))
+ centroid[1],
2,
),
)
def _compute_polygon_vertex(centroid, polygon_radius, angle):
2020-08-09 23:43:37 +03:00
start_point = [polygon_radius, 0]
return _apply_rotation(start_point, angle, centroid)
2020-08-09 23:43:37 +03:00
def _get_angles(n_sides, rotation):
2020-08-09 23:43:37 +03:00
angles = []
degrees = 360 / n_sides
2020-08-09 23:43:37 +03:00
# Start with the bottom left polygon vertex
current_angle = (270 - 0.5 * degrees) + rotation
for _ in range(0, n_sides):
2020-08-09 23:43:37 +03:00
angles.append(current_angle)
current_angle += degrees
if current_angle > 360:
current_angle -= 360
return angles
# 3. Variable Declarations
angles = _get_angles(n_sides, rotation)
2020-08-09 23:43:37 +03:00
# 4. Compute Vertices
return [
_compute_polygon_vertex(centroid, polygon_radius, angle) for angle in angles
]
2020-08-09 23:43:37 +03:00
def _color_diff(color1, color2):
"""
Uses 1-norm distance to calculate difference between two values.
"""
if isinstance(color2, tuple):
return sum(abs(color1[i] - color2[i]) for i in range(0, len(color2)))
else:
2019-03-21 16:28:20 +03:00
return abs(color1 - color2)