@ai-ecoverse/py-pillow 12.3.0-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (217) hide show
  1. package/LICENSE +30 -0
  2. package/README.md +4 -0
  3. package/lib/python3.14/site-packages/PIL/AvifImagePlugin.py +295 -0
  4. package/lib/python3.14/site-packages/PIL/BdfFontFile.py +124 -0
  5. package/lib/python3.14/site-packages/PIL/BlpImagePlugin.py +498 -0
  6. package/lib/python3.14/site-packages/PIL/BmpImagePlugin.py +514 -0
  7. package/lib/python3.14/site-packages/PIL/BufrStubImagePlugin.py +72 -0
  8. package/lib/python3.14/site-packages/PIL/ContainerIO.py +173 -0
  9. package/lib/python3.14/site-packages/PIL/CurImagePlugin.py +103 -0
  10. package/lib/python3.14/site-packages/PIL/DcxImagePlugin.py +84 -0
  11. package/lib/python3.14/site-packages/PIL/DdsImagePlugin.py +632 -0
  12. package/lib/python3.14/site-packages/PIL/EpsImagePlugin.py +484 -0
  13. package/lib/python3.14/site-packages/PIL/ExifTags.py +384 -0
  14. package/lib/python3.14/site-packages/PIL/FitsImagePlugin.py +153 -0
  15. package/lib/python3.14/site-packages/PIL/FliImagePlugin.py +184 -0
  16. package/lib/python3.14/site-packages/PIL/FontFile.py +160 -0
  17. package/lib/python3.14/site-packages/PIL/FpxImagePlugin.py +262 -0
  18. package/lib/python3.14/site-packages/PIL/FtexImagePlugin.py +115 -0
  19. package/lib/python3.14/site-packages/PIL/GbrImagePlugin.py +103 -0
  20. package/lib/python3.14/site-packages/PIL/GdImageFile.py +104 -0
  21. package/lib/python3.14/site-packages/PIL/GifImagePlugin.py +1223 -0
  22. package/lib/python3.14/site-packages/PIL/GimpGradientFile.py +154 -0
  23. package/lib/python3.14/site-packages/PIL/GimpPaletteFile.py +75 -0
  24. package/lib/python3.14/site-packages/PIL/GribStubImagePlugin.py +72 -0
  25. package/lib/python3.14/site-packages/PIL/Hdf5StubImagePlugin.py +72 -0
  26. package/lib/python3.14/site-packages/PIL/IcnsImagePlugin.py +401 -0
  27. package/lib/python3.14/site-packages/PIL/IcoImagePlugin.py +396 -0
  28. package/lib/python3.14/site-packages/PIL/ImImagePlugin.py +390 -0
  29. package/lib/python3.14/site-packages/PIL/Image.py +4385 -0
  30. package/lib/python3.14/site-packages/PIL/ImageChops.py +311 -0
  31. package/lib/python3.14/site-packages/PIL/ImageCms.py +1078 -0
  32. package/lib/python3.14/site-packages/PIL/ImageColor.py +320 -0
  33. package/lib/python3.14/site-packages/PIL/ImageDraw.py +1002 -0
  34. package/lib/python3.14/site-packages/PIL/ImageDraw2.py +244 -0
  35. package/lib/python3.14/site-packages/PIL/ImageEnhance.py +113 -0
  36. package/lib/python3.14/site-packages/PIL/ImageFile.py +938 -0
  37. package/lib/python3.14/site-packages/PIL/ImageFilter.py +617 -0
  38. package/lib/python3.14/site-packages/PIL/ImageFont.py +1330 -0
  39. package/lib/python3.14/site-packages/PIL/ImageGrab.py +234 -0
  40. package/lib/python3.14/site-packages/PIL/ImageMath.py +314 -0
  41. package/lib/python3.14/site-packages/PIL/ImageMode.py +85 -0
  42. package/lib/python3.14/site-packages/PIL/ImageMorph.py +317 -0
  43. package/lib/python3.14/site-packages/PIL/ImageOps.py +749 -0
  44. package/lib/python3.14/site-packages/PIL/ImagePalette.py +290 -0
  45. package/lib/python3.14/site-packages/PIL/ImagePath.py +20 -0
  46. package/lib/python3.14/site-packages/PIL/ImageQt.py +219 -0
  47. package/lib/python3.14/site-packages/PIL/ImageSequence.py +88 -0
  48. package/lib/python3.14/site-packages/PIL/ImageShow.py +367 -0
  49. package/lib/python3.14/site-packages/PIL/ImageStat.py +167 -0
  50. package/lib/python3.14/site-packages/PIL/ImageText.py +509 -0
  51. package/lib/python3.14/site-packages/PIL/ImageTk.py +266 -0
  52. package/lib/python3.14/site-packages/PIL/ImageTransform.py +136 -0
  53. package/lib/python3.14/site-packages/PIL/ImageWin.py +247 -0
  54. package/lib/python3.14/site-packages/PIL/ImtImagePlugin.py +103 -0
  55. package/lib/python3.14/site-packages/PIL/IptcImagePlugin.py +226 -0
  56. package/lib/python3.14/site-packages/PIL/Jpeg2KImagePlugin.py +466 -0
  57. package/lib/python3.14/site-packages/PIL/JpegImagePlugin.py +889 -0
  58. package/lib/python3.14/site-packages/PIL/JpegPresets.py +242 -0
  59. package/lib/python3.14/site-packages/PIL/McIdasImagePlugin.py +78 -0
  60. package/lib/python3.14/site-packages/PIL/MicImagePlugin.py +103 -0
  61. package/lib/python3.14/site-packages/PIL/MpegImagePlugin.py +84 -0
  62. package/lib/python3.14/site-packages/PIL/MpoImagePlugin.py +203 -0
  63. package/lib/python3.14/site-packages/PIL/MspImagePlugin.py +200 -0
  64. package/lib/python3.14/site-packages/PIL/PSDraw.py +238 -0
  65. package/lib/python3.14/site-packages/PIL/PaletteFile.py +54 -0
  66. package/lib/python3.14/site-packages/PIL/PalmImagePlugin.py +217 -0
  67. package/lib/python3.14/site-packages/PIL/PcdImagePlugin.py +68 -0
  68. package/lib/python3.14/site-packages/PIL/PcfFontFile.py +256 -0
  69. package/lib/python3.14/site-packages/PIL/PcxImagePlugin.py +232 -0
  70. package/lib/python3.14/site-packages/PIL/PdfImagePlugin.py +315 -0
  71. package/lib/python3.14/site-packages/PIL/PdfParser.py +1092 -0
  72. package/lib/python3.14/site-packages/PIL/PixarImagePlugin.py +72 -0
  73. package/lib/python3.14/site-packages/PIL/PngImagePlugin.py +1578 -0
  74. package/lib/python3.14/site-packages/PIL/PpmImagePlugin.py +375 -0
  75. package/lib/python3.14/site-packages/PIL/PsdImagePlugin.py +339 -0
  76. package/lib/python3.14/site-packages/PIL/QoiImagePlugin.py +235 -0
  77. package/lib/python3.14/site-packages/PIL/SgiImagePlugin.py +231 -0
  78. package/lib/python3.14/site-packages/PIL/SpiderImagePlugin.py +332 -0
  79. package/lib/python3.14/site-packages/PIL/SunImagePlugin.py +145 -0
  80. package/lib/python3.14/site-packages/PIL/TarIO.py +61 -0
  81. package/lib/python3.14/site-packages/PIL/TgaImagePlugin.py +284 -0
  82. package/lib/python3.14/site-packages/PIL/TiffImagePlugin.py +2354 -0
  83. package/lib/python3.14/site-packages/PIL/TiffTags.py +566 -0
  84. package/lib/python3.14/site-packages/PIL/WalImageFile.py +129 -0
  85. package/lib/python3.14/site-packages/PIL/WebPImagePlugin.py +322 -0
  86. package/lib/python3.14/site-packages/PIL/WmfImagePlugin.py +183 -0
  87. package/lib/python3.14/site-packages/PIL/XVThumbImagePlugin.py +83 -0
  88. package/lib/python3.14/site-packages/PIL/XbmImagePlugin.py +98 -0
  89. package/lib/python3.14/site-packages/PIL/XpmImagePlugin.py +157 -0
  90. package/lib/python3.14/site-packages/PIL/__init__.py +87 -0
  91. package/lib/python3.14/site-packages/PIL/__main__.py +7 -0
  92. package/lib/python3.14/site-packages/PIL/__pycache__/AvifImagePlugin.cpython-314.pyc +0 -0
  93. package/lib/python3.14/site-packages/PIL/__pycache__/BdfFontFile.cpython-314.pyc +0 -0
  94. package/lib/python3.14/site-packages/PIL/__pycache__/BlpImagePlugin.cpython-314.pyc +0 -0
  95. package/lib/python3.14/site-packages/PIL/__pycache__/BmpImagePlugin.cpython-314.pyc +0 -0
  96. package/lib/python3.14/site-packages/PIL/__pycache__/BufrStubImagePlugin.cpython-314.pyc +0 -0
  97. package/lib/python3.14/site-packages/PIL/__pycache__/ContainerIO.cpython-314.pyc +0 -0
  98. package/lib/python3.14/site-packages/PIL/__pycache__/CurImagePlugin.cpython-314.pyc +0 -0
  99. package/lib/python3.14/site-packages/PIL/__pycache__/DcxImagePlugin.cpython-314.pyc +0 -0
  100. package/lib/python3.14/site-packages/PIL/__pycache__/DdsImagePlugin.cpython-314.pyc +0 -0
  101. package/lib/python3.14/site-packages/PIL/__pycache__/EpsImagePlugin.cpython-314.pyc +0 -0
  102. package/lib/python3.14/site-packages/PIL/__pycache__/ExifTags.cpython-314.pyc +0 -0
  103. package/lib/python3.14/site-packages/PIL/__pycache__/FitsImagePlugin.cpython-314.pyc +0 -0
  104. package/lib/python3.14/site-packages/PIL/__pycache__/FliImagePlugin.cpython-314.pyc +0 -0
  105. package/lib/python3.14/site-packages/PIL/__pycache__/FontFile.cpython-314.pyc +0 -0
  106. package/lib/python3.14/site-packages/PIL/__pycache__/FpxImagePlugin.cpython-314.pyc +0 -0
  107. package/lib/python3.14/site-packages/PIL/__pycache__/FtexImagePlugin.cpython-314.pyc +0 -0
  108. package/lib/python3.14/site-packages/PIL/__pycache__/GbrImagePlugin.cpython-314.pyc +0 -0
  109. package/lib/python3.14/site-packages/PIL/__pycache__/GdImageFile.cpython-314.pyc +0 -0
  110. package/lib/python3.14/site-packages/PIL/__pycache__/GifImagePlugin.cpython-314.pyc +0 -0
  111. package/lib/python3.14/site-packages/PIL/__pycache__/GimpGradientFile.cpython-314.pyc +0 -0
  112. package/lib/python3.14/site-packages/PIL/__pycache__/GimpPaletteFile.cpython-314.pyc +0 -0
  113. package/lib/python3.14/site-packages/PIL/__pycache__/GribStubImagePlugin.cpython-314.pyc +0 -0
  114. package/lib/python3.14/site-packages/PIL/__pycache__/Hdf5StubImagePlugin.cpython-314.pyc +0 -0
  115. package/lib/python3.14/site-packages/PIL/__pycache__/IcnsImagePlugin.cpython-314.pyc +0 -0
  116. package/lib/python3.14/site-packages/PIL/__pycache__/IcoImagePlugin.cpython-314.pyc +0 -0
  117. package/lib/python3.14/site-packages/PIL/__pycache__/ImImagePlugin.cpython-314.pyc +0 -0
  118. package/lib/python3.14/site-packages/PIL/__pycache__/Image.cpython-314.pyc +0 -0
  119. package/lib/python3.14/site-packages/PIL/__pycache__/ImageChops.cpython-314.pyc +0 -0
  120. package/lib/python3.14/site-packages/PIL/__pycache__/ImageCms.cpython-314.pyc +0 -0
  121. package/lib/python3.14/site-packages/PIL/__pycache__/ImageColor.cpython-314.pyc +0 -0
  122. package/lib/python3.14/site-packages/PIL/__pycache__/ImageDraw.cpython-314.pyc +0 -0
  123. package/lib/python3.14/site-packages/PIL/__pycache__/ImageDraw2.cpython-314.pyc +0 -0
  124. package/lib/python3.14/site-packages/PIL/__pycache__/ImageEnhance.cpython-314.pyc +0 -0
  125. package/lib/python3.14/site-packages/PIL/__pycache__/ImageFile.cpython-314.pyc +0 -0
  126. package/lib/python3.14/site-packages/PIL/__pycache__/ImageFilter.cpython-314.pyc +0 -0
  127. package/lib/python3.14/site-packages/PIL/__pycache__/ImageFont.cpython-314.pyc +0 -0
  128. package/lib/python3.14/site-packages/PIL/__pycache__/ImageGrab.cpython-314.pyc +0 -0
  129. package/lib/python3.14/site-packages/PIL/__pycache__/ImageMath.cpython-314.pyc +0 -0
  130. package/lib/python3.14/site-packages/PIL/__pycache__/ImageMode.cpython-314.pyc +0 -0
  131. package/lib/python3.14/site-packages/PIL/__pycache__/ImageMorph.cpython-314.pyc +0 -0
  132. package/lib/python3.14/site-packages/PIL/__pycache__/ImageOps.cpython-314.pyc +0 -0
  133. package/lib/python3.14/site-packages/PIL/__pycache__/ImagePalette.cpython-314.pyc +0 -0
  134. package/lib/python3.14/site-packages/PIL/__pycache__/ImagePath.cpython-314.pyc +0 -0
  135. package/lib/python3.14/site-packages/PIL/__pycache__/ImageQt.cpython-314.pyc +0 -0
  136. package/lib/python3.14/site-packages/PIL/__pycache__/ImageSequence.cpython-314.pyc +0 -0
  137. package/lib/python3.14/site-packages/PIL/__pycache__/ImageShow.cpython-314.pyc +0 -0
  138. package/lib/python3.14/site-packages/PIL/__pycache__/ImageStat.cpython-314.pyc +0 -0
  139. package/lib/python3.14/site-packages/PIL/__pycache__/ImageText.cpython-314.pyc +0 -0
  140. package/lib/python3.14/site-packages/PIL/__pycache__/ImageTk.cpython-314.pyc +0 -0
  141. package/lib/python3.14/site-packages/PIL/__pycache__/ImageTransform.cpython-314.pyc +0 -0
  142. package/lib/python3.14/site-packages/PIL/__pycache__/ImageWin.cpython-314.pyc +0 -0
  143. package/lib/python3.14/site-packages/PIL/__pycache__/ImtImagePlugin.cpython-314.pyc +0 -0
  144. package/lib/python3.14/site-packages/PIL/__pycache__/IptcImagePlugin.cpython-314.pyc +0 -0
  145. package/lib/python3.14/site-packages/PIL/__pycache__/Jpeg2KImagePlugin.cpython-314.pyc +0 -0
  146. package/lib/python3.14/site-packages/PIL/__pycache__/JpegImagePlugin.cpython-314.pyc +0 -0
  147. package/lib/python3.14/site-packages/PIL/__pycache__/JpegPresets.cpython-314.pyc +0 -0
  148. package/lib/python3.14/site-packages/PIL/__pycache__/McIdasImagePlugin.cpython-314.pyc +0 -0
  149. package/lib/python3.14/site-packages/PIL/__pycache__/MicImagePlugin.cpython-314.pyc +0 -0
  150. package/lib/python3.14/site-packages/PIL/__pycache__/MpegImagePlugin.cpython-314.pyc +0 -0
  151. package/lib/python3.14/site-packages/PIL/__pycache__/MpoImagePlugin.cpython-314.pyc +0 -0
  152. package/lib/python3.14/site-packages/PIL/__pycache__/MspImagePlugin.cpython-314.pyc +0 -0
  153. package/lib/python3.14/site-packages/PIL/__pycache__/PSDraw.cpython-314.pyc +0 -0
  154. package/lib/python3.14/site-packages/PIL/__pycache__/PaletteFile.cpython-314.pyc +0 -0
  155. package/lib/python3.14/site-packages/PIL/__pycache__/PalmImagePlugin.cpython-314.pyc +0 -0
  156. package/lib/python3.14/site-packages/PIL/__pycache__/PcdImagePlugin.cpython-314.pyc +0 -0
  157. package/lib/python3.14/site-packages/PIL/__pycache__/PcfFontFile.cpython-314.pyc +0 -0
  158. package/lib/python3.14/site-packages/PIL/__pycache__/PcxImagePlugin.cpython-314.pyc +0 -0
  159. package/lib/python3.14/site-packages/PIL/__pycache__/PdfImagePlugin.cpython-314.pyc +0 -0
  160. package/lib/python3.14/site-packages/PIL/__pycache__/PdfParser.cpython-314.pyc +0 -0
  161. package/lib/python3.14/site-packages/PIL/__pycache__/PixarImagePlugin.cpython-314.pyc +0 -0
  162. package/lib/python3.14/site-packages/PIL/__pycache__/PngImagePlugin.cpython-314.pyc +0 -0
  163. package/lib/python3.14/site-packages/PIL/__pycache__/PpmImagePlugin.cpython-314.pyc +0 -0
  164. package/lib/python3.14/site-packages/PIL/__pycache__/PsdImagePlugin.cpython-314.pyc +0 -0
  165. package/lib/python3.14/site-packages/PIL/__pycache__/QoiImagePlugin.cpython-314.pyc +0 -0
  166. package/lib/python3.14/site-packages/PIL/__pycache__/SgiImagePlugin.cpython-314.pyc +0 -0
  167. package/lib/python3.14/site-packages/PIL/__pycache__/SpiderImagePlugin.cpython-314.pyc +0 -0
  168. package/lib/python3.14/site-packages/PIL/__pycache__/SunImagePlugin.cpython-314.pyc +0 -0
  169. package/lib/python3.14/site-packages/PIL/__pycache__/TarIO.cpython-314.pyc +0 -0
  170. package/lib/python3.14/site-packages/PIL/__pycache__/TgaImagePlugin.cpython-314.pyc +0 -0
  171. package/lib/python3.14/site-packages/PIL/__pycache__/TiffImagePlugin.cpython-314.pyc +0 -0
  172. package/lib/python3.14/site-packages/PIL/__pycache__/TiffTags.cpython-314.pyc +0 -0
  173. package/lib/python3.14/site-packages/PIL/__pycache__/WalImageFile.cpython-314.pyc +0 -0
  174. package/lib/python3.14/site-packages/PIL/__pycache__/WebPImagePlugin.cpython-314.pyc +0 -0
  175. package/lib/python3.14/site-packages/PIL/__pycache__/WmfImagePlugin.cpython-314.pyc +0 -0
  176. package/lib/python3.14/site-packages/PIL/__pycache__/XVThumbImagePlugin.cpython-314.pyc +0 -0
  177. package/lib/python3.14/site-packages/PIL/__pycache__/XbmImagePlugin.cpython-314.pyc +0 -0
  178. package/lib/python3.14/site-packages/PIL/__pycache__/XpmImagePlugin.cpython-314.pyc +0 -0
  179. package/lib/python3.14/site-packages/PIL/__pycache__/__init__.cpython-314.pyc +0 -0
  180. package/lib/python3.14/site-packages/PIL/__pycache__/__main__.cpython-314.pyc +0 -0
  181. package/lib/python3.14/site-packages/PIL/__pycache__/_binary.cpython-314.pyc +0 -0
  182. package/lib/python3.14/site-packages/PIL/__pycache__/_deprecate.cpython-314.pyc +0 -0
  183. package/lib/python3.14/site-packages/PIL/__pycache__/_tkinter_finder.cpython-314.pyc +0 -0
  184. package/lib/python3.14/site-packages/PIL/__pycache__/_typing.cpython-314.pyc +0 -0
  185. package/lib/python3.14/site-packages/PIL/__pycache__/_util.cpython-314.pyc +0 -0
  186. package/lib/python3.14/site-packages/PIL/__pycache__/_version.cpython-314.pyc +0 -0
  187. package/lib/python3.14/site-packages/PIL/__pycache__/features.cpython-314.pyc +0 -0
  188. package/lib/python3.14/site-packages/PIL/__pycache__/report.cpython-314.pyc +0 -0
  189. package/lib/python3.14/site-packages/PIL/_avif.pyi +3 -0
  190. package/lib/python3.14/site-packages/PIL/_binary.py +113 -0
  191. package/lib/python3.14/site-packages/PIL/_deprecate.py +72 -0
  192. package/lib/python3.14/site-packages/PIL/_imaging.cpython-314-wasm32-wasix.so +0 -0
  193. package/lib/python3.14/site-packages/PIL/_imaging.pyi +31 -0
  194. package/lib/python3.14/site-packages/PIL/_imagingcms.pyi +143 -0
  195. package/lib/python3.14/site-packages/PIL/_imagingft.cpython-314-wasm32-wasix.so +0 -0
  196. package/lib/python3.14/site-packages/PIL/_imagingft.pyi +70 -0
  197. package/lib/python3.14/site-packages/PIL/_imagingmath.cpython-314-wasm32-wasix.so +0 -0
  198. package/lib/python3.14/site-packages/PIL/_imagingmath.pyi +3 -0
  199. package/lib/python3.14/site-packages/PIL/_imagingmorph.cpython-314-wasm32-wasix.so +0 -0
  200. package/lib/python3.14/site-packages/PIL/_imagingmorph.pyi +3 -0
  201. package/lib/python3.14/site-packages/PIL/_imagingtk.cpython-314-wasm32-wasix.so +0 -0
  202. package/lib/python3.14/site-packages/PIL/_imagingtk.pyi +3 -0
  203. package/lib/python3.14/site-packages/PIL/_tkinter_finder.py +20 -0
  204. package/lib/python3.14/site-packages/PIL/_typing.py +45 -0
  205. package/lib/python3.14/site-packages/PIL/_util.py +29 -0
  206. package/lib/python3.14/site-packages/PIL/_version.py +4 -0
  207. package/lib/python3.14/site-packages/PIL/_webp.pyi +3 -0
  208. package/lib/python3.14/site-packages/PIL/features.py +343 -0
  209. package/lib/python3.14/site-packages/PIL/py.typed +0 -0
  210. package/lib/python3.14/site-packages/PIL/report.py +5 -0
  211. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/METADATA +180 -0
  212. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/RECORD +117 -0
  213. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/WHEEL +5 -0
  214. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/licenses/LICENSE +30 -0
  215. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/top_level.txt +1 -0
  216. package/lib/python3.14/site-packages/pillow-12.3.0.dist-info/zip-safe +1 -0
  217. package/package.json +46 -0
