ImageDraw.py 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 numbers
  35. import struct
  36. from types import ModuleType
  37. from typing import TYPE_CHECKING, AnyStr, Callable, List, Sequence, Tuple, 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 not isinstance(ink, numbers.Number):
  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 not isinstance(fill, numbers.Number):
  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. else:
  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(self, font, spacing, stroke_width):
  508. return (
  509. self.textbbox((0, 0), "A", font, stroke_width=stroke_width)[3]
  510. + stroke_width
  511. + spacing
  512. )
  513. def text(
  514. self,
  515. xy: tuple[float, float],
  516. text: str,
  517. fill=None,
  518. font: (
  519. ImageFont.ImageFont
  520. | ImageFont.FreeTypeFont
  521. | ImageFont.TransposedFont
  522. | None
  523. ) = None,
  524. anchor=None,
  525. spacing=4,
  526. align="left",
  527. direction=None,
  528. features=None,
  529. language=None,
  530. stroke_width=0,
  531. stroke_fill=None,
  532. embedded_color=False,
  533. *args,
  534. **kwargs,
  535. ) -> None:
  536. """Draw text."""
  537. if embedded_color and self.mode not in ("RGB", "RGBA"):
  538. msg = "Embedded color supported only in RGB and RGBA modes"
  539. raise ValueError(msg)
  540. if font is None:
  541. font = self._getfont(kwargs.get("font_size"))
  542. if self._multiline_check(text):
  543. return self.multiline_text(
  544. xy,
  545. text,
  546. fill,
  547. font,
  548. anchor,
  549. spacing,
  550. align,
  551. direction,
  552. features,
  553. language,
  554. stroke_width,
  555. stroke_fill,
  556. embedded_color,
  557. )
  558. def getink(fill: _Ink | None) -> int:
  559. ink, fill_ink = self._getink(fill)
  560. if ink is None:
  561. assert fill_ink is not None
  562. return fill_ink
  563. return ink
  564. def draw_text(ink, stroke_width=0, stroke_offset=None) -> None:
  565. mode = self.fontmode
  566. if stroke_width == 0 and embedded_color:
  567. mode = "RGBA"
  568. coord = []
  569. start = []
  570. for i in range(2):
  571. coord.append(int(xy[i]))
  572. start.append(math.modf(xy[i])[0])
  573. try:
  574. mask, offset = font.getmask2( # type: ignore[union-attr,misc]
  575. text,
  576. mode,
  577. direction=direction,
  578. features=features,
  579. language=language,
  580. stroke_width=stroke_width,
  581. anchor=anchor,
  582. ink=ink,
  583. start=start,
  584. *args,
  585. **kwargs,
  586. )
  587. coord = [coord[0] + offset[0], coord[1] + offset[1]]
  588. except AttributeError:
  589. try:
  590. mask = font.getmask( # type: ignore[misc]
  591. text,
  592. mode,
  593. direction,
  594. features,
  595. language,
  596. stroke_width,
  597. anchor,
  598. ink,
  599. start=start,
  600. *args,
  601. **kwargs,
  602. )
  603. except TypeError:
  604. mask = font.getmask(text)
  605. if stroke_offset:
  606. coord = [coord[0] + stroke_offset[0], coord[1] + stroke_offset[1]]
  607. if mode == "RGBA":
  608. # font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
  609. # extract mask and set text alpha
  610. color, mask = mask, mask.getband(3)
  611. ink_alpha = struct.pack("i", ink)[3]
  612. color.fillband(3, ink_alpha)
  613. x, y = coord
  614. if self.im is not None:
  615. self.im.paste(
  616. color, (x, y, x + mask.size[0], y + mask.size[1]), mask
  617. )
  618. else:
  619. self.draw.draw_bitmap(coord, mask, ink)
  620. ink = getink(fill)
  621. if ink is not None:
  622. stroke_ink = None
  623. if stroke_width:
  624. stroke_ink = getink(stroke_fill) if stroke_fill is not None else ink
  625. if stroke_ink is not None:
  626. # Draw stroked text
  627. draw_text(stroke_ink, stroke_width)
  628. # Draw normal text
  629. draw_text(ink, 0)
  630. else:
  631. # Only draw normal text
  632. draw_text(ink)
  633. def multiline_text(
  634. self,
  635. xy: tuple[float, float],
  636. text: str,
  637. fill=None,
  638. font: (
  639. ImageFont.ImageFont
  640. | ImageFont.FreeTypeFont
  641. | ImageFont.TransposedFont
  642. | None
  643. ) = None,
  644. anchor=None,
  645. spacing=4,
  646. align="left",
  647. direction=None,
  648. features=None,
  649. language=None,
  650. stroke_width=0,
  651. stroke_fill=None,
  652. embedded_color=False,
  653. *,
  654. font_size=None,
  655. ) -> None:
  656. if direction == "ttb":
  657. msg = "ttb direction is unsupported for multiline text"
  658. raise ValueError(msg)
  659. if anchor is None:
  660. anchor = "la"
  661. elif len(anchor) != 2:
  662. msg = "anchor must be a 2 character string"
  663. raise ValueError(msg)
  664. elif anchor[1] in "tb":
  665. msg = "anchor not supported for multiline text"
  666. raise ValueError(msg)
  667. if font is None:
  668. font = self._getfont(font_size)
  669. widths = []
  670. max_width: float = 0
  671. lines = self._multiline_split(text)
  672. line_spacing = self._multiline_spacing(font, spacing, stroke_width)
  673. for line in lines:
  674. line_width = self.textlength(
  675. line, font, direction=direction, features=features, language=language
  676. )
  677. widths.append(line_width)
  678. max_width = max(max_width, line_width)
  679. top = xy[1]
  680. if anchor[1] == "m":
  681. top -= (len(lines) - 1) * line_spacing / 2.0
  682. elif anchor[1] == "d":
  683. top -= (len(lines) - 1) * line_spacing
  684. for idx, line in enumerate(lines):
  685. left = xy[0]
  686. width_difference = max_width - widths[idx]
  687. # first align left by anchor
  688. if anchor[0] == "m":
  689. left -= width_difference / 2.0
  690. elif anchor[0] == "r":
  691. left -= width_difference
  692. # then align by align parameter
  693. if align == "left":
  694. pass
  695. elif align == "center":
  696. left += width_difference / 2.0
  697. elif align == "right":
  698. left += width_difference
  699. else:
  700. msg = 'align must be "left", "center" or "right"'
  701. raise ValueError(msg)
  702. self.text(
  703. (left, top),
  704. line,
  705. fill,
  706. font,
  707. anchor,
  708. direction=direction,
  709. features=features,
  710. language=language,
  711. stroke_width=stroke_width,
  712. stroke_fill=stroke_fill,
  713. embedded_color=embedded_color,
  714. )
  715. top += line_spacing
  716. def textlength(
  717. self,
  718. text: str,
  719. font: (
  720. ImageFont.ImageFont
  721. | ImageFont.FreeTypeFont
  722. | ImageFont.TransposedFont
  723. | None
  724. ) = None,
  725. direction=None,
  726. features=None,
  727. language=None,
  728. embedded_color=False,
  729. *,
  730. font_size=None,
  731. ) -> float:
  732. """Get the length of a given string, in pixels with 1/64 precision."""
  733. if self._multiline_check(text):
  734. msg = "can't measure length of multiline text"
  735. raise ValueError(msg)
  736. if embedded_color and self.mode not in ("RGB", "RGBA"):
  737. msg = "Embedded color supported only in RGB and RGBA modes"
  738. raise ValueError(msg)
  739. if font is None:
  740. font = self._getfont(font_size)
  741. mode = "RGBA" if embedded_color else self.fontmode
  742. return font.getlength(text, mode, direction, features, language)
  743. def textbbox(
  744. self,
  745. xy,
  746. text,
  747. font=None,
  748. anchor=None,
  749. spacing=4,
  750. align="left",
  751. direction=None,
  752. features=None,
  753. language=None,
  754. stroke_width=0,
  755. embedded_color=False,
  756. *,
  757. font_size=None,
  758. ) -> tuple[int, int, int, int]:
  759. """Get the bounding box of a given string, in pixels."""
  760. if embedded_color and self.mode not in ("RGB", "RGBA"):
  761. msg = "Embedded color supported only in RGB and RGBA modes"
  762. raise ValueError(msg)
  763. if font is None:
  764. font = self._getfont(font_size)
  765. if self._multiline_check(text):
  766. return self.multiline_textbbox(
  767. xy,
  768. text,
  769. font,
  770. anchor,
  771. spacing,
  772. align,
  773. direction,
  774. features,
  775. language,
  776. stroke_width,
  777. embedded_color,
  778. )
  779. mode = "RGBA" if embedded_color else self.fontmode
  780. bbox = font.getbbox(
  781. text, mode, direction, features, language, stroke_width, anchor
  782. )
  783. return bbox[0] + xy[0], bbox[1] + xy[1], bbox[2] + xy[0], bbox[3] + xy[1]
  784. def multiline_textbbox(
  785. self,
  786. xy,
  787. text,
  788. font=None,
  789. anchor=None,
  790. spacing=4,
  791. align="left",
  792. direction=None,
  793. features=None,
  794. language=None,
  795. stroke_width=0,
  796. embedded_color=False,
  797. *,
  798. font_size=None,
  799. ) -> tuple[int, int, int, int]:
  800. if direction == "ttb":
  801. msg = "ttb direction is unsupported for multiline text"
  802. raise ValueError(msg)
  803. if anchor is None:
  804. anchor = "la"
  805. elif len(anchor) != 2:
  806. msg = "anchor must be a 2 character string"
  807. raise ValueError(msg)
  808. elif anchor[1] in "tb":
  809. msg = "anchor not supported for multiline text"
  810. raise ValueError(msg)
  811. if font is None:
  812. font = self._getfont(font_size)
  813. widths = []
  814. max_width: float = 0
  815. lines = self._multiline_split(text)
  816. line_spacing = self._multiline_spacing(font, spacing, stroke_width)
  817. for line in lines:
  818. line_width = self.textlength(
  819. line,
  820. font,
  821. direction=direction,
  822. features=features,
  823. language=language,
  824. embedded_color=embedded_color,
  825. )
  826. widths.append(line_width)
  827. max_width = max(max_width, line_width)
  828. top = xy[1]
  829. if anchor[1] == "m":
  830. top -= (len(lines) - 1) * line_spacing / 2.0
  831. elif anchor[1] == "d":
  832. top -= (len(lines) - 1) * line_spacing
  833. bbox: tuple[int, int, int, int] | None = None
  834. for idx, line in enumerate(lines):
  835. left = xy[0]
  836. width_difference = max_width - widths[idx]
  837. # first align left by anchor
  838. if anchor[0] == "m":
  839. left -= width_difference / 2.0
  840. elif anchor[0] == "r":
  841. left -= width_difference
  842. # then align by align parameter
  843. if align == "left":
  844. pass
  845. elif align == "center":
  846. left += width_difference / 2.0
  847. elif align == "right":
  848. left += width_difference
  849. else:
  850. msg = 'align must be "left", "center" or "right"'
  851. raise ValueError(msg)
  852. bbox_line = self.textbbox(
  853. (left, top),
  854. line,
  855. font,
  856. anchor,
  857. direction=direction,
  858. features=features,
  859. language=language,
  860. stroke_width=stroke_width,
  861. embedded_color=embedded_color,
  862. )
  863. if bbox is None:
  864. bbox = bbox_line
  865. else:
  866. bbox = (
  867. min(bbox[0], bbox_line[0]),
  868. min(bbox[1], bbox_line[1]),
  869. max(bbox[2], bbox_line[2]),
  870. max(bbox[3], bbox_line[3]),
  871. )
  872. top += line_spacing
  873. if bbox is None:
  874. return xy[0], xy[1], xy[0], xy[1]
  875. return bbox
  876. def Draw(im: Image.Image, mode: str | None = None) -> ImageDraw:
  877. """
  878. A simple 2D drawing interface for PIL images.
  879. :param im: The image to draw in.
  880. :param mode: Optional mode to use for color values. For RGB
  881. images, this argument can be RGB or RGBA (to blend the
  882. drawing into the image). For all other modes, this argument
  883. must be the same as the image mode. If omitted, the mode
  884. defaults to the mode of the image.
  885. """
  886. try:
  887. return getattr(im, "getdraw")(mode)
  888. except AttributeError:
  889. return ImageDraw(im, mode)
  890. def getdraw(
  891. im: Image.Image | None = None, hints: list[str] | None = None
  892. ) -> tuple[ImageDraw2.Draw | None, ModuleType]:
  893. """
  894. :param im: The image to draw in.
  895. :param hints: An optional list of hints. Deprecated.
  896. :returns: A (drawing context, drawing resource factory) tuple.
  897. """
  898. if hints is not None:
  899. deprecate("'hints' parameter", 12)
  900. from . import ImageDraw2
  901. draw = ImageDraw2.Draw(im) if im is not None else None
  902. return draw, ImageDraw2
  903. def floodfill(
  904. image: Image.Image,
  905. xy: tuple[int, int],
  906. value: float | tuple[int, ...],
  907. border: float | tuple[int, ...] | None = None,
  908. thresh: float = 0,
  909. ) -> None:
  910. """
  911. .. warning:: This method is experimental.
  912. Fills a bounded region with a given color.
  913. :param image: Target image.
  914. :param xy: Seed position (a 2-item coordinate tuple). See
  915. :ref:`coordinate-system`.
  916. :param value: Fill color.
  917. :param border: Optional border value. If given, the region consists of
  918. pixels with a color different from the border color. If not given,
  919. the region consists of pixels having the same color as the seed
  920. pixel.
  921. :param thresh: Optional threshold value which specifies a maximum
  922. tolerable difference of a pixel value from the 'background' in
  923. order for it to be replaced. Useful for filling regions of
  924. non-homogeneous, but similar, colors.
  925. """
  926. # based on an implementation by Eric S. Raymond
  927. # amended by yo1995 @20180806
  928. pixel = image.load()
  929. assert pixel is not None
  930. x, y = xy
  931. try:
  932. background = pixel[x, y]
  933. if _color_diff(value, background) <= thresh:
  934. return # seed point already has fill color
  935. pixel[x, y] = value
  936. except (ValueError, IndexError):
  937. return # seed point outside image
  938. edge = {(x, y)}
  939. # use a set to keep record of current and previous edge pixels
  940. # to reduce memory consumption
  941. full_edge = set()
  942. while edge:
  943. new_edge = set()
  944. for x, y in edge: # 4 adjacent method
  945. for s, t in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
  946. # If already processed, or if a coordinate is negative, skip
  947. if (s, t) in full_edge or s < 0 or t < 0:
  948. continue
  949. try:
  950. p = pixel[s, t]
  951. except (ValueError, IndexError):
  952. pass
  953. else:
  954. full_edge.add((s, t))
  955. if border is None:
  956. fill = _color_diff(p, background) <= thresh
  957. else:
  958. fill = p not in (value, border)
  959. if fill:
  960. pixel[s, t] = value
  961. new_edge.add((s, t))
  962. full_edge = edge # discard pixels processed
  963. edge = new_edge
  964. def _compute_regular_polygon_vertices(
  965. bounding_circle: Sequence[Sequence[float] | float], n_sides: int, rotation: float
  966. ) -> list[tuple[float, float]]:
  967. """
  968. Generate a list of vertices for a 2D regular polygon.
  969. :param bounding_circle: The bounding circle is a sequence defined
  970. by a point and radius. The polygon is inscribed in this circle.
  971. (e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)
  972. :param n_sides: Number of sides
  973. (e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)
  974. :param rotation: Apply an arbitrary rotation to the polygon
  975. (e.g. ``rotation=90``, applies a 90 degree rotation)
  976. :return: List of regular polygon vertices
  977. (e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)
  978. How are the vertices computed?
  979. 1. Compute the following variables
  980. - theta: Angle between the apothem & the nearest polygon vertex
  981. - side_length: Length of each polygon edge
  982. - centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)
  983. - polygon_radius: Polygon radius (last element of bounding_circle)
  984. - angles: Location of each polygon vertex in polar grid
  985. (e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])
  986. 2. For each angle in angles, get the polygon vertex at that angle
  987. The vertex is computed using the equation below.
  988. X= xcos(φ) + ysin(φ)
  989. Y= −xsin(φ) + ycos(φ)
  990. Note:
  991. φ = angle in degrees
  992. x = 0
  993. y = polygon_radius
  994. The formula above assumes rotation around the origin.
  995. In our case, we are rotating around the centroid.
  996. To account for this, we use the formula below
  997. X = xcos(φ) + ysin(φ) + centroid_x
  998. Y = −xsin(φ) + ycos(φ) + centroid_y
  999. """
  1000. # 1. Error Handling
  1001. # 1.1 Check `n_sides` has an appropriate value
  1002. if not isinstance(n_sides, int):
  1003. msg = "n_sides should be an int" # type: ignore[unreachable]
  1004. raise TypeError(msg)
  1005. if n_sides < 3:
  1006. msg = "n_sides should be an int > 2"
  1007. raise ValueError(msg)
  1008. # 1.2 Check `bounding_circle` has an appropriate value
  1009. if not isinstance(bounding_circle, (list, tuple)):
  1010. msg = "bounding_circle should be a sequence"
  1011. raise TypeError(msg)
  1012. if len(bounding_circle) == 3:
  1013. if not all(isinstance(i, (int, float)) for i in bounding_circle):
  1014. msg = "bounding_circle should only contain numeric data"
  1015. raise ValueError(msg)
  1016. *centroid, polygon_radius = cast(List[float], list(bounding_circle))
  1017. elif len(bounding_circle) == 2 and isinstance(bounding_circle[0], (list, tuple)):
  1018. if not all(
  1019. isinstance(i, (int, float)) for i in bounding_circle[0]
  1020. ) or not isinstance(bounding_circle[1], (int, float)):
  1021. msg = "bounding_circle should only contain numeric data"
  1022. raise ValueError(msg)
  1023. if len(bounding_circle[0]) != 2:
  1024. msg = "bounding_circle centre should contain 2D coordinates (e.g. (x, y))"
  1025. raise ValueError(msg)
  1026. centroid = cast(List[float], list(bounding_circle[0]))
  1027. polygon_radius = cast(float, bounding_circle[1])
  1028. else:
  1029. msg = (
  1030. "bounding_circle should contain 2D coordinates "
  1031. "and a radius (e.g. (x, y, r) or ((x, y), r) )"
  1032. )
  1033. raise ValueError(msg)
  1034. if polygon_radius <= 0:
  1035. msg = "bounding_circle radius should be > 0"
  1036. raise ValueError(msg)
  1037. # 1.3 Check `rotation` has an appropriate value
  1038. if not isinstance(rotation, (int, float)):
  1039. msg = "rotation should be an int or float" # type: ignore[unreachable]
  1040. raise ValueError(msg)
  1041. # 2. Define Helper Functions
  1042. def _apply_rotation(point: list[float], degrees: float) -> tuple[float, float]:
  1043. return (
  1044. round(
  1045. point[0] * math.cos(math.radians(360 - degrees))
  1046. - point[1] * math.sin(math.radians(360 - degrees))
  1047. + centroid[0],
  1048. 2,
  1049. ),
  1050. round(
  1051. point[1] * math.cos(math.radians(360 - degrees))
  1052. + point[0] * math.sin(math.radians(360 - degrees))
  1053. + centroid[1],
  1054. 2,
  1055. ),
  1056. )
  1057. def _compute_polygon_vertex(angle: float) -> tuple[float, float]:
  1058. start_point = [polygon_radius, 0]
  1059. return _apply_rotation(start_point, angle)
  1060. def _get_angles(n_sides: int, rotation: float) -> list[float]:
  1061. angles = []
  1062. degrees = 360 / n_sides
  1063. # Start with the bottom left polygon vertex
  1064. current_angle = (270 - 0.5 * degrees) + rotation
  1065. for _ in range(0, n_sides):
  1066. angles.append(current_angle)
  1067. current_angle += degrees
  1068. if current_angle > 360:
  1069. current_angle -= 360
  1070. return angles
  1071. # 3. Variable Declarations
  1072. angles = _get_angles(n_sides, rotation)
  1073. # 4. Compute Vertices
  1074. return [_compute_polygon_vertex(angle) for angle in angles]
  1075. def _color_diff(
  1076. color1: float | tuple[int, ...], color2: float | tuple[int, ...]
  1077. ) -> float:
  1078. """
  1079. Uses 1-norm distance to calculate difference between two values.
  1080. """
  1081. first = color1 if isinstance(color1, tuple) else (color1,)
  1082. second = color2 if isinstance(color2, tuple) else (color2,)
  1083. return sum(abs(first[i] - second[i]) for i in range(0, len(second)))