ImageDraw.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  1. #
  2. # The Python Imaging Library
  3. # $Id$
  4. #
  5. # drawing interface operations
  6. #
  7. # History:
  8. # 1996-04-13 fl Created (experimental)
  9. # 1996-08-07 fl Filled polygons, ellipses.
  10. # 1996-08-13 fl Added text support
  11. # 1998-06-28 fl Handle I and F images
  12. # 1998-12-29 fl Added arc; use arc primitive to draw ellipses
  13. # 1999-01-10 fl Added shape stuff (experimental)
  14. # 1999-02-06 fl Added bitmap support
  15. # 1999-02-11 fl Changed all primitives to take options
  16. # 1999-02-20 fl Fixed backwards compatibility
  17. # 2000-10-12 fl Copy on write, when necessary
  18. # 2001-02-18 fl Use default ink for bitmap/text also in fill mode
  19. # 2002-10-24 fl Added support for CSS-style color strings
  20. # 2002-12-10 fl Added experimental support for RGBA-on-RGB drawing
  21. # 2002-12-11 fl Refactored low-level drawing API (work in progress)
  22. # 2004-08-26 fl Made Draw() a factory function, added getdraw() support
  23. # 2004-09-04 fl Added width support to line primitive
  24. # 2004-09-10 fl Added font mode handling
  25. # 2006-06-19 fl Added font bearing support (getmask2)
  26. #
  27. # Copyright (c) 1997-2006 by Secret Labs AB
  28. # Copyright (c) 1996-2006 by Fredrik Lundh
  29. #
  30. # See the README file for information on usage and redistribution.
  31. #
  32. from __future__ import annotations
  33. import math
  34. import struct
  35. from collections.abc import Sequence
  36. from types import ModuleType
  37. from typing import TYPE_CHECKING, Any, AnyStr, Callable, Union, cast
  38. from . import Image, ImageColor
  39. from ._deprecate import deprecate
  40. from ._typing import Coords
  41. # experimental access to the outline API
  42. Outline: Callable[[], Image.core._Outline] | None
  43. try:
  44. Outline = Image.core.outline
  45. except AttributeError:
  46. Outline = None
  47. if TYPE_CHECKING:
  48. from . import ImageDraw2, ImageFont
  49. _Ink = Union[float, tuple[int, ...], str]
  50. """
  51. A simple 2D drawing interface for PIL images.
  52. <p>
  53. Application code should use the <b>Draw</b> factory, instead of
  54. directly.
  55. """
  56. class ImageDraw:
  57. font: (
  58. ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont | None
  59. ) = None
  60. def __init__(self, im: Image.Image, mode: str | None = None) -> None:
  61. """
  62. Create a drawing instance.
  63. :param im: The image to draw in.
  64. :param mode: Optional mode to use for color values. For RGB
  65. images, this argument can be RGB or RGBA (to blend the
  66. drawing into the image). For all other modes, this argument
  67. must be the same as the image mode. If omitted, the mode
  68. defaults to the mode of the image.
  69. """
  70. im.load()
  71. if im.readonly:
  72. im._copy() # make it writeable
  73. blend = 0
  74. if mode is None:
  75. mode = im.mode
  76. if mode != im.mode:
  77. if mode == "RGBA" and im.mode == "RGB":
  78. blend = 1
  79. else:
  80. msg = "mode mismatch"
  81. raise ValueError(msg)
  82. if mode == "P":
  83. self.palette = im.palette
  84. else:
  85. self.palette = None
  86. self._image = im
  87. self.im = im.im
  88. self.draw = Image.core.draw(self.im, blend)
  89. self.mode = mode
  90. if mode in ("I", "F"):
  91. self.ink = self.draw.draw_ink(1)
  92. else:
  93. self.ink = self.draw.draw_ink(-1)
  94. if mode in ("1", "P", "I", "F"):
  95. # FIXME: fix Fill2 to properly support matte for I+F images
  96. self.fontmode = "1"
  97. else:
  98. self.fontmode = "L" # aliasing is okay for other modes
  99. self.fill = False
  100. def getfont(
  101. self,
  102. ) -> ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont:
  103. """
  104. Get the current default font.
  105. To set the default font for this ImageDraw instance::
  106. from PIL import ImageDraw, ImageFont
  107. draw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
  108. To set the default font for all future ImageDraw instances::
  109. from PIL import ImageDraw, ImageFont
  110. ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
  111. If the current default font is ``None``,
  112. it is initialized with ``ImageFont.load_default()``.
  113. :returns: An image font."""
  114. if not self.font:
  115. # FIXME: should add a font repository
  116. from . import ImageFont
  117. self.font = ImageFont.load_default()
  118. return self.font
  119. def _getfont(
  120. self, font_size: float | None
  121. ) -> ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont:
  122. if font_size is not None:
  123. from . import ImageFont
  124. return ImageFont.load_default(font_size)
  125. else:
  126. return self.getfont()
  127. def _getink(
  128. self, ink: _Ink | None, fill: _Ink | None = None
  129. ) -> tuple[int | None, int | None]:
  130. result_ink = None
  131. result_fill = None
  132. if ink is None and fill is None:
  133. if self.fill:
  134. result_fill = self.ink
  135. else:
  136. result_ink = self.ink
  137. else:
  138. if ink is not None:
  139. if isinstance(ink, str):
  140. ink = ImageColor.getcolor(ink, self.mode)
  141. if self.palette and isinstance(ink, tuple):
  142. ink = self.palette.getcolor(ink, self._image)
  143. result_ink = self.draw.draw_ink(ink)
  144. if fill is not None:
  145. if isinstance(fill, str):
  146. fill = ImageColor.getcolor(fill, self.mode)
  147. if self.palette and isinstance(fill, tuple):
  148. fill = self.palette.getcolor(fill, self._image)
  149. result_fill = self.draw.draw_ink(fill)
  150. return result_ink, result_fill
  151. def arc(
  152. self,
  153. xy: Coords,
  154. start: float,
  155. end: float,
  156. fill: _Ink | None = None,
  157. width: int = 1,
  158. ) -> None:
  159. """Draw an arc."""
  160. ink, fill = self._getink(fill)
  161. if ink is not None:
  162. self.draw.draw_arc(xy, start, end, ink, width)
  163. def bitmap(
  164. self, xy: Sequence[int], bitmap: Image.Image, fill: _Ink | None = None
  165. ) -> None:
  166. """Draw a bitmap."""
  167. bitmap.load()
  168. ink, fill = self._getink(fill)
  169. if ink is None:
  170. ink = fill
  171. if ink is not None:
  172. self.draw.draw_bitmap(xy, bitmap.im, ink)
  173. def chord(
  174. self,
  175. xy: Coords,
  176. start: float,
  177. end: float,
  178. fill: _Ink | None = None,
  179. outline: _Ink | None = None,
  180. width: int = 1,
  181. ) -> None:
  182. """Draw a chord."""
  183. ink, fill_ink = self._getink(outline, fill)
  184. if fill_ink is not None:
  185. self.draw.draw_chord(xy, start, end, fill_ink, 1)
  186. if ink is not None and ink != fill_ink and width != 0:
  187. self.draw.draw_chord(xy, start, end, ink, 0, width)
  188. def ellipse(
  189. self,
  190. xy: Coords,
  191. fill: _Ink | None = None,
  192. outline: _Ink | None = None,
  193. width: int = 1,
  194. ) -> None:
  195. """Draw an ellipse."""
  196. ink, fill_ink = self._getink(outline, fill)
  197. if fill_ink is not None:
  198. self.draw.draw_ellipse(xy, fill_ink, 1)
  199. if ink is not None and ink != fill_ink and width != 0:
  200. self.draw.draw_ellipse(xy, ink, 0, width)
  201. def circle(
  202. self,
  203. xy: Sequence[float],
  204. radius: float,
  205. fill: _Ink | None = None,
  206. outline: _Ink | None = None,
  207. width: int = 1,
  208. ) -> None:
  209. """Draw a circle given center coordinates and a radius."""
  210. ellipse_xy = (xy[0] - radius, xy[1] - radius, xy[0] + radius, xy[1] + radius)
  211. self.ellipse(ellipse_xy, fill, outline, width)
  212. def line(
  213. self,
  214. xy: Coords,
  215. fill: _Ink | None = None,
  216. width: int = 0,
  217. joint: str | None = None,
  218. ) -> None:
  219. """Draw a line, or a connected sequence of line segments."""
  220. ink = self._getink(fill)[0]
  221. if ink is not None:
  222. self.draw.draw_lines(xy, ink, width)
  223. if joint == "curve" and width > 4:
  224. points: Sequence[Sequence[float]]
  225. if isinstance(xy[0], (list, tuple)):
  226. points = cast(Sequence[Sequence[float]], xy)
  227. else:
  228. points = [
  229. cast(Sequence[float], tuple(xy[i : i + 2]))
  230. for i in range(0, len(xy), 2)
  231. ]
  232. for i in range(1, len(points) - 1):
  233. point = points[i]
  234. angles = [
  235. math.degrees(math.atan2(end[0] - start[0], start[1] - end[1]))
  236. % 360
  237. for start, end in (
  238. (points[i - 1], point),
  239. (point, points[i + 1]),
  240. )
  241. ]
  242. if angles[0] == angles[1]:
  243. # This is a straight line, so no joint is required
  244. continue
  245. def coord_at_angle(
  246. coord: Sequence[float], angle: float
  247. ) -> tuple[float, ...]:
  248. x, y = coord
  249. angle -= 90
  250. distance = width / 2 - 1
  251. return tuple(
  252. p + (math.floor(p_d) if p_d > 0 else math.ceil(p_d))
  253. for p, p_d in (
  254. (x, distance * math.cos(math.radians(angle))),
  255. (y, distance * math.sin(math.radians(angle))),
  256. )
  257. )
  258. flipped = (
  259. angles[1] > angles[0] and angles[1] - 180 > angles[0]
  260. ) or (angles[1] < angles[0] and angles[1] + 180 > angles[0])
  261. coords = [
  262. (point[0] - width / 2 + 1, point[1] - width / 2 + 1),
  263. (point[0] + width / 2 - 1, point[1] + width / 2 - 1),
  264. ]
  265. if flipped:
  266. start, end = (angles[1] + 90, angles[0] + 90)
  267. else:
  268. start, end = (angles[0] - 90, angles[1] - 90)
  269. self.pieslice(coords, start - 90, end - 90, fill)
  270. if width > 8:
  271. # Cover potential gaps between the line and the joint
  272. if flipped:
  273. gap_coords = [
  274. coord_at_angle(point, angles[0] + 90),
  275. point,
  276. coord_at_angle(point, angles[1] + 90),
  277. ]
  278. else:
  279. gap_coords = [
  280. coord_at_angle(point, angles[0] - 90),
  281. point,
  282. coord_at_angle(point, angles[1] - 90),
  283. ]
  284. self.line(gap_coords, fill, width=3)
  285. def shape(
  286. self,
  287. shape: Image.core._Outline,
  288. fill: _Ink | None = None,
  289. outline: _Ink | None = None,
  290. ) -> None:
  291. """(Experimental) Draw a shape."""
  292. shape.close()
  293. ink, fill_ink = self._getink(outline, fill)
  294. if fill_ink is not None:
  295. self.draw.draw_outline(shape, fill_ink, 1)
  296. if ink is not None and ink != fill_ink:
  297. self.draw.draw_outline(shape, ink, 0)
  298. def pieslice(
  299. self,
  300. xy: Coords,
  301. start: float,
  302. end: float,
  303. fill: _Ink | None = None,
  304. outline: _Ink | None = None,
  305. width: int = 1,
  306. ) -> None:
  307. """Draw a pieslice."""
  308. ink, fill_ink = self._getink(outline, fill)
  309. if fill_ink is not None:
  310. self.draw.draw_pieslice(xy, start, end, fill_ink, 1)
  311. if ink is not None and ink != fill_ink and width != 0:
  312. self.draw.draw_pieslice(xy, start, end, ink, 0, width)
  313. def point(self, xy: Coords, fill: _Ink | None = None) -> None:
  314. """Draw one or more individual pixels."""
  315. ink, fill = self._getink(fill)
  316. if ink is not None:
  317. self.draw.draw_points(xy, ink)
  318. def polygon(
  319. self,
  320. xy: Coords,
  321. fill: _Ink | None = None,
  322. outline: _Ink | None = None,
  323. width: int = 1,
  324. ) -> None:
  325. """Draw a polygon."""
  326. ink, fill_ink = self._getink(outline, fill)
  327. if fill_ink is not None:
  328. self.draw.draw_polygon(xy, fill_ink, 1)
  329. if ink is not None and ink != fill_ink and width != 0:
  330. if width == 1:
  331. self.draw.draw_polygon(xy, ink, 0, width)
  332. elif self.im is not None:
  333. # To avoid expanding the polygon outwards,
  334. # use the fill as a mask
  335. mask = Image.new("1", self.im.size)
  336. mask_ink = self._getink(1)[0]
  337. fill_im = mask.copy()
  338. draw = Draw(fill_im)
  339. draw.draw.draw_polygon(xy, mask_ink, 1)
  340. ink_im = mask.copy()
  341. draw = Draw(ink_im)
  342. width = width * 2 - 1
  343. draw.draw.draw_polygon(xy, mask_ink, 0, width)
  344. mask.paste(ink_im, mask=fill_im)
  345. im = Image.new(self.mode, self.im.size)
  346. draw = Draw(im)
  347. draw.draw.draw_polygon(xy, ink, 0, width)
  348. self.im.paste(im.im, (0, 0) + im.size, mask.im)
  349. def regular_polygon(
  350. self,
  351. bounding_circle: Sequence[Sequence[float] | float],
  352. n_sides: int,
  353. rotation: float = 0,
  354. fill: _Ink | None = None,
  355. outline: _Ink | None = None,
  356. width: int = 1,
  357. ) -> None:
  358. """Draw a regular polygon."""
  359. xy = _compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
  360. self.polygon(xy, fill, outline, width)
  361. def rectangle(
  362. self,
  363. xy: Coords,
  364. fill: _Ink | None = None,
  365. outline: _Ink | None = None,
  366. width: int = 1,
  367. ) -> None:
  368. """Draw a rectangle."""
  369. ink, fill_ink = self._getink(outline, fill)
  370. if fill_ink is not None:
  371. self.draw.draw_rectangle(xy, fill_ink, 1)
  372. if ink is not None and ink != fill_ink and width != 0:
  373. self.draw.draw_rectangle(xy, ink, 0, width)
  374. def rounded_rectangle(
  375. self,
  376. xy: Coords,
  377. radius: float = 0,
  378. fill: _Ink | None = None,
  379. outline: _Ink | None = None,
  380. width: int = 1,
  381. *,
  382. corners: tuple[bool, bool, bool, bool] | None = None,
  383. ) -> None:
  384. """Draw a rounded rectangle."""
  385. if isinstance(xy[0], (list, tuple)):
  386. (x0, y0), (x1, y1) = cast(Sequence[Sequence[float]], xy)
  387. else:
  388. x0, y0, x1, y1 = cast(Sequence[float], xy)
  389. if x1 < x0:
  390. msg = "x1 must be greater than or equal to x0"
  391. raise ValueError(msg)
  392. if y1 < y0:
  393. msg = "y1 must be greater than or equal to y0"
  394. raise ValueError(msg)
  395. if corners is None:
  396. corners = (True, True, True, True)
  397. d = radius * 2
  398. x0 = round(x0)
  399. y0 = round(y0)
  400. x1 = round(x1)
  401. y1 = round(y1)
  402. full_x, full_y = False, False
  403. if all(corners):
  404. full_x = d >= x1 - x0 - 1
  405. if full_x:
  406. # The two left and two right corners are joined
  407. d = x1 - x0
  408. full_y = d >= y1 - y0 - 1
  409. if full_y:
  410. # The two top and two bottom corners are joined
  411. d = y1 - y0
  412. if full_x and full_y:
  413. # If all corners are joined, that is a circle
  414. return self.ellipse(xy, fill, outline, width)
  415. if d == 0 or not any(corners):
  416. # If the corners have no curve,
  417. # or there are no corners,
  418. # that is a rectangle
  419. return self.rectangle(xy, fill, outline, width)
  420. r = int(d // 2)
  421. ink, fill_ink = self._getink(outline, fill)
  422. def draw_corners(pieslice: bool) -> None:
  423. parts: tuple[tuple[tuple[float, float, float, float], int, int], ...]
  424. if full_x:
  425. # Draw top and bottom halves
  426. parts = (
  427. ((x0, y0, x0 + d, y0 + d), 180, 360),
  428. ((x0, y1 - d, x0 + d, y1), 0, 180),
  429. )
  430. elif full_y:
  431. # Draw left and right halves
  432. parts = (
  433. ((x0, y0, x0 + d, y0 + d), 90, 270),
  434. ((x1 - d, y0, x1, y0 + d), 270, 90),
  435. )
  436. else:
  437. # Draw four separate corners
  438. parts = tuple(
  439. part
  440. for i, part in enumerate(
  441. (
  442. ((x0, y0, x0 + d, y0 + d), 180, 270),
  443. ((x1 - d, y0, x1, y0 + d), 270, 360),
  444. ((x1 - d, y1 - d, x1, y1), 0, 90),
  445. ((x0, y1 - d, x0 + d, y1), 90, 180),
  446. )
  447. )
  448. if corners[i]
  449. )
  450. for part in parts:
  451. if pieslice:
  452. self.draw.draw_pieslice(*(part + (fill_ink, 1)))
  453. else:
  454. self.draw.draw_arc(*(part + (ink, width)))
  455. if fill_ink is not None:
  456. draw_corners(True)
  457. if full_x:
  458. self.draw.draw_rectangle((x0, y0 + r + 1, x1, y1 - r - 1), fill_ink, 1)
  459. elif x1 - r - 1 > x0 + r + 1:
  460. self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill_ink, 1)
  461. if not full_x and not full_y:
  462. left = [x0, y0, x0 + r, y1]
  463. if corners[0]:
  464. left[1] += r + 1
  465. if corners[3]:
  466. left[3] -= r + 1
  467. self.draw.draw_rectangle(left, fill_ink, 1)
  468. right = [x1 - r, y0, x1, y1]
  469. if corners[1]:
  470. right[1] += r + 1
  471. if corners[2]:
  472. right[3] -= r + 1
  473. self.draw.draw_rectangle(right, fill_ink, 1)
  474. if ink is not None and ink != fill_ink and width != 0:
  475. draw_corners(False)
  476. if not full_x:
  477. top = [x0, y0, x1, y0 + width - 1]
  478. if corners[0]:
  479. top[0] += r + 1
  480. if corners[1]:
  481. top[2] -= r + 1
  482. self.draw.draw_rectangle(top, ink, 1)
  483. bottom = [x0, y1 - width + 1, x1, y1]
  484. if corners[3]:
  485. bottom[0] += r + 1
  486. if corners[2]:
  487. bottom[2] -= r + 1
  488. self.draw.draw_rectangle(bottom, ink, 1)
  489. if not full_y:
  490. left = [x0, y0, x0 + width - 1, y1]
  491. if corners[0]:
  492. left[1] += r + 1
  493. if corners[3]:
  494. left[3] -= r + 1
  495. self.draw.draw_rectangle(left, ink, 1)
  496. right = [x1 - width + 1, y0, x1, y1]
  497. if corners[1]:
  498. right[1] += r + 1
  499. if corners[2]:
  500. right[3] -= r + 1
  501. self.draw.draw_rectangle(right, ink, 1)
  502. def _multiline_check(self, text: AnyStr) -> bool:
  503. split_character = "\n" if isinstance(text, str) else b"\n"
  504. return split_character in text
  505. def _multiline_split(self, text: AnyStr) -> list[AnyStr]:
  506. return text.split("\n" if isinstance(text, str) else b"\n")
  507. def _multiline_spacing(
  508. self,
  509. font: ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont,
  510. spacing: float,
  511. stroke_width: float,
  512. ) -> float:
  513. return (
  514. self.textbbox((0, 0), "A", font, stroke_width=stroke_width)[3]
  515. + stroke_width
  516. + spacing
  517. )
  518. def text(
  519. self,
  520. xy: tuple[float, float],
  521. text: AnyStr,
  522. fill: _Ink | None = None,
  523. font: (
  524. ImageFont.ImageFont
  525. | ImageFont.FreeTypeFont
  526. | ImageFont.TransposedFont
  527. | None
  528. ) = None,
  529. anchor: str | None = None,
  530. spacing: float = 4,
  531. align: str = "left",
  532. direction: str | None = None,
  533. features: list[str] | None = None,
  534. language: str | None = None,
  535. stroke_width: float = 0,
  536. stroke_fill: _Ink | None = None,
  537. embedded_color: bool = False,
  538. *args: Any,
  539. **kwargs: Any,
  540. ) -> None:
  541. """Draw text."""
  542. if embedded_color and self.mode not in ("RGB", "RGBA"):
  543. msg = "Embedded color supported only in RGB and RGBA modes"
  544. raise ValueError(msg)
  545. if font is None:
  546. font = self._getfont(kwargs.get("font_size"))
  547. if self._multiline_check(text):
  548. return self.multiline_text(
  549. xy,
  550. text,
  551. fill,
  552. font,
  553. anchor,
  554. spacing,
  555. align,
  556. direction,
  557. features,
  558. language,
  559. stroke_width,
  560. stroke_fill,
  561. embedded_color,
  562. )
  563. def getink(fill: _Ink | None) -> int:
  564. ink, fill_ink = self._getink(fill)
  565. if ink is None:
  566. assert fill_ink is not None
  567. return fill_ink
  568. return ink
  569. def draw_text(ink: int, stroke_width: float = 0) -> None:
  570. mode = self.fontmode
  571. if stroke_width == 0 and embedded_color:
  572. mode = "RGBA"
  573. coord = []
  574. for i in range(2):
  575. coord.append(int(xy[i]))
  576. start = (math.modf(xy[0])[0], math.modf(xy[1])[0])
  577. try:
  578. mask, offset = font.getmask2( # type: ignore[union-attr,misc]
  579. text,
  580. mode,
  581. direction=direction,
  582. features=features,
  583. language=language,
  584. stroke_width=stroke_width,
  585. anchor=anchor,
  586. ink=ink,
  587. start=start,
  588. *args,
  589. **kwargs,
  590. )
  591. coord = [coord[0] + offset[0], coord[1] + offset[1]]
  592. except AttributeError:
  593. try:
  594. mask = font.getmask( # type: ignore[misc]
  595. text,
  596. mode,
  597. direction,
  598. features,
  599. language,
  600. stroke_width,
  601. anchor,
  602. ink,
  603. start=start,
  604. *args,
  605. **kwargs,
  606. )
  607. except TypeError:
  608. mask = font.getmask(text)
  609. if mode == "RGBA":
  610. # font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
  611. # extract mask and set text alpha
  612. color, mask = mask, mask.getband(3)
  613. ink_alpha = struct.pack("i", ink)[3]
  614. color.fillband(3, ink_alpha)
  615. x, y = coord
  616. if self.im is not None:
  617. self.im.paste(
  618. color, (x, y, x + mask.size[0], y + mask.size[1]), mask
  619. )
  620. else:
  621. self.draw.draw_bitmap(coord, mask, ink)
  622. ink = getink(fill)
  623. if ink is not None:
  624. stroke_ink = None
  625. if stroke_width:
  626. stroke_ink = getink(stroke_fill) if stroke_fill is not None else ink
  627. if stroke_ink is not None:
  628. # Draw stroked text
  629. draw_text(stroke_ink, stroke_width)
  630. # Draw normal text
  631. draw_text(ink, 0)
  632. else:
  633. # Only draw normal text
  634. draw_text(ink)
  635. def multiline_text(
  636. self,
  637. xy: tuple[float, float],
  638. text: AnyStr,
  639. fill: _Ink | None = None,
  640. font: (
  641. ImageFont.ImageFont
  642. | ImageFont.FreeTypeFont
  643. | ImageFont.TransposedFont
  644. | None
  645. ) = None,
  646. anchor: str | None = None,
  647. spacing: float = 4,
  648. align: str = "left",
  649. direction: str | None = None,
  650. features: list[str] | None = None,
  651. language: str | None = None,
  652. stroke_width: float = 0,
  653. stroke_fill: _Ink | None = None,
  654. embedded_color: bool = False,
  655. *,
  656. font_size: float | None = None,
  657. ) -> None:
  658. if direction == "ttb":
  659. msg = "ttb direction is unsupported for multiline text"
  660. raise ValueError(msg)
  661. if anchor is None:
  662. anchor = "la"
  663. elif len(anchor) != 2:
  664. msg = "anchor must be a 2 character string"
  665. raise ValueError(msg)
  666. elif anchor[1] in "tb":
  667. msg = "anchor not supported for multiline text"
  668. raise ValueError(msg)
  669. if font is None:
  670. font = self._getfont(font_size)
  671. widths = []
  672. max_width: float = 0
  673. lines = self._multiline_split(text)
  674. line_spacing = self._multiline_spacing(font, spacing, stroke_width)
  675. for line in lines:
  676. line_width = self.textlength(
  677. line, font, direction=direction, features=features, language=language
  678. )
  679. widths.append(line_width)
  680. max_width = max(max_width, line_width)
  681. top = xy[1]
  682. if anchor[1] == "m":
  683. top -= (len(lines) - 1) * line_spacing / 2.0
  684. elif anchor[1] == "d":
  685. top -= (len(lines) - 1) * line_spacing
  686. for idx, line in enumerate(lines):
  687. left = xy[0]
  688. width_difference = max_width - widths[idx]
  689. # first align left by anchor
  690. if anchor[0] == "m":
  691. left -= width_difference / 2.0
  692. elif anchor[0] == "r":
  693. left -= width_difference
  694. # then align by align parameter
  695. if align == "left":
  696. pass
  697. elif align == "center":
  698. left += width_difference / 2.0
  699. elif align == "right":
  700. left += width_difference
  701. else:
  702. msg = 'align must be "left", "center" or "right"'
  703. raise ValueError(msg)
  704. self.text(
  705. (left, top),
  706. line,
  707. fill,
  708. font,
  709. anchor,
  710. direction=direction,
  711. features=features,
  712. language=language,
  713. stroke_width=stroke_width,
  714. stroke_fill=stroke_fill,
  715. embedded_color=embedded_color,
  716. )
  717. top += line_spacing
  718. def textlength(
  719. self,
  720. text: AnyStr,
  721. font: (
  722. ImageFont.ImageFont
  723. | ImageFont.FreeTypeFont
  724. | ImageFont.TransposedFont
  725. | None
  726. ) = None,
  727. direction: str | None = None,
  728. features: list[str] | None = None,
  729. language: str | None = None,
  730. embedded_color: bool = False,
  731. *,
  732. font_size: float | None = None,
  733. ) -> float:
  734. """Get the length of a given string, in pixels with 1/64 precision."""
  735. if self._multiline_check(text):
  736. msg = "can't measure length of multiline text"
  737. raise ValueError(msg)
  738. if embedded_color and self.mode not in ("RGB", "RGBA"):
  739. msg = "Embedded color supported only in RGB and RGBA modes"
  740. raise ValueError(msg)
  741. if font is None:
  742. font = self._getfont(font_size)
  743. mode = "RGBA" if embedded_color else self.fontmode
  744. return font.getlength(text, mode, direction, features, language)
  745. def textbbox(
  746. self,
  747. xy: tuple[float, float],
  748. text: AnyStr,
  749. font: (
  750. ImageFont.ImageFont
  751. | ImageFont.FreeTypeFont
  752. | ImageFont.TransposedFont
  753. | None
  754. ) = None,
  755. anchor: str | None = None,
  756. spacing: float = 4,
  757. align: str = "left",
  758. direction: str | None = None,
  759. features: list[str] | None = None,
  760. language: str | None = None,
  761. stroke_width: float = 0,
  762. embedded_color: bool = False,
  763. *,
  764. font_size: float | None = None,
  765. ) -> tuple[float, float, float, float]:
  766. """Get the bounding box of a given string, in pixels."""
  767. if embedded_color and self.mode not in ("RGB", "RGBA"):
  768. msg = "Embedded color supported only in RGB and RGBA modes"
  769. raise ValueError(msg)
  770. if font is None:
  771. font = self._getfont(font_size)
  772. if self._multiline_check(text):
  773. return self.multiline_textbbox(
  774. xy,
  775. text,
  776. font,
  777. anchor,
  778. spacing,
  779. align,
  780. direction,
  781. features,
  782. language,
  783. stroke_width,
  784. embedded_color,
  785. )
  786. mode = "RGBA" if embedded_color else self.fontmode
  787. bbox = font.getbbox(
  788. text, mode, direction, features, language, stroke_width, anchor
  789. )
  790. return bbox[0] + xy[0], bbox[1] + xy[1], bbox[2] + xy[0], bbox[3] + xy[1]
  791. def multiline_textbbox(
  792. self,
  793. xy: tuple[float, float],
  794. text: AnyStr,
  795. font: (
  796. ImageFont.ImageFont
  797. | ImageFont.FreeTypeFont
  798. | ImageFont.TransposedFont
  799. | None
  800. ) = None,
  801. anchor: str | None = None,
  802. spacing: float = 4,
  803. align: str = "left",
  804. direction: str | None = None,
  805. features: list[str] | None = None,
  806. language: str | None = None,
  807. stroke_width: float = 0,
  808. embedded_color: bool = False,
  809. *,
  810. font_size: float | None = None,
  811. ) -> tuple[float, float, float, float]:
  812. if direction == "ttb":
  813. msg = "ttb direction is unsupported for multiline text"
  814. raise ValueError(msg)
  815. if anchor is None:
  816. anchor = "la"
  817. elif len(anchor) != 2:
  818. msg = "anchor must be a 2 character string"
  819. raise ValueError(msg)
  820. elif anchor[1] in "tb":
  821. msg = "anchor not supported for multiline text"
  822. raise ValueError(msg)
  823. if font is None:
  824. font = self._getfont(font_size)
  825. widths = []
  826. max_width: float = 0
  827. lines = self._multiline_split(text)
  828. line_spacing = self._multiline_spacing(font, spacing, stroke_width)
  829. for line in lines:
  830. line_width = self.textlength(
  831. line,
  832. font,
  833. direction=direction,
  834. features=features,
  835. language=language,
  836. embedded_color=embedded_color,
  837. )
  838. widths.append(line_width)
  839. max_width = max(max_width, line_width)
  840. top = xy[1]
  841. if anchor[1] == "m":
  842. top -= (len(lines) - 1) * line_spacing / 2.0
  843. elif anchor[1] == "d":
  844. top -= (len(lines) - 1) * line_spacing
  845. bbox: tuple[float, float, float, float] | None = None
  846. for idx, line in enumerate(lines):
  847. left = xy[0]
  848. width_difference = max_width - widths[idx]
  849. # first align left by anchor
  850. if anchor[0] == "m":
  851. left -= width_difference / 2.0
  852. elif anchor[0] == "r":
  853. left -= width_difference
  854. # then align by align parameter
  855. if align == "left":
  856. pass
  857. elif align == "center":
  858. left += width_difference / 2.0
  859. elif align == "right":
  860. left += width_difference
  861. else:
  862. msg = 'align must be "left", "center" or "right"'
  863. raise ValueError(msg)
  864. bbox_line = self.textbbox(
  865. (left, top),
  866. line,
  867. font,
  868. anchor,
  869. direction=direction,
  870. features=features,
  871. language=language,
  872. stroke_width=stroke_width,
  873. embedded_color=embedded_color,
  874. )
  875. if bbox is None:
  876. bbox = bbox_line
  877. else:
  878. bbox = (
  879. min(bbox[0], bbox_line[0]),
  880. min(bbox[1], bbox_line[1]),
  881. max(bbox[2], bbox_line[2]),
  882. max(bbox[3], bbox_line[3]),
  883. )
  884. top += line_spacing
  885. if bbox is None:
  886. return xy[0], xy[1], xy[0], xy[1]
  887. return bbox
  888. def Draw(im: Image.Image, mode: str | None = None) -> ImageDraw:
  889. """
  890. A simple 2D drawing interface for PIL images.
  891. :param im: The image to draw in.
  892. :param mode: Optional mode to use for color values. For RGB
  893. images, this argument can be RGB or RGBA (to blend the
  894. drawing into the image). For all other modes, this argument
  895. must be the same as the image mode. If omitted, the mode
  896. defaults to the mode of the image.
  897. """
  898. try:
  899. return getattr(im, "getdraw")(mode)
  900. except AttributeError:
  901. return ImageDraw(im, mode)
  902. def getdraw(
  903. im: Image.Image | None = None, hints: list[str] | None = None
  904. ) -> tuple[ImageDraw2.Draw | None, ModuleType]:
  905. """
  906. :param im: The image to draw in.
  907. :param hints: An optional list of hints. Deprecated.
  908. :returns: A (drawing context, drawing resource factory) tuple.
  909. """
  910. if hints is not None:
  911. deprecate("'hints' parameter", 12)
  912. from . import ImageDraw2
  913. draw = ImageDraw2.Draw(im) if im is not None else None
  914. return draw, ImageDraw2
  915. def floodfill(
  916. image: Image.Image,
  917. xy: tuple[int, int],
  918. value: float | tuple[int, ...],
  919. border: float | tuple[int, ...] | None = None,
  920. thresh: float = 0,
  921. ) -> None:
  922. """
  923. .. warning:: This method is experimental.
  924. Fills a bounded region with a given color.
  925. :param image: Target image.
  926. :param xy: Seed position (a 2-item coordinate tuple). See
  927. :ref:`coordinate-system`.
  928. :param value: Fill color.
  929. :param border: Optional border value. If given, the region consists of
  930. pixels with a color different from the border color. If not given,
  931. the region consists of pixels having the same color as the seed
  932. pixel.
  933. :param thresh: Optional threshold value which specifies a maximum
  934. tolerable difference of a pixel value from the 'background' in
  935. order for it to be replaced. Useful for filling regions of
  936. non-homogeneous, but similar, colors.
  937. """
  938. # based on an implementation by Eric S. Raymond
  939. # amended by yo1995 @20180806
  940. pixel = image.load()
  941. assert pixel is not None
  942. x, y = xy
  943. try:
  944. background = pixel[x, y]
  945. if _color_diff(value, background) <= thresh:
  946. return # seed point already has fill color
  947. pixel[x, y] = value
  948. except (ValueError, IndexError):
  949. return # seed point outside image
  950. edge = {(x, y)}
  951. # use a set to keep record of current and previous edge pixels
  952. # to reduce memory consumption
  953. full_edge = set()
  954. while edge:
  955. new_edge = set()
  956. for x, y in edge: # 4 adjacent method
  957. for s, t in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
  958. # If already processed, or if a coordinate is negative, skip
  959. if (s, t) in full_edge or s < 0 or t < 0:
  960. continue
  961. try:
  962. p = pixel[s, t]
  963. except (ValueError, IndexError):
  964. pass
  965. else:
  966. full_edge.add((s, t))
  967. if border is None:
  968. fill = _color_diff(p, background) <= thresh
  969. else:
  970. fill = p not in (value, border)
  971. if fill:
  972. pixel[s, t] = value
  973. new_edge.add((s, t))
  974. full_edge = edge # discard pixels processed
  975. edge = new_edge
  976. def _compute_regular_polygon_vertices(
  977. bounding_circle: Sequence[Sequence[float] | float], n_sides: int, rotation: float
  978. ) -> list[tuple[float, float]]:
  979. """
  980. Generate a list of vertices for a 2D regular polygon.
  981. :param bounding_circle: The bounding circle is a sequence defined
  982. by a point and radius. The polygon is inscribed in this circle.
  983. (e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)
  984. :param n_sides: Number of sides
  985. (e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)
  986. :param rotation: Apply an arbitrary rotation to the polygon
  987. (e.g. ``rotation=90``, applies a 90 degree rotation)
  988. :return: List of regular polygon vertices
  989. (e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)
  990. How are the vertices computed?
  991. 1. Compute the following variables
  992. - theta: Angle between the apothem & the nearest polygon vertex
  993. - side_length: Length of each polygon edge
  994. - centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)
  995. - polygon_radius: Polygon radius (last element of bounding_circle)
  996. - angles: Location of each polygon vertex in polar grid
  997. (e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])
  998. 2. For each angle in angles, get the polygon vertex at that angle
  999. The vertex is computed using the equation below.
  1000. X= xcos(φ) + ysin(φ)
  1001. Y= −xsin(φ) + ycos(φ)
  1002. Note:
  1003. φ = angle in degrees
  1004. x = 0
  1005. y = polygon_radius
  1006. The formula above assumes rotation around the origin.
  1007. In our case, we are rotating around the centroid.
  1008. To account for this, we use the formula below
  1009. X = xcos(φ) + ysin(φ) + centroid_x
  1010. Y = −xsin(φ) + ycos(φ) + centroid_y
  1011. """
  1012. # 1. Error Handling
  1013. # 1.1 Check `n_sides` has an appropriate value
  1014. if not isinstance(n_sides, int):
  1015. msg = "n_sides should be an int" # type: ignore[unreachable]
  1016. raise TypeError(msg)
  1017. if n_sides < 3:
  1018. msg = "n_sides should be an int > 2"
  1019. raise ValueError(msg)
  1020. # 1.2 Check `bounding_circle` has an appropriate value
  1021. if not isinstance(bounding_circle, (list, tuple)):
  1022. msg = "bounding_circle should be a sequence"
  1023. raise TypeError(msg)
  1024. if len(bounding_circle) == 3:
  1025. if not all(isinstance(i, (int, float)) for i in bounding_circle):
  1026. msg = "bounding_circle should only contain numeric data"
  1027. raise ValueError(msg)
  1028. *centroid, polygon_radius = cast(list[float], list(bounding_circle))
  1029. elif len(bounding_circle) == 2 and isinstance(bounding_circle[0], (list, tuple)):
  1030. if not all(
  1031. isinstance(i, (int, float)) for i in bounding_circle[0]
  1032. ) or not isinstance(bounding_circle[1], (int, float)):
  1033. msg = "bounding_circle should only contain numeric data"
  1034. raise ValueError(msg)
  1035. if len(bounding_circle[0]) != 2:
  1036. msg = "bounding_circle centre should contain 2D coordinates (e.g. (x, y))"
  1037. raise ValueError(msg)
  1038. centroid = cast(list[float], list(bounding_circle[0]))
  1039. polygon_radius = cast(float, bounding_circle[1])
  1040. else:
  1041. msg = (
  1042. "bounding_circle should contain 2D coordinates "
  1043. "and a radius (e.g. (x, y, r) or ((x, y), r) )"
  1044. )
  1045. raise ValueError(msg)
  1046. if polygon_radius <= 0:
  1047. msg = "bounding_circle radius should be > 0"
  1048. raise ValueError(msg)
  1049. # 1.3 Check `rotation` has an appropriate value
  1050. if not isinstance(rotation, (int, float)):
  1051. msg = "rotation should be an int or float" # type: ignore[unreachable]
  1052. raise ValueError(msg)
  1053. # 2. Define Helper Functions
  1054. def _apply_rotation(point: list[float], degrees: float) -> tuple[float, float]:
  1055. return (
  1056. round(
  1057. point[0] * math.cos(math.radians(360 - degrees))
  1058. - point[1] * math.sin(math.radians(360 - degrees))
  1059. + centroid[0],
  1060. 2,
  1061. ),
  1062. round(
  1063. point[1] * math.cos(math.radians(360 - degrees))
  1064. + point[0] * math.sin(math.radians(360 - degrees))
  1065. + centroid[1],
  1066. 2,
  1067. ),
  1068. )
  1069. def _compute_polygon_vertex(angle: float) -> tuple[float, float]:
  1070. start_point = [polygon_radius, 0]
  1071. return _apply_rotation(start_point, angle)
  1072. def _get_angles(n_sides: int, rotation: float) -> list[float]:
  1073. angles = []
  1074. degrees = 360 / n_sides
  1075. # Start with the bottom left polygon vertex
  1076. current_angle = (270 - 0.5 * degrees) + rotation
  1077. for _ in range(0, n_sides):
  1078. angles.append(current_angle)
  1079. current_angle += degrees
  1080. if current_angle > 360:
  1081. current_angle -= 360
  1082. return angles
  1083. # 3. Variable Declarations
  1084. angles = _get_angles(n_sides, rotation)
  1085. # 4. Compute Vertices
  1086. return [_compute_polygon_vertex(angle) for angle in angles]
  1087. def _color_diff(
  1088. color1: float | tuple[int, ...], color2: float | tuple[int, ...]
  1089. ) -> float:
  1090. """
  1091. Uses 1-norm distance to calculate difference between two values.
  1092. """
  1093. first = color1 if isinstance(color1, tuple) else (color1,)
  1094. second = color2 if isinstance(color2, tuple) else (color2,)
  1095. return sum(abs(first[i] - second[i]) for i in range(0, len(second)))