@@ -0,0 +1,617 @@
1
+ #
2
+ # The Python Imaging Library.
3
+ # $Id$
4
+ #
5
+ # standard filters
6
+ #
7
+ # History:
8
+ # 1995-11-27 fl Created
9
+ # 2002-06-08 fl Added rank and mode filters
10
+ # 2003-09-15 fl Fixed rank calculation in rank filter; added expand call
11
+ #
12
+ # Copyright (c) 1997-2003 by Secret Labs AB.
13
+ # Copyright (c) 1995-2002 by Fredrik Lundh.
14
+ #
15
+ # See the README file for information on usage and redistribution.
16
+ #
17
+ from __future__ import annotations
18
+
19
+ import abc
20
+ import functools
21
+ from collections.abc import Sequence
22
+ from typing import cast
23
+
24
+ TYPE_CHECKING = False
25
+ if TYPE_CHECKING:
26
+ from collections.abc import Callable
27
+ from types import ModuleType
28
+ from typing import Any
29
+
30
+ from . import _imaging
31
+ from ._typing import NumpyArray
32
+
33
+
34
+ class Filter(abc.ABC):
35
+ @abc.abstractmethod
36
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
37
+ pass
38
+
39
+
40
+ class MultibandFilter(Filter):
41
+ pass
42
+
43
+
44
+ class BuiltinFilter(MultibandFilter):
45
+ name: str
46
+ filterargs: tuple[Any, ...]
47
+
48
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
49
+ if image.mode == "P":
50
+ msg = "cannot filter palette images"
51
+ raise ValueError(msg)
52
+ return image.filter(*self.filterargs)
53
+
54
+
55
+ class Kernel(BuiltinFilter):
56
+ """
57
+ Create a convolution kernel. This only supports 3x3 and 5x5 integer and floating
58
+ point kernels.
59
+
60
+ Kernels can only be applied to "L" and "RGB" images.
61
+
62
+ :param size: Kernel size, given as (width, height). This must be (3,3) or (5,5).
63
+ :param kernel: A sequence containing kernel weights. The kernel will be flipped
64
+ vertically before being applied to the image.
65
+ :param scale: Scale factor. If given, the result for each pixel is divided by this
66
+ value. The default is the sum of the kernel weights.
67
+ :param offset: Offset. If given, this value is added to the result, after it has
68
+ been divided by the scale factor.
69
+ """
70
+
71
+ name = "Kernel"
72
+
73
+ def __init__(
74
+ self,
75
+ size: tuple[int, int],
76
+ kernel: Sequence[float],
77
+ scale: float | None = None,
78
+ offset: float = 0,
79
+ ) -> None:
80
+ if scale is None:
81
+ # default scale is sum of kernel
82
+ scale = functools.reduce(lambda a, b: a + b, kernel)
83
+ if size[0] * size[1] != len(kernel):
84
+ msg = "not enough coefficients in kernel"
85
+ raise ValueError(msg)
86
+ self.filterargs = size, scale, offset, kernel
87
+
88
+
89
+ class RankFilter(Filter):
90
+ """
91
+ Create a rank filter. The rank filter sorts all pixels in
92
+ a window of the given size, and returns the ``rank``'th value.
93
+
94
+ :param size: The kernel size, in pixels.
95
+ :param rank: What pixel value to pick. Use 0 for a min filter,
96
+ ``size * size / 2`` for a median filter, ``size * size - 1``
97
+ for a max filter, etc.
98
+ """
99
+
100
+ name = "Rank"
101
+
102
+ def __init__(self, size: int, rank: int) -> None:
103
+ if size % 2 == 0:
104
+ msg = "bad filter size"
105
+ raise ValueError(msg)
106
+ if size * size * 4 > (2**31 - 1):
107
+ msg = "filter size too large"
108
+ raise ValueError(msg)
109
+ if rank < 0 or rank >= size * size:
110
+ msg = "bad rank value"
111
+ raise ValueError(msg)
112
+ self.size = size
113
+ self.rank = rank
114
+
115
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
116
+ if image.mode == "P":
117
+ msg = "cannot filter palette images"
118
+ raise ValueError(msg)
119
+ image = image.expand(self.size // 2)
120
+ return image.rankfilter(self.size, self.rank)
121
+
122
+
123
+ class MedianFilter(RankFilter):
124
+ """
125
+ Create a median filter. Picks the median pixel value in a window with the
126
+ given size.
127
+
128
+ :param size: The kernel size, in pixels.
129
+ """
130
+
131
+ name = "Median"
132
+
133
+ def __init__(self, size: int = 3) -> None:
134
+ rank = size * size // 2
135
+ super().__init__(size, rank)
136
+
137
+
138
+ class MinFilter(RankFilter):
139
+ """
140
+ Create a min filter. Picks the lowest pixel value in a window with the
141
+ given size.
142
+
143
+ :param size: The kernel size, in pixels.
144
+ """
145
+
146
+ name = "Min"
147
+
148
+ def __init__(self, size: int = 3) -> None:
149
+ rank = 0
150
+ super().__init__(size, rank)
151
+
152
+
153
+ class MaxFilter(RankFilter):
154
+ """
155
+ Create a max filter. Picks the largest pixel value in a window with the
156
+ given size.
157
+
158
+ :param size: The kernel size, in pixels.
159
+ """
160
+
161
+ name = "Max"
162
+
163
+ def __init__(self, size: int = 3) -> None:
164
+ rank = size * size - 1
165
+ super().__init__(size, rank)
166
+
167
+
168
+ class ModeFilter(Filter):
169
+ """
170
+ Create a mode filter. Picks the most frequent pixel value in a box with the
171
+ given size. Pixel values that occur only once or twice are ignored; if no
172
+ pixel value occurs more than twice, the original pixel value is preserved.
173
+
174
+ :param size: The kernel size, in pixels.
175
+ """
176
+
177
+ name = "Mode"
178
+
179
+ def __init__(self, size: int = 3) -> None:
180
+ self.size = size
181
+
182
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
183
+ return image.modefilter(self.size)
184
+
185
+
186
+ class GaussianBlur(MultibandFilter):
187
+ """Blurs the image with a sequence of extended box filters, which
188
+ approximates a Gaussian kernel. For details on accuracy see
189
+ <https://www.mia.uni-saarland.de/Publications/gwosdek-ssvm11.pdf>
190
+
191
+ :param radius: Standard deviation of the Gaussian kernel. Either a sequence of two
192
+ numbers for x and y, or a single number for both.
193
+ """
194
+
195
+ name = "GaussianBlur"
196
+
197
+ def __init__(self, radius: float | Sequence[float] = 2) -> None:
198
+ self.radius = radius
199
+
200
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
201
+ xy = self.radius
202
+ if xy == (0, 0) or xy == 0:
203
+ return image.copy()
204
+ if isinstance(xy, (int, float)):
205
+ xy = (xy, xy)
206
+ return image.gaussian_blur(xy)
207
+
208
+
209
+ class BoxBlur(MultibandFilter):
210
+ """Blurs the image by setting each pixel to the average value of the pixels
211
+ in a square box extending radius pixels in each direction.
212
+ Supports float radius of arbitrary size. Uses an optimized implementation
213
+ which runs in linear time relative to the size of the image
214
+ for any radius value.
215
+
216
+ :param radius: Size of the box in a direction. Either a sequence of two numbers for
217
+ x and y, or a single number for both.
218
+
219
+ Radius 0 does not blur, returns an identical image.
220
+ Radius 1 takes 1 pixel in each direction, i.e. 9 pixels in total.
221
+ """
222
+
223
+ name = "BoxBlur"
224
+
225
+ def __init__(self, radius: float | Sequence[float]) -> None:
226
+ xy = radius if isinstance(radius, (tuple, list)) else (radius, radius)
227
+ if xy[0] < 0 or xy[1] < 0:
228
+ msg = "radius must be >= 0"
229
+ raise ValueError(msg)
230
+ self.radius = radius
231
+
232
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
233
+ xy = self.radius
234
+ if xy == (0, 0) or xy == 0:
235
+ return image.copy()
236
+ if isinstance(xy, (int, float)):
237
+ xy = (xy, xy)
238
+ return image.box_blur(xy)
239
+
240
+
241
+ class UnsharpMask(MultibandFilter):
242
+ """Unsharp mask filter.
243
+
244
+ See Wikipedia's entry on `digital unsharp masking`_ for an explanation of
245
+ the parameters.
246
+
247
+ :param radius: Blur Radius
248
+ :param percent: Unsharp strength, in percent
249
+ :param threshold: Threshold controls the minimum brightness change that
250
+ will be sharpened
251
+
252
+ .. _digital unsharp masking: https://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking
253
+
254
+ """
255
+
256
+ name = "UnsharpMask"
257
+
258
+ def __init__(
259
+ self, radius: float = 2, percent: int = 150, threshold: int = 3
260
+ ) -> None:
261
+ self.radius = radius
262
+ self.percent = percent
263
+ self.threshold = threshold
264
+
265
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
266
+ return image.unsharp_mask(self.radius, self.percent, self.threshold)
267
+
268
+
269
+ class BLUR(BuiltinFilter):
270
+ name = "Blur"
271
+ # fmt: off
272
+ filterargs = (5, 5), 16, 0, (
273
+ 1, 1, 1, 1, 1,
274
+ 1, 0, 0, 0, 1,
275
+ 1, 0, 0, 0, 1,
276
+ 1, 0, 0, 0, 1,
277
+ 1, 1, 1, 1, 1,
278
+ )
279
+ # fmt: on
280
+
281
+
282
+ class CONTOUR(BuiltinFilter):
283
+ name = "Contour"
284
+ # fmt: off
285
+ filterargs = (3, 3), 1, 255, (
286
+ -1, -1, -1,
287
+ -1, 8, -1,
288
+ -1, -1, -1,
289
+ )
290
+ # fmt: on
291
+
292
+
293
+ class DETAIL(BuiltinFilter):
294
+ name = "Detail"
295
+ # fmt: off
296
+ filterargs = (3, 3), 6, 0, (
297
+ 0, -1, 0,
298
+ -1, 10, -1,
299
+ 0, -1, 0,
300
+ )
301
+ # fmt: on
302
+
303
+
304
+ class EDGE_ENHANCE(BuiltinFilter):
305
+ name = "Edge-enhance"
306
+ # fmt: off
307
+ filterargs = (3, 3), 2, 0, (
308
+ -1, -1, -1,
309
+ -1, 10, -1,
310
+ -1, -1, -1,
311
+ )
312
+ # fmt: on
313
+
314
+
315
+ class EDGE_ENHANCE_MORE(BuiltinFilter):
316
+ name = "Edge-enhance More"
317
+ # fmt: off
318
+ filterargs = (3, 3), 1, 0, (
319
+ -1, -1, -1,
320
+ -1, 9, -1,
321
+ -1, -1, -1,
322
+ )
323
+ # fmt: on
324
+
325
+
326
+ class EMBOSS(BuiltinFilter):
327
+ name = "Emboss"
328
+ # fmt: off
329
+ filterargs = (3, 3), 1, 128, (
330
+ -1, 0, 0,
331
+ 0, 1, 0,
332
+ 0, 0, 0,
333
+ )
334
+ # fmt: on
335
+
336
+
337
+ class FIND_EDGES(BuiltinFilter):
338
+ name = "Find Edges"
339
+ # fmt: off
340
+ filterargs = (3, 3), 1, 0, (
341
+ -1, -1, -1,
342
+ -1, 8, -1,
343
+ -1, -1, -1,
344
+ )
345
+ # fmt: on
346
+
347
+
348
+ class SHARPEN(BuiltinFilter):
349
+ name = "Sharpen"
350
+ # fmt: off
351
+ filterargs = (3, 3), 16, 0, (
352
+ -2, -2, -2,
353
+ -2, 32, -2,
354
+ -2, -2, -2,
355
+ )
356
+ # fmt: on
357
+
358
+
359
+ class SMOOTH(BuiltinFilter):
360
+ name = "Smooth"
361
+ # fmt: off
362
+ filterargs = (3, 3), 13, 0, (
363
+ 1, 1, 1,
364
+ 1, 5, 1,
365
+ 1, 1, 1,
366
+ )
367
+ # fmt: on
368
+
369
+
370
+ class SMOOTH_MORE(BuiltinFilter):
371
+ name = "Smooth More"
372
+ # fmt: off
373
+ filterargs = (5, 5), 100, 0, (
374
+ 1, 1, 1, 1, 1,
375
+ 1, 5, 5, 5, 1,
376
+ 1, 5, 44, 5, 1,
377
+ 1, 5, 5, 5, 1,
378
+ 1, 1, 1, 1, 1,
379
+ )
380
+ # fmt: on
381
+
382
+
383
+ class Color3DLUT(MultibandFilter):
384
+ """Three-dimensional color lookup table.
385
+
386
+ Transforms 3-channel pixels using the values of the channels as coordinates
387
+ in the 3D lookup table and interpolating the nearest elements.
388
+
389
+ This method allows you to apply almost any color transformation
390
+ in constant time by using pre-calculated decimated tables.
391
+
392
+ .. versionadded:: 5.2.0
393
+
394
+ :param size: Size of the table. One int or tuple of (int, int, int).
395
+ Minimal size in any dimension is 2, maximum is 65.
396
+ :param table: Flat lookup table. A list of ``channels * size**3``
397
+ float elements or a list of ``size**3`` channels-sized
398
+ tuples with floats. Channels are changed first,
399
+ then first dimension, then second, then third.
400
+ Value 0.0 corresponds lowest value of output, 1.0 highest.
401
+ :param channels: Number of channels in the table. Could be 3 or 4.
402
+ Default is 3.
403
+ :param target_mode: A mode for the result image. Should have not less
404
+ than ``channels`` channels. Default is ``None``,
405
+ which means that mode wouldn't be changed.
406
+ """
407
+
408
+ name = "Color 3D LUT"
409
+
410
+ def __init__(
411
+ self,
412
+ size: int | tuple[int, int, int],
413
+ table: Sequence[float] | Sequence[Sequence[int]] | NumpyArray,
414
+ channels: int = 3,
415
+ target_mode: str | None = None,
416
+ **kwargs: bool,
417
+ ) -> None:
418
+ if channels not in (3, 4):
419
+ msg = "Only 3 or 4 output channels are supported"
420
+ raise ValueError(msg)
421
+ self.size = size = self._check_size(size)
422
+ self.channels = channels
423
+ self.mode = target_mode
424
+
425
+ # Hidden flag `_copy_table=False` could be used to avoid extra copying
426
+ # of the table if the table is specially made for the constructor.
427
+ copy_table = kwargs.get("_copy_table", True)
428
+ items = size[0] * size[1] * size[2]
429
+ wrong_size = False
430
+
431
+ numpy: ModuleType | None = None
432
+ if hasattr(table, "shape"):
433
+ try:
434
+ import numpy
435
+ except ImportError:
436
+ pass
437
+
438
+ if numpy and isinstance(table, numpy.ndarray):
439
+ numpy_table: NumpyArray = table
440
+ if copy_table:
441
+ numpy_table = numpy_table.copy()
442
+
443
+ if numpy_table.shape in [
444
+ (items * channels,),
445
+ (items, channels),
446
+ (size[2], size[1], size[0], channels),
447
+ ]:
448
+ table = numpy_table.reshape(items * channels)
449
+ else:
450
+ wrong_size = True
451
+
452
+ else:
453
+ if copy_table:
454
+ table = list(table)
455
+
456
+ # Convert to a flat list
457
+ if table and isinstance(table[0], (list, tuple)):
458
+ raw_table = cast(Sequence[Sequence[int]], table)
459
+ flat_table: list[int] = []
460
+ for pixel in raw_table:
461
+ if len(pixel) != channels:
462
+ msg = (
463
+ "The elements of the table should "
464
+ f"have a length of {channels}."
465
+ )
466
+ raise ValueError(msg)
467
+ flat_table.extend(pixel)
468
+ table = flat_table
469
+
470
+ if wrong_size or len(table) != items * channels:
471
+ msg = (
472
+ "The table should have either channels * size**3 float items "
473
+ "or size**3 items of channels-sized tuples with floats. "
474
+ f"Table should be: {channels}x{size[0]}x{size[1]}x{size[2]}. "
475
+ f"Actual length: {len(table)}"
476
+ )
477
+ raise ValueError(msg)
478
+ self.table = table
479
+
480
+ @staticmethod
481
+ def _check_size(size: Any) -> tuple[int, int, int]:
482
+ try:
483
+ _, _, _ = size
484
+ except ValueError as e:
485
+ msg = "Size should be either an integer or a tuple of three integers."
486
+ raise ValueError(msg) from e
487
+ except TypeError:
488
+ size = (size, size, size)
489
+ size = tuple(int(x) for x in size)
490
+ for size_1d in size:
491
+ if not 2 <= size_1d <= 65:
492
+ msg = "Size should be in [2, 65] range."
493
+ raise ValueError(msg)
494
+ return size
495
+
496
+ @classmethod
497
+ def generate(
498
+ cls,
499
+ size: int | tuple[int, int, int],
500
+ callback: Callable[[float, float, float], tuple[float, ...]],
501
+ channels: int = 3,
502
+ target_mode: str | None = None,
503
+ ) -> Color3DLUT:
504
+ """Generates new LUT using provided callback.
505
+
506
+ :param size: Size of the table. Passed to the constructor.
507
+ :param callback: Function with three parameters which correspond
508
+ three color channels. Will be called ``size**3``
509
+ times with values from 0.0 to 1.0 and should return
510
+ a tuple with ``channels`` elements.
511
+ :param channels: The number of channels which should return callback.
512
+ :param target_mode: Passed to the constructor of the resulting
513
+ lookup table.
514
+ """
515
+ size_1d, size_2d, size_3d = cls._check_size(size)
516
+ if channels not in (3, 4):
517
+ msg = "Only 3 or 4 output channels are supported"
518
+ raise ValueError(msg)
519
+
520
+ table: list[float] = [0] * (size_1d * size_2d * size_3d * channels)
521
+ idx_out = 0
522
+ for b in range(size_3d):
523
+ for g in range(size_2d):
524
+ for r in range(size_1d):
525
+ table[idx_out : idx_out + channels] = callback(
526
+ r / (size_1d - 1), g / (size_2d - 1), b / (size_3d - 1)
527
+ )
528
+ idx_out += channels
529
+
530
+ return cls(
531
+ (size_1d, size_2d, size_3d),
532
+ table,
533
+ channels=channels,
534
+ target_mode=target_mode,
535
+ _copy_table=False,
536
+ )
537
+
538
+ def transform(
539
+ self,
540
+ callback: Callable[..., tuple[float, ...]],
541
+ with_normals: bool = False,
542
+ channels: int | None = None,
543
+ target_mode: str | None = None,
544
+ ) -> Color3DLUT:
545
+ """Transforms the table values using provided callback and returns
546
+ a new LUT with altered values.
547
+
548
+ :param callback: A function which takes old lookup table values
549
+ and returns a new set of values. The number
550
+ of arguments which function should take is
551
+ ``self.channels`` or ``3 + self.channels``
552
+ if ``with_normals`` flag is set.
553
+ Should return a tuple of ``self.channels`` or
554
+ ``channels`` elements if it is set.
555
+ :param with_normals: If true, ``callback`` will be called with
556
+ coordinates in the color cube as the first
557
+ three arguments. Otherwise, ``callback``
558
+ will be called only with actual color values.
559
+ :param channels: The number of channels in the resulting lookup table.
560
+ :param target_mode: Passed to the constructor of the resulting
561
+ lookup table.
562
+ """
563
+ if channels not in (None, 3, 4):
564
+ msg = "Only 3 or 4 output channels are supported"
565
+ raise ValueError(msg)
566
+ ch_in = self.channels
567
+ ch_out = channels or ch_in
568
+ size_1d, size_2d, size_3d = self.size
569
+
570
+ table: list[float] = [0] * (size_1d * size_2d * size_3d * ch_out)
571
+ idx_in = 0
572
+ idx_out = 0
573
+ for b in range(size_3d):
574
+ for g in range(size_2d):
575
+ for r in range(size_1d):
576
+ values = self.table[idx_in : idx_in + ch_in]
577
+ if with_normals:
578
+ values = callback(
579
+ r / (size_1d - 1),
580
+ g / (size_2d - 1),
581
+ b / (size_3d - 1),
582
+ *values,
583
+ )
584
+ else:
585
+ values = callback(*values)
586
+ table[idx_out : idx_out + ch_out] = values
587
+ idx_in += ch_in
588
+ idx_out += ch_out
589
+
590
+ return type(self)(
591
+ self.size,
592
+ table,
593
+ channels=ch_out,
594
+ target_mode=target_mode or self.mode,
595
+ _copy_table=False,
596
+ )
597
+
598
+ def __repr__(self) -> str:
599
+ r = [
600
+ f"{self.__class__.__name__} from {self.table.__class__.__name__}",
601
+ "size={:d}x{:d}x{:d}".format(*self.size),
602
+ f"channels={self.channels:d}",
603
+ ]
604
+ if self.mode:
605
+ r.append(f"target_mode={self.mode}")
606
+ return "<{}>".format(" ".join(r))
607
+
608
+ def filter(self, image: _imaging.ImagingCore) -> _imaging.ImagingCore:
609
+ from . import Image
610
+
611
+ return image.color_lut_3d(
612
+ self.mode or image.mode,
613
+ Image.Resampling.BILINEAR,
614
+ self.channels,
615
+ self.size,
616
+ self.table,
617
+ )