Add files using upload-large-folder tool
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- .venv/lib/python3.11/site-packages/PIL/BlpImagePlugin.py +488 -0
- .venv/lib/python3.11/site-packages/PIL/EpsImagePlugin.py +478 -0
- .venv/lib/python3.11/site-packages/PIL/FitsImagePlugin.py +152 -0
- .venv/lib/python3.11/site-packages/PIL/FliImagePlugin.py +174 -0
- .venv/lib/python3.11/site-packages/PIL/FtexImagePlugin.py +115 -0
- .venv/lib/python3.11/site-packages/PIL/GbrImagePlugin.py +103 -0
- .venv/lib/python3.11/site-packages/PIL/GdImageFile.py +102 -0
- .venv/lib/python3.11/site-packages/PIL/GimpGradientFile.py +149 -0
- .venv/lib/python3.11/site-packages/PIL/GimpPaletteFile.py +58 -0
- .venv/lib/python3.11/site-packages/PIL/GribStubImagePlugin.py +76 -0
- .venv/lib/python3.11/site-packages/PIL/Hdf5StubImagePlugin.py +76 -0
- .venv/lib/python3.11/site-packages/PIL/IcoImagePlugin.py +360 -0
- .venv/lib/python3.11/site-packages/PIL/ImImagePlugin.py +374 -0
- .venv/lib/python3.11/site-packages/PIL/ImageChops.py +311 -0
- .venv/lib/python3.11/site-packages/PIL/ImageDraw.py +1206 -0
- .venv/lib/python3.11/site-packages/PIL/ImageDraw2.py +206 -0
- .venv/lib/python3.11/site-packages/PIL/ImageGrab.py +194 -0
- .venv/lib/python3.11/site-packages/PIL/ImageMath.py +357 -0
- .venv/lib/python3.11/site-packages/PIL/ImageMorph.py +265 -0
- .venv/lib/python3.11/site-packages/PIL/ImageOps.py +728 -0
- .venv/lib/python3.11/site-packages/PIL/ImagePalette.py +284 -0
- .venv/lib/python3.11/site-packages/PIL/ImageQt.py +205 -0
- .venv/lib/python3.11/site-packages/PIL/ImageSequence.py +86 -0
- .venv/lib/python3.11/site-packages/PIL/IptcImagePlugin.py +235 -0
- .venv/lib/python3.11/site-packages/PIL/JpegImagePlugin.py +861 -0
- .venv/lib/python3.11/site-packages/PIL/MicImagePlugin.py +107 -0
- .venv/lib/python3.11/site-packages/PIL/MpegImagePlugin.py +88 -0
- .venv/lib/python3.11/site-packages/PIL/PSDraw.py +237 -0
- .venv/lib/python3.11/site-packages/PIL/PcdImagePlugin.py +66 -0
- .venv/lib/python3.11/site-packages/PIL/PcfFontFile.py +254 -0
- .venv/lib/python3.11/site-packages/PIL/SpiderImagePlugin.py +325 -0
- .venv/lib/python3.11/site-packages/PIL/SunImagePlugin.py +141 -0
- .venv/lib/python3.11/site-packages/PIL/TarIO.py +67 -0
- .venv/lib/python3.11/site-packages/PIL/__init__.py +86 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/BmpImagePlugin.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/EpsImagePlugin.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/GimpGradientFile.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/GimpPaletteFile.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/GribStubImagePlugin.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/IcnsImagePlugin.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/IcoImagePlugin.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageChops.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageDraw.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageQt.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageSequence.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageShow.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/ImageStat.cpython-311.pyc +0 -0
- .venv/lib/python3.11/site-packages/PIL/__pycache__/IptcImagePlugin.cpython-311.pyc +0 -0
- .venv/l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|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Blizzard Mipmap Format (.blp)
|
| 3 |
+
Jerome Leclanche <jerome@leclan.ch>
|
| 4 |
+
|
| 5 |
+
The contents of this file are hereby released in the public domain (CC0)
|
| 6 |
+
Full text of the CC0 license:
|
| 7 |
+
https://creativecommons.org/publicdomain/zero/1.0/
|
| 8 |
+
|
| 9 |
+
BLP1 files, used mostly in Warcraft III, are not fully supported.
|
| 10 |
+
All types of BLP2 files used in World of Warcraft are supported.
|
| 11 |
+
|
| 12 |
+
The BLP file structure consists of a header, up to 16 mipmaps of the
|
| 13 |
+
texture
|
| 14 |
+
|
| 15 |
+
Texture sizes must be powers of two, though the two dimensions do
|
| 16 |
+
not have to be equal; 512x256 is valid, but 512x200 is not.
|
| 17 |
+
The first mipmap (mipmap #0) is the full size image; each subsequent
|
| 18 |
+
mipmap halves both dimensions. The final mipmap should be 1x1.
|
| 19 |
+
|
| 20 |
+
BLP files come in many different flavours:
|
| 21 |
+
* JPEG-compressed (type == 0) - only supported for BLP1.
|
| 22 |
+
* RAW images (type == 1, encoding == 1). Each mipmap is stored as an
|
| 23 |
+
array of 8-bit values, one per pixel, left to right, top to bottom.
|
| 24 |
+
Each value is an index to the palette.
|
| 25 |
+
* DXT-compressed (type == 1, encoding == 2):
|
| 26 |
+
- DXT1 compression is used if alpha_encoding == 0.
|
| 27 |
+
- An additional alpha bit is used if alpha_depth == 1.
|
| 28 |
+
- DXT3 compression is used if alpha_encoding == 1.
|
| 29 |
+
- DXT5 compression is used if alpha_encoding == 7.
|
| 30 |
+
"""
|
| 31 |
+
|
| 32 |
+
from __future__ import annotations
|
| 33 |
+
|
| 34 |
+
import abc
|
| 35 |
+
import os
|
| 36 |
+
import struct
|
| 37 |
+
from enum import IntEnum
|
| 38 |
+
from io import BytesIO
|
| 39 |
+
from typing import IO
|
| 40 |
+
|
| 41 |
+
from . import Image, ImageFile
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
class Format(IntEnum):
|
| 45 |
+
JPEG = 0
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class Encoding(IntEnum):
|
| 49 |
+
UNCOMPRESSED = 1
|
| 50 |
+
DXT = 2
|
| 51 |
+
UNCOMPRESSED_RAW_BGRA = 3
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class AlphaEncoding(IntEnum):
|
| 55 |
+
DXT1 = 0
|
| 56 |
+
DXT3 = 1
|
| 57 |
+
DXT5 = 7
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
def unpack_565(i: int) -> tuple[int, int, int]:
|
| 61 |
+
return ((i >> 11) & 0x1F) << 3, ((i >> 5) & 0x3F) << 2, (i & 0x1F) << 3
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def decode_dxt1(
|
| 65 |
+
data: bytes, alpha: bool = False
|
| 66 |
+
) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 67 |
+
"""
|
| 68 |
+
input: one "row" of data (i.e. will produce 4*width pixels)
|
| 69 |
+
"""
|
| 70 |
+
|
| 71 |
+
blocks = len(data) // 8 # number of blocks in row
|
| 72 |
+
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 73 |
+
|
| 74 |
+
for block_index in range(blocks):
|
| 75 |
+
# Decode next 8-byte block.
|
| 76 |
+
idx = block_index * 8
|
| 77 |
+
color0, color1, bits = struct.unpack_from("<HHI", data, idx)
|
| 78 |
+
|
| 79 |
+
r0, g0, b0 = unpack_565(color0)
|
| 80 |
+
r1, g1, b1 = unpack_565(color1)
|
| 81 |
+
|
| 82 |
+
# Decode this block into 4x4 pixels
|
| 83 |
+
# Accumulate the results onto our 4 row accumulators
|
| 84 |
+
for j in range(4):
|
| 85 |
+
for i in range(4):
|
| 86 |
+
# get next control op and generate a pixel
|
| 87 |
+
|
| 88 |
+
control = bits & 3
|
| 89 |
+
bits = bits >> 2
|
| 90 |
+
|
| 91 |
+
a = 0xFF
|
| 92 |
+
if control == 0:
|
| 93 |
+
r, g, b = r0, g0, b0
|
| 94 |
+
elif control == 1:
|
| 95 |
+
r, g, b = r1, g1, b1
|
| 96 |
+
elif control == 2:
|
| 97 |
+
if color0 > color1:
|
| 98 |
+
r = (2 * r0 + r1) // 3
|
| 99 |
+
g = (2 * g0 + g1) // 3
|
| 100 |
+
b = (2 * b0 + b1) // 3
|
| 101 |
+
else:
|
| 102 |
+
r = (r0 + r1) // 2
|
| 103 |
+
g = (g0 + g1) // 2
|
| 104 |
+
b = (b0 + b1) // 2
|
| 105 |
+
elif control == 3:
|
| 106 |
+
if color0 > color1:
|
| 107 |
+
r = (2 * r1 + r0) // 3
|
| 108 |
+
g = (2 * g1 + g0) // 3
|
| 109 |
+
b = (2 * b1 + b0) // 3
|
| 110 |
+
else:
|
| 111 |
+
r, g, b, a = 0, 0, 0, 0
|
| 112 |
+
|
| 113 |
+
if alpha:
|
| 114 |
+
ret[j].extend([r, g, b, a])
|
| 115 |
+
else:
|
| 116 |
+
ret[j].extend([r, g, b])
|
| 117 |
+
|
| 118 |
+
return ret
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
def decode_dxt3(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 122 |
+
"""
|
| 123 |
+
input: one "row" of data (i.e. will produce 4*width pixels)
|
| 124 |
+
"""
|
| 125 |
+
|
| 126 |
+
blocks = len(data) // 16 # number of blocks in row
|
| 127 |
+
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 128 |
+
|
| 129 |
+
for block_index in range(blocks):
|
| 130 |
+
idx = block_index * 16
|
| 131 |
+
block = data[idx : idx + 16]
|
| 132 |
+
# Decode next 16-byte block.
|
| 133 |
+
bits = struct.unpack_from("<8B", block)
|
| 134 |
+
color0, color1 = struct.unpack_from("<HH", block, 8)
|
| 135 |
+
|
| 136 |
+
(code,) = struct.unpack_from("<I", block, 12)
|
| 137 |
+
|
| 138 |
+
r0, g0, b0 = unpack_565(color0)
|
| 139 |
+
r1, g1, b1 = unpack_565(color1)
|
| 140 |
+
|
| 141 |
+
for j in range(4):
|
| 142 |
+
high = False # Do we want the higher bits?
|
| 143 |
+
for i in range(4):
|
| 144 |
+
alphacode_index = (4 * j + i) // 2
|
| 145 |
+
a = bits[alphacode_index]
|
| 146 |
+
if high:
|
| 147 |
+
high = False
|
| 148 |
+
a >>= 4
|
| 149 |
+
else:
|
| 150 |
+
high = True
|
| 151 |
+
a &= 0xF
|
| 152 |
+
a *= 17 # We get a value between 0 and 15
|
| 153 |
+
|
| 154 |
+
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
| 155 |
+
|
| 156 |
+
if color_code == 0:
|
| 157 |
+
r, g, b = r0, g0, b0
|
| 158 |
+
elif color_code == 1:
|
| 159 |
+
r, g, b = r1, g1, b1
|
| 160 |
+
elif color_code == 2:
|
| 161 |
+
r = (2 * r0 + r1) // 3
|
| 162 |
+
g = (2 * g0 + g1) // 3
|
| 163 |
+
b = (2 * b0 + b1) // 3
|
| 164 |
+
elif color_code == 3:
|
| 165 |
+
r = (2 * r1 + r0) // 3
|
| 166 |
+
g = (2 * g1 + g0) // 3
|
| 167 |
+
b = (2 * b1 + b0) // 3
|
| 168 |
+
|
| 169 |
+
ret[j].extend([r, g, b, a])
|
| 170 |
+
|
| 171 |
+
return ret
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
def decode_dxt5(data: bytes) -> tuple[bytearray, bytearray, bytearray, bytearray]:
|
| 175 |
+
"""
|
| 176 |
+
input: one "row" of data (i.e. will produce 4 * width pixels)
|
| 177 |
+
"""
|
| 178 |
+
|
| 179 |
+
blocks = len(data) // 16 # number of blocks in row
|
| 180 |
+
ret = (bytearray(), bytearray(), bytearray(), bytearray())
|
| 181 |
+
|
| 182 |
+
for block_index in range(blocks):
|
| 183 |
+
idx = block_index * 16
|
| 184 |
+
block = data[idx : idx + 16]
|
| 185 |
+
# Decode next 16-byte block.
|
| 186 |
+
a0, a1 = struct.unpack_from("<BB", block)
|
| 187 |
+
|
| 188 |
+
bits = struct.unpack_from("<6B", block, 2)
|
| 189 |
+
alphacode1 = bits[2] | (bits[3] << 8) | (bits[4] << 16) | (bits[5] << 24)
|
| 190 |
+
alphacode2 = bits[0] | (bits[1] << 8)
|
| 191 |
+
|
| 192 |
+
color0, color1 = struct.unpack_from("<HH", block, 8)
|
| 193 |
+
|
| 194 |
+
(code,) = struct.unpack_from("<I", block, 12)
|
| 195 |
+
|
| 196 |
+
r0, g0, b0 = unpack_565(color0)
|
| 197 |
+
r1, g1, b1 = unpack_565(color1)
|
| 198 |
+
|
| 199 |
+
for j in range(4):
|
| 200 |
+
for i in range(4):
|
| 201 |
+
# get next control op and generate a pixel
|
| 202 |
+
alphacode_index = 3 * (4 * j + i)
|
| 203 |
+
|
| 204 |
+
if alphacode_index <= 12:
|
| 205 |
+
alphacode = (alphacode2 >> alphacode_index) & 0x07
|
| 206 |
+
elif alphacode_index == 15:
|
| 207 |
+
alphacode = (alphacode2 >> 15) | ((alphacode1 << 1) & 0x06)
|
| 208 |
+
else: # alphacode_index >= 18 and alphacode_index <= 45
|
| 209 |
+
alphacode = (alphacode1 >> (alphacode_index - 16)) & 0x07
|
| 210 |
+
|
| 211 |
+
if alphacode == 0:
|
| 212 |
+
a = a0
|
| 213 |
+
elif alphacode == 1:
|
| 214 |
+
a = a1
|
| 215 |
+
elif a0 > a1:
|
| 216 |
+
a = ((8 - alphacode) * a0 + (alphacode - 1) * a1) // 7
|
| 217 |
+
elif alphacode == 6:
|
| 218 |
+
a = 0
|
| 219 |
+
elif alphacode == 7:
|
| 220 |
+
a = 255
|
| 221 |
+
else:
|
| 222 |
+
a = ((6 - alphacode) * a0 + (alphacode - 1) * a1) // 5
|
| 223 |
+
|
| 224 |
+
color_code = (code >> 2 * (4 * j + i)) & 0x03
|
| 225 |
+
|
| 226 |
+
if color_code == 0:
|
| 227 |
+
r, g, b = r0, g0, b0
|
| 228 |
+
elif color_code == 1:
|
| 229 |
+
r, g, b = r1, g1, b1
|
| 230 |
+
elif color_code == 2:
|
| 231 |
+
r = (2 * r0 + r1) // 3
|
| 232 |
+
g = (2 * g0 + g1) // 3
|
| 233 |
+
b = (2 * b0 + b1) // 3
|
| 234 |
+
elif color_code == 3:
|
| 235 |
+
r = (2 * r1 + r0) // 3
|
| 236 |
+
g = (2 * g1 + g0) // 3
|
| 237 |
+
b = (2 * b1 + b0) // 3
|
| 238 |
+
|
| 239 |
+
ret[j].extend([r, g, b, a])
|
| 240 |
+
|
| 241 |
+
return ret
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
class BLPFormatError(NotImplementedError):
|
| 245 |
+
pass
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def _accept(prefix: bytes) -> bool:
|
| 249 |
+
return prefix[:4] in (b"BLP1", b"BLP2")
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
class BlpImageFile(ImageFile.ImageFile):
|
| 253 |
+
"""
|
| 254 |
+
Blizzard Mipmap Format
|
| 255 |
+
"""
|
| 256 |
+
|
| 257 |
+
format = "BLP"
|
| 258 |
+
format_description = "Blizzard Mipmap Format"
|
| 259 |
+
|
| 260 |
+
def _open(self) -> None:
|
| 261 |
+
self.magic = self.fp.read(4)
|
| 262 |
+
|
| 263 |
+
self.fp.seek(5, os.SEEK_CUR)
|
| 264 |
+
(self._blp_alpha_depth,) = struct.unpack("<b", self.fp.read(1))
|
| 265 |
+
|
| 266 |
+
self.fp.seek(2, os.SEEK_CUR)
|
| 267 |
+
self._size = struct.unpack("<II", self.fp.read(8))
|
| 268 |
+
|
| 269 |
+
if self.magic in (b"BLP1", b"BLP2"):
|
| 270 |
+
decoder = self.magic.decode()
|
| 271 |
+
else:
|
| 272 |
+
msg = f"Bad BLP magic {repr(self.magic)}"
|
| 273 |
+
raise BLPFormatError(msg)
|
| 274 |
+
|
| 275 |
+
self._mode = "RGBA" if self._blp_alpha_depth else "RGB"
|
| 276 |
+
self.tile = [(decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))]
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
class _BLPBaseDecoder(ImageFile.PyDecoder):
|
| 280 |
+
_pulls_fd = True
|
| 281 |
+
|
| 282 |
+
def decode(self, buffer: bytes) -> tuple[int, int]:
|
| 283 |
+
try:
|
| 284 |
+
self._read_blp_header()
|
| 285 |
+
self._load()
|
| 286 |
+
except struct.error as e:
|
| 287 |
+
msg = "Truncated BLP file"
|
| 288 |
+
raise OSError(msg) from e
|
| 289 |
+
return -1, 0
|
| 290 |
+
|
| 291 |
+
@abc.abstractmethod
|
| 292 |
+
def _load(self) -> None:
|
| 293 |
+
pass
|
| 294 |
+
|
| 295 |
+
def _read_blp_header(self) -> None:
|
| 296 |
+
assert self.fd is not None
|
| 297 |
+
self.fd.seek(4)
|
| 298 |
+
(self._blp_compression,) = struct.unpack("<i", self._safe_read(4))
|
| 299 |
+
|
| 300 |
+
(self._blp_encoding,) = struct.unpack("<b", self._safe_read(1))
|
| 301 |
+
(self._blp_alpha_depth,) = struct.unpack("<b", self._safe_read(1))
|
| 302 |
+
(self._blp_alpha_encoding,) = struct.unpack("<b", self._safe_read(1))
|
| 303 |
+
self.fd.seek(1, os.SEEK_CUR) # mips
|
| 304 |
+
|
| 305 |
+
self.size = struct.unpack("<II", self._safe_read(8))
|
| 306 |
+
|
| 307 |
+
if isinstance(self, BLP1Decoder):
|
| 308 |
+
# Only present for BLP1
|
| 309 |
+
(self._blp_encoding,) = struct.unpack("<i", self._safe_read(4))
|
| 310 |
+
self.fd.seek(4, os.SEEK_CUR) # subtype
|
| 311 |
+
|
| 312 |
+
self._blp_offsets = struct.unpack("<16I", self._safe_read(16 * 4))
|
| 313 |
+
self._blp_lengths = struct.unpack("<16I", self._safe_read(16 * 4))
|
| 314 |
+
|
| 315 |
+
def _safe_read(self, length: int) -> bytes:
|
| 316 |
+
return ImageFile._safe_read(self.fd, length)
|
| 317 |
+
|
| 318 |
+
def _read_palette(self) -> list[tuple[int, int, int, int]]:
|
| 319 |
+
ret = []
|
| 320 |
+
for i in range(256):
|
| 321 |
+
try:
|
| 322 |
+
b, g, r, a = struct.unpack("<4B", self._safe_read(4))
|
| 323 |
+
except struct.error:
|
| 324 |
+
break
|
| 325 |
+
ret.append((b, g, r, a))
|
| 326 |
+
return ret
|
| 327 |
+
|
| 328 |
+
def _read_bgra(self, palette: list[tuple[int, int, int, int]]) -> bytearray:
|
| 329 |
+
data = bytearray()
|
| 330 |
+
_data = BytesIO(self._safe_read(self._blp_lengths[0]))
|
| 331 |
+
while True:
|
| 332 |
+
try:
|
| 333 |
+
(offset,) = struct.unpack("<B", _data.read(1))
|
| 334 |
+
except struct.error:
|
| 335 |
+
break
|
| 336 |
+
b, g, r, a = palette[offset]
|
| 337 |
+
d: tuple[int, ...] = (r, g, b)
|
| 338 |
+
if self._blp_alpha_depth:
|
| 339 |
+
d += (a,)
|
| 340 |
+
data.extend(d)
|
| 341 |
+
return data
|
| 342 |
+
|
| 343 |
+
|
| 344 |
+
class BLP1Decoder(_BLPBaseDecoder):
|
| 345 |
+
def _load(self) -> None:
|
| 346 |
+
if self._blp_compression == Format.JPEG:
|
| 347 |
+
self._decode_jpeg_stream()
|
| 348 |
+
|
| 349 |
+
elif self._blp_compression == 1:
|
| 350 |
+
if self._blp_encoding in (4, 5):
|
| 351 |
+
palette = self._read_palette()
|
| 352 |
+
data = self._read_bgra(palette)
|
| 353 |
+
self.set_as_raw(data)
|
| 354 |
+
else:
|
| 355 |
+
msg = f"Unsupported BLP encoding {repr(self._blp_encoding)}"
|
| 356 |
+
raise BLPFormatError(msg)
|
| 357 |
+
else:
|
| 358 |
+
msg = f"Unsupported BLP compression {repr(self._blp_encoding)}"
|
| 359 |
+
raise BLPFormatError(msg)
|
| 360 |
+
|
| 361 |
+
def _decode_jpeg_stream(self) -> None:
|
| 362 |
+
from .JpegImagePlugin import JpegImageFile
|
| 363 |
+
|
| 364 |
+
(jpeg_header_size,) = struct.unpack("<I", self._safe_read(4))
|
| 365 |
+
jpeg_header = self._safe_read(jpeg_header_size)
|
| 366 |
+
assert self.fd is not None
|
| 367 |
+
self._safe_read(self._blp_offsets[0] - self.fd.tell()) # What IS this?
|
| 368 |
+
data = self._safe_read(self._blp_lengths[0])
|
| 369 |
+
data = jpeg_header + data
|
| 370 |
+
image = JpegImageFile(BytesIO(data))
|
| 371 |
+
Image._decompression_bomb_check(image.size)
|
| 372 |
+
if image.mode == "CMYK":
|
| 373 |
+
decoder_name, extents, offset, args = image.tile[0]
|
| 374 |
+
image.tile = [(decoder_name, extents, offset, (args[0], "CMYK"))]
|
| 375 |
+
r, g, b = image.convert("RGB").split()
|
| 376 |
+
reversed_image = Image.merge("RGB", (b, g, r))
|
| 377 |
+
self.set_as_raw(reversed_image.tobytes())
|
| 378 |
+
|
| 379 |
+
|
| 380 |
+
class BLP2Decoder(_BLPBaseDecoder):
|
| 381 |
+
def _load(self) -> None:
|
| 382 |
+
palette = self._read_palette()
|
| 383 |
+
|
| 384 |
+
assert self.fd is not None
|
| 385 |
+
self.fd.seek(self._blp_offsets[0])
|
| 386 |
+
|
| 387 |
+
if self._blp_compression == 1:
|
| 388 |
+
# Uncompressed or DirectX compression
|
| 389 |
+
|
| 390 |
+
if self._blp_encoding == Encoding.UNCOMPRESSED:
|
| 391 |
+
data = self._read_bgra(palette)
|
| 392 |
+
|
| 393 |
+
elif self._blp_encoding == Encoding.DXT:
|
| 394 |
+
data = bytearray()
|
| 395 |
+
if self._blp_alpha_encoding == AlphaEncoding.DXT1:
|
| 396 |
+
linesize = (self.size[0] + 3) // 4 * 8
|
| 397 |
+
for yb in range((self.size[1] + 3) // 4):
|
| 398 |
+
for d in decode_dxt1(
|
| 399 |
+
self._safe_read(linesize), alpha=bool(self._blp_alpha_depth)
|
| 400 |
+
):
|
| 401 |
+
data += d
|
| 402 |
+
|
| 403 |
+
elif self._blp_alpha_encoding == AlphaEncoding.DXT3:
|
| 404 |
+
linesize = (self.size[0] + 3) // 4 * 16
|
| 405 |
+
for yb in range((self.size[1] + 3) // 4):
|
| 406 |
+
for d in decode_dxt3(self._safe_read(linesize)):
|
| 407 |
+
data += d
|
| 408 |
+
|
| 409 |
+
elif self._blp_alpha_encoding == AlphaEncoding.DXT5:
|
| 410 |
+
linesize = (self.size[0] + 3) // 4 * 16
|
| 411 |
+
for yb in range((self.size[1] + 3) // 4):
|
| 412 |
+
for d in decode_dxt5(self._safe_read(linesize)):
|
| 413 |
+
data += d
|
| 414 |
+
else:
|
| 415 |
+
msg = f"Unsupported alpha encoding {repr(self._blp_alpha_encoding)}"
|
| 416 |
+
raise BLPFormatError(msg)
|
| 417 |
+
else:
|
| 418 |
+
msg = f"Unknown BLP encoding {repr(self._blp_encoding)}"
|
| 419 |
+
raise BLPFormatError(msg)
|
| 420 |
+
|
| 421 |
+
else:
|
| 422 |
+
msg = f"Unknown BLP compression {repr(self._blp_compression)}"
|
| 423 |
+
raise BLPFormatError(msg)
|
| 424 |
+
|
| 425 |
+
self.set_as_raw(data)
|
| 426 |
+
|
| 427 |
+
|
| 428 |
+
class BLPEncoder(ImageFile.PyEncoder):
|
| 429 |
+
_pushes_fd = True
|
| 430 |
+
|
| 431 |
+
def _write_palette(self) -> bytes:
|
| 432 |
+
data = b""
|
| 433 |
+
assert self.im is not None
|
| 434 |
+
palette = self.im.getpalette("RGBA", "RGBA")
|
| 435 |
+
for i in range(len(palette) // 4):
|
| 436 |
+
r, g, b, a = palette[i * 4 : (i + 1) * 4]
|
| 437 |
+
data += struct.pack("<4B", b, g, r, a)
|
| 438 |
+
while len(data) < 256 * 4:
|
| 439 |
+
data += b"\x00" * 4
|
| 440 |
+
return data
|
| 441 |
+
|
| 442 |
+
def encode(self, bufsize: int) -> tuple[int, int, bytes]:
|
| 443 |
+
palette_data = self._write_palette()
|
| 444 |
+
|
| 445 |
+
offset = 20 + 16 * 4 * 2 + len(palette_data)
|
| 446 |
+
data = struct.pack("<16I", offset, *((0,) * 15))
|
| 447 |
+
|
| 448 |
+
assert self.im is not None
|
| 449 |
+
w, h = self.im.size
|
| 450 |
+
data += struct.pack("<16I", w * h, *((0,) * 15))
|
| 451 |
+
|
| 452 |
+
data += palette_data
|
| 453 |
+
|
| 454 |
+
for y in range(h):
|
| 455 |
+
for x in range(w):
|
| 456 |
+
data += struct.pack("<B", self.im.getpixel((x, y)))
|
| 457 |
+
|
| 458 |
+
return len(data), 0, data
|
| 459 |
+
|
| 460 |
+
|
| 461 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 462 |
+
if im.mode != "P":
|
| 463 |
+
msg = "Unsupported BLP image mode"
|
| 464 |
+
raise ValueError(msg)
|
| 465 |
+
|
| 466 |
+
magic = b"BLP1" if im.encoderinfo.get("blp_version") == "BLP1" else b"BLP2"
|
| 467 |
+
fp.write(magic)
|
| 468 |
+
|
| 469 |
+
fp.write(struct.pack("<i", 1)) # Uncompressed or DirectX compression
|
| 470 |
+
fp.write(struct.pack("<b", Encoding.UNCOMPRESSED))
|
| 471 |
+
fp.write(struct.pack("<b", 1 if im.palette.mode == "RGBA" else 0))
|
| 472 |
+
fp.write(struct.pack("<b", 0)) # alpha encoding
|
| 473 |
+
fp.write(struct.pack("<b", 0)) # mips
|
| 474 |
+
fp.write(struct.pack("<II", *im.size))
|
| 475 |
+
if magic == b"BLP1":
|
| 476 |
+
fp.write(struct.pack("<i", 5))
|
| 477 |
+
fp.write(struct.pack("<i", 0))
|
| 478 |
+
|
| 479 |
+
ImageFile._save(im, fp, [("BLP", (0, 0) + im.size, 0, im.mode)])
|
| 480 |
+
|
| 481 |
+
|
| 482 |
+
Image.register_open(BlpImageFile.format, BlpImageFile, _accept)
|
| 483 |
+
Image.register_extension(BlpImageFile.format, ".blp")
|
| 484 |
+
Image.register_decoder("BLP1", BLP1Decoder)
|
| 485 |
+
Image.register_decoder("BLP2", BLP2Decoder)
|
| 486 |
+
|
| 487 |
+
Image.register_save(BlpImageFile.format, _save)
|
| 488 |
+
Image.register_encoder("BLP", BLPEncoder)
|
.venv/lib/python3.11/site-packages/PIL/EpsImagePlugin.py
ADDED
|
@@ -0,0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|
+
# 1996-08-22 fl Don't choke on floating point BoundingBox values
|
| 11 |
+
# 1996-08-23 fl Handle files from Macintosh (0.3)
|
| 12 |
+
# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.4)
|
| 13 |
+
# 2003-09-07 fl Check gs.close status (from Federico Di Gregorio) (0.5)
|
| 14 |
+
# 2014-05-07 e Handling of EPS with binary preview and fixed resolution
|
| 15 |
+
# resizing
|
| 16 |
+
#
|
| 17 |
+
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 18 |
+
# Copyright (c) 1995-2003 by Fredrik Lundh
|
| 19 |
+
#
|
| 20 |
+
# See the README file for information on usage and redistribution.
|
| 21 |
+
#
|
| 22 |
+
from __future__ import annotations
|
| 23 |
+
|
| 24 |
+
import io
|
| 25 |
+
import os
|
| 26 |
+
import re
|
| 27 |
+
import subprocess
|
| 28 |
+
import sys
|
| 29 |
+
import tempfile
|
| 30 |
+
from typing import IO
|
| 31 |
+
|
| 32 |
+
from . import Image, ImageFile
|
| 33 |
+
from ._binary import i32le as i32
|
| 34 |
+
from ._deprecate import deprecate
|
| 35 |
+
|
| 36 |
+
# --------------------------------------------------------------------
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
split = re.compile(r"^%%([^:]*):[ \t]*(.*)[ \t]*$")
|
| 40 |
+
field = re.compile(r"^%[%!\w]([^:]*)[ \t]*$")
|
| 41 |
+
|
| 42 |
+
gs_binary: str | bool | None = None
|
| 43 |
+
gs_windows_binary = None
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def has_ghostscript() -> bool:
|
| 47 |
+
global gs_binary, gs_windows_binary
|
| 48 |
+
if gs_binary is None:
|
| 49 |
+
if sys.platform.startswith("win"):
|
| 50 |
+
if gs_windows_binary is None:
|
| 51 |
+
import shutil
|
| 52 |
+
|
| 53 |
+
for binary in ("gswin32c", "gswin64c", "gs"):
|
| 54 |
+
if shutil.which(binary) is not None:
|
| 55 |
+
gs_windows_binary = binary
|
| 56 |
+
break
|
| 57 |
+
else:
|
| 58 |
+
gs_windows_binary = False
|
| 59 |
+
gs_binary = gs_windows_binary
|
| 60 |
+
else:
|
| 61 |
+
try:
|
| 62 |
+
subprocess.check_call(["gs", "--version"], stdout=subprocess.DEVNULL)
|
| 63 |
+
gs_binary = "gs"
|
| 64 |
+
except OSError:
|
| 65 |
+
gs_binary = False
|
| 66 |
+
return gs_binary is not False
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def Ghostscript(tile, size, fp, scale=1, transparency=False):
|
| 70 |
+
"""Render an image using Ghostscript"""
|
| 71 |
+
global gs_binary
|
| 72 |
+
if not has_ghostscript():
|
| 73 |
+
msg = "Unable to locate Ghostscript on paths"
|
| 74 |
+
raise OSError(msg)
|
| 75 |
+
|
| 76 |
+
# Unpack decoder tile
|
| 77 |
+
decoder, tile, offset, data = tile[0]
|
| 78 |
+
length, bbox = data
|
| 79 |
+
|
| 80 |
+
# Hack to support hi-res rendering
|
| 81 |
+
scale = int(scale) or 1
|
| 82 |
+
width = size[0] * scale
|
| 83 |
+
height = size[1] * scale
|
| 84 |
+
# resolution is dependent on bbox and size
|
| 85 |
+
res_x = 72.0 * width / (bbox[2] - bbox[0])
|
| 86 |
+
res_y = 72.0 * height / (bbox[3] - bbox[1])
|
| 87 |
+
|
| 88 |
+
out_fd, outfile = tempfile.mkstemp()
|
| 89 |
+
os.close(out_fd)
|
| 90 |
+
|
| 91 |
+
infile_temp = None
|
| 92 |
+
if hasattr(fp, "name") and os.path.exists(fp.name):
|
| 93 |
+
infile = fp.name
|
| 94 |
+
else:
|
| 95 |
+
in_fd, infile_temp = tempfile.mkstemp()
|
| 96 |
+
os.close(in_fd)
|
| 97 |
+
infile = infile_temp
|
| 98 |
+
|
| 99 |
+
# Ignore length and offset!
|
| 100 |
+
# Ghostscript can read it
|
| 101 |
+
# Copy whole file to read in Ghostscript
|
| 102 |
+
with open(infile_temp, "wb") as f:
|
| 103 |
+
# fetch length of fp
|
| 104 |
+
fp.seek(0, io.SEEK_END)
|
| 105 |
+
fsize = fp.tell()
|
| 106 |
+
# ensure start position
|
| 107 |
+
# go back
|
| 108 |
+
fp.seek(0)
|
| 109 |
+
lengthfile = fsize
|
| 110 |
+
while lengthfile > 0:
|
| 111 |
+
s = fp.read(min(lengthfile, 100 * 1024))
|
| 112 |
+
if not s:
|
| 113 |
+
break
|
| 114 |
+
lengthfile -= len(s)
|
| 115 |
+
f.write(s)
|
| 116 |
+
|
| 117 |
+
device = "pngalpha" if transparency else "ppmraw"
|
| 118 |
+
|
| 119 |
+
# Build Ghostscript command
|
| 120 |
+
command = [
|
| 121 |
+
gs_binary,
|
| 122 |
+
"-q", # quiet mode
|
| 123 |
+
f"-g{width:d}x{height:d}", # set output geometry (pixels)
|
| 124 |
+
f"-r{res_x:f}x{res_y:f}", # set input DPI (dots per inch)
|
| 125 |
+
"-dBATCH", # exit after processing
|
| 126 |
+
"-dNOPAUSE", # don't pause between pages
|
| 127 |
+
"-dSAFER", # safe mode
|
| 128 |
+
f"-sDEVICE={device}",
|
| 129 |
+
f"-sOutputFile={outfile}", # output file
|
| 130 |
+
# adjust for image origin
|
| 131 |
+
"-c",
|
| 132 |
+
f"{-bbox[0]} {-bbox[1]} translate",
|
| 133 |
+
"-f",
|
| 134 |
+
infile, # input file
|
| 135 |
+
# showpage (see https://bugs.ghostscript.com/show_bug.cgi?id=698272)
|
| 136 |
+
"-c",
|
| 137 |
+
"showpage",
|
| 138 |
+
]
|
| 139 |
+
|
| 140 |
+
# push data through Ghostscript
|
| 141 |
+
try:
|
| 142 |
+
startupinfo = None
|
| 143 |
+
if sys.platform.startswith("win"):
|
| 144 |
+
startupinfo = subprocess.STARTUPINFO()
|
| 145 |
+
startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
|
| 146 |
+
subprocess.check_call(command, startupinfo=startupinfo)
|
| 147 |
+
out_im = Image.open(outfile)
|
| 148 |
+
out_im.load()
|
| 149 |
+
finally:
|
| 150 |
+
try:
|
| 151 |
+
os.unlink(outfile)
|
| 152 |
+
if infile_temp:
|
| 153 |
+
os.unlink(infile_temp)
|
| 154 |
+
except OSError:
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
im = out_im.im.copy()
|
| 158 |
+
out_im.close()
|
| 159 |
+
return im
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
class PSFile:
|
| 163 |
+
"""
|
| 164 |
+
Wrapper for bytesio object that treats either CR or LF as end of line.
|
| 165 |
+
This class is no longer used internally, but kept for backwards compatibility.
|
| 166 |
+
"""
|
| 167 |
+
|
| 168 |
+
def __init__(self, fp):
|
| 169 |
+
deprecate(
|
| 170 |
+
"PSFile",
|
| 171 |
+
11,
|
| 172 |
+
action="If you need the functionality of this class "
|
| 173 |
+
"you will need to implement it yourself.",
|
| 174 |
+
)
|
| 175 |
+
self.fp = fp
|
| 176 |
+
self.char = None
|
| 177 |
+
|
| 178 |
+
def seek(self, offset, whence=io.SEEK_SET):
|
| 179 |
+
self.char = None
|
| 180 |
+
self.fp.seek(offset, whence)
|
| 181 |
+
|
| 182 |
+
def readline(self) -> str:
|
| 183 |
+
s = [self.char or b""]
|
| 184 |
+
self.char = None
|
| 185 |
+
|
| 186 |
+
c = self.fp.read(1)
|
| 187 |
+
while (c not in b"\r\n") and len(c):
|
| 188 |
+
s.append(c)
|
| 189 |
+
c = self.fp.read(1)
|
| 190 |
+
|
| 191 |
+
self.char = self.fp.read(1)
|
| 192 |
+
# line endings can be 1 or 2 of \r \n, in either order
|
| 193 |
+
if self.char in b"\r\n":
|
| 194 |
+
self.char = None
|
| 195 |
+
|
| 196 |
+
return b"".join(s).decode("latin-1")
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def _accept(prefix: bytes) -> bool:
|
| 200 |
+
return prefix[:4] == b"%!PS" or (len(prefix) >= 4 and i32(prefix) == 0xC6D3D0C5)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
##
|
| 204 |
+
# Image plugin for Encapsulated PostScript. This plugin supports only
|
| 205 |
+
# a few variants of this format.
|
| 206 |
+
|
| 207 |
+
|
| 208 |
+
class EpsImageFile(ImageFile.ImageFile):
|
| 209 |
+
"""EPS File Parser for the Python Imaging Library"""
|
| 210 |
+
|
| 211 |
+
format = "EPS"
|
| 212 |
+
format_description = "Encapsulated Postscript"
|
| 213 |
+
|
| 214 |
+
mode_map = {1: "L", 2: "LAB", 3: "RGB", 4: "CMYK"}
|
| 215 |
+
|
| 216 |
+
def _open(self) -> None:
|
| 217 |
+
(length, offset) = self._find_offset(self.fp)
|
| 218 |
+
|
| 219 |
+
# go to offset - start of "%!PS"
|
| 220 |
+
self.fp.seek(offset)
|
| 221 |
+
|
| 222 |
+
self._mode = "RGB"
|
| 223 |
+
self._size = None
|
| 224 |
+
|
| 225 |
+
byte_arr = bytearray(255)
|
| 226 |
+
bytes_mv = memoryview(byte_arr)
|
| 227 |
+
bytes_read = 0
|
| 228 |
+
reading_header_comments = True
|
| 229 |
+
reading_trailer_comments = False
|
| 230 |
+
trailer_reached = False
|
| 231 |
+
|
| 232 |
+
def check_required_header_comments() -> None:
|
| 233 |
+
"""
|
| 234 |
+
The EPS specification requires that some headers exist.
|
| 235 |
+
This should be checked when the header comments formally end,
|
| 236 |
+
when image data starts, or when the file ends, whichever comes first.
|
| 237 |
+
"""
|
| 238 |
+
if "PS-Adobe" not in self.info:
|
| 239 |
+
msg = 'EPS header missing "%!PS-Adobe" comment'
|
| 240 |
+
raise SyntaxError(msg)
|
| 241 |
+
if "BoundingBox" not in self.info:
|
| 242 |
+
msg = 'EPS header missing "%%BoundingBox" comment'
|
| 243 |
+
raise SyntaxError(msg)
|
| 244 |
+
|
| 245 |
+
def _read_comment(s: str) -> bool:
|
| 246 |
+
nonlocal reading_trailer_comments
|
| 247 |
+
try:
|
| 248 |
+
m = split.match(s)
|
| 249 |
+
except re.error as e:
|
| 250 |
+
msg = "not an EPS file"
|
| 251 |
+
raise SyntaxError(msg) from e
|
| 252 |
+
|
| 253 |
+
if not m:
|
| 254 |
+
return False
|
| 255 |
+
|
| 256 |
+
k, v = m.group(1, 2)
|
| 257 |
+
self.info[k] = v
|
| 258 |
+
if k == "BoundingBox":
|
| 259 |
+
if v == "(atend)":
|
| 260 |
+
reading_trailer_comments = True
|
| 261 |
+
elif not self._size or (trailer_reached and reading_trailer_comments):
|
| 262 |
+
try:
|
| 263 |
+
# Note: The DSC spec says that BoundingBox
|
| 264 |
+
# fields should be integers, but some drivers
|
| 265 |
+
# put floating point values there anyway.
|
| 266 |
+
box = [int(float(i)) for i in v.split()]
|
| 267 |
+
self._size = box[2] - box[0], box[3] - box[1]
|
| 268 |
+
self.tile = [("eps", (0, 0) + self.size, offset, (length, box))]
|
| 269 |
+
except Exception:
|
| 270 |
+
pass
|
| 271 |
+
return True
|
| 272 |
+
|
| 273 |
+
while True:
|
| 274 |
+
byte = self.fp.read(1)
|
| 275 |
+
if byte == b"":
|
| 276 |
+
# if we didn't read a byte we must be at the end of the file
|
| 277 |
+
if bytes_read == 0:
|
| 278 |
+
if reading_header_comments:
|
| 279 |
+
check_required_header_comments()
|
| 280 |
+
break
|
| 281 |
+
elif byte in b"\r\n":
|
| 282 |
+
# if we read a line ending character, ignore it and parse what
|
| 283 |
+
# we have already read. if we haven't read any other characters,
|
| 284 |
+
# continue reading
|
| 285 |
+
if bytes_read == 0:
|
| 286 |
+
continue
|
| 287 |
+
else:
|
| 288 |
+
# ASCII/hexadecimal lines in an EPS file must not exceed
|
| 289 |
+
# 255 characters, not including line ending characters
|
| 290 |
+
if bytes_read >= 255:
|
| 291 |
+
# only enforce this for lines starting with a "%",
|
| 292 |
+
# otherwise assume it's binary data
|
| 293 |
+
if byte_arr[0] == ord("%"):
|
| 294 |
+
msg = "not an EPS file"
|
| 295 |
+
raise SyntaxError(msg)
|
| 296 |
+
else:
|
| 297 |
+
if reading_header_comments:
|
| 298 |
+
check_required_header_comments()
|
| 299 |
+
reading_header_comments = False
|
| 300 |
+
# reset bytes_read so we can keep reading
|
| 301 |
+
# data until the end of the line
|
| 302 |
+
bytes_read = 0
|
| 303 |
+
byte_arr[bytes_read] = byte[0]
|
| 304 |
+
bytes_read += 1
|
| 305 |
+
continue
|
| 306 |
+
|
| 307 |
+
if reading_header_comments:
|
| 308 |
+
# Load EPS header
|
| 309 |
+
|
| 310 |
+
# if this line doesn't start with a "%",
|
| 311 |
+
# or does start with "%%EndComments",
|
| 312 |
+
# then we've reached the end of the header/comments
|
| 313 |
+
if byte_arr[0] != ord("%") or bytes_mv[:13] == b"%%EndComments":
|
| 314 |
+
check_required_header_comments()
|
| 315 |
+
reading_header_comments = False
|
| 316 |
+
continue
|
| 317 |
+
|
| 318 |
+
s = str(bytes_mv[:bytes_read], "latin-1")
|
| 319 |
+
if not _read_comment(s):
|
| 320 |
+
m = field.match(s)
|
| 321 |
+
if m:
|
| 322 |
+
k = m.group(1)
|
| 323 |
+
if k[:8] == "PS-Adobe":
|
| 324 |
+
self.info["PS-Adobe"] = k[9:]
|
| 325 |
+
else:
|
| 326 |
+
self.info[k] = ""
|
| 327 |
+
elif s[0] == "%":
|
| 328 |
+
# handle non-DSC PostScript comments that some
|
| 329 |
+
# tools mistakenly put in the Comments section
|
| 330 |
+
pass
|
| 331 |
+
else:
|
| 332 |
+
msg = "bad EPS header"
|
| 333 |
+
raise OSError(msg)
|
| 334 |
+
elif bytes_mv[:11] == b"%ImageData:":
|
| 335 |
+
# Check for an "ImageData" descriptor
|
| 336 |
+
# https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#50577413_pgfId-1035096
|
| 337 |
+
|
| 338 |
+
# Values:
|
| 339 |
+
# columns
|
| 340 |
+
# rows
|
| 341 |
+
# bit depth (1 or 8)
|
| 342 |
+
# mode (1: L, 2: LAB, 3: RGB, 4: CMYK)
|
| 343 |
+
# number of padding channels
|
| 344 |
+
# block size (number of bytes per row per channel)
|
| 345 |
+
# binary/ascii (1: binary, 2: ascii)
|
| 346 |
+
# data start identifier (the image data follows after a single line
|
| 347 |
+
# consisting only of this quoted value)
|
| 348 |
+
image_data_values = byte_arr[11:bytes_read].split(None, 7)
|
| 349 |
+
columns, rows, bit_depth, mode_id = (
|
| 350 |
+
int(value) for value in image_data_values[:4]
|
| 351 |
+
)
|
| 352 |
+
|
| 353 |
+
if bit_depth == 1:
|
| 354 |
+
self._mode = "1"
|
| 355 |
+
elif bit_depth == 8:
|
| 356 |
+
try:
|
| 357 |
+
self._mode = self.mode_map[mode_id]
|
| 358 |
+
except ValueError:
|
| 359 |
+
break
|
| 360 |
+
else:
|
| 361 |
+
break
|
| 362 |
+
|
| 363 |
+
self._size = columns, rows
|
| 364 |
+
return
|
| 365 |
+
elif bytes_mv[:5] == b"%%EOF":
|
| 366 |
+
break
|
| 367 |
+
elif trailer_reached and reading_trailer_comments:
|
| 368 |
+
# Load EPS trailer
|
| 369 |
+
s = str(bytes_mv[:bytes_read], "latin-1")
|
| 370 |
+
_read_comment(s)
|
| 371 |
+
elif bytes_mv[:9] == b"%%Trailer":
|
| 372 |
+
trailer_reached = True
|
| 373 |
+
bytes_read = 0
|
| 374 |
+
|
| 375 |
+
if not self._size:
|
| 376 |
+
msg = "cannot determine EPS bounding box"
|
| 377 |
+
raise OSError(msg)
|
| 378 |
+
|
| 379 |
+
def _find_offset(self, fp):
|
| 380 |
+
s = fp.read(4)
|
| 381 |
+
|
| 382 |
+
if s == b"%!PS":
|
| 383 |
+
# for HEAD without binary preview
|
| 384 |
+
fp.seek(0, io.SEEK_END)
|
| 385 |
+
length = fp.tell()
|
| 386 |
+
offset = 0
|
| 387 |
+
elif i32(s) == 0xC6D3D0C5:
|
| 388 |
+
# FIX for: Some EPS file not handled correctly / issue #302
|
| 389 |
+
# EPS can contain binary data
|
| 390 |
+
# or start directly with latin coding
|
| 391 |
+
# more info see:
|
| 392 |
+
# https://web.archive.org/web/20160528181353/http://partners.adobe.com/public/developer/en/ps/5002.EPSF_Spec.pdf
|
| 393 |
+
s = fp.read(8)
|
| 394 |
+
offset = i32(s)
|
| 395 |
+
length = i32(s, 4)
|
| 396 |
+
else:
|
| 397 |
+
msg = "not an EPS file"
|
| 398 |
+
raise SyntaxError(msg)
|
| 399 |
+
|
| 400 |
+
return length, offset
|
| 401 |
+
|
| 402 |
+
def load(self, scale=1, transparency=False):
|
| 403 |
+
# Load EPS via Ghostscript
|
| 404 |
+
if self.tile:
|
| 405 |
+
self.im = Ghostscript(self.tile, self.size, self.fp, scale, transparency)
|
| 406 |
+
self._mode = self.im.mode
|
| 407 |
+
self._size = self.im.size
|
| 408 |
+
self.tile = []
|
| 409 |
+
return Image.Image.load(self)
|
| 410 |
+
|
| 411 |
+
def load_seek(self, pos: int) -> None:
|
| 412 |
+
# we can't incrementally load, so force ImageFile.parser to
|
| 413 |
+
# use our custom load method by defining this method.
|
| 414 |
+
pass
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
# --------------------------------------------------------------------
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes, eps: int = 1) -> None:
|
| 421 |
+
"""EPS Writer for the Python Imaging Library."""
|
| 422 |
+
|
| 423 |
+
# make sure image data is available
|
| 424 |
+
im.load()
|
| 425 |
+
|
| 426 |
+
# determine PostScript image mode
|
| 427 |
+
if im.mode == "L":
|
| 428 |
+
operator = (8, 1, b"image")
|
| 429 |
+
elif im.mode == "RGB":
|
| 430 |
+
operator = (8, 3, b"false 3 colorimage")
|
| 431 |
+
elif im.mode == "CMYK":
|
| 432 |
+
operator = (8, 4, b"false 4 colorimage")
|
| 433 |
+
else:
|
| 434 |
+
msg = "image mode is not supported"
|
| 435 |
+
raise ValueError(msg)
|
| 436 |
+
|
| 437 |
+
if eps:
|
| 438 |
+
# write EPS header
|
| 439 |
+
fp.write(b"%!PS-Adobe-3.0 EPSF-3.0\n")
|
| 440 |
+
fp.write(b"%%Creator: PIL 0.1 EpsEncode\n")
|
| 441 |
+
# fp.write("%%CreationDate: %s"...)
|
| 442 |
+
fp.write(b"%%%%BoundingBox: 0 0 %d %d\n" % im.size)
|
| 443 |
+
fp.write(b"%%Pages: 1\n")
|
| 444 |
+
fp.write(b"%%EndComments\n")
|
| 445 |
+
fp.write(b"%%Page: 1 1\n")
|
| 446 |
+
fp.write(b"%%ImageData: %d %d " % im.size)
|
| 447 |
+
fp.write(b'%d %d 0 1 1 "%s"\n' % operator)
|
| 448 |
+
|
| 449 |
+
# image header
|
| 450 |
+
fp.write(b"gsave\n")
|
| 451 |
+
fp.write(b"10 dict begin\n")
|
| 452 |
+
fp.write(b"/buf %d string def\n" % (im.size[0] * operator[1]))
|
| 453 |
+
fp.write(b"%d %d scale\n" % im.size)
|
| 454 |
+
fp.write(b"%d %d 8\n" % im.size) # <= bits
|
| 455 |
+
fp.write(b"[%d 0 0 -%d 0 %d]\n" % (im.size[0], im.size[1], im.size[1]))
|
| 456 |
+
fp.write(b"{ currentfile buf readhexstring pop } bind\n")
|
| 457 |
+
fp.write(operator[2] + b"\n")
|
| 458 |
+
if hasattr(fp, "flush"):
|
| 459 |
+
fp.flush()
|
| 460 |
+
|
| 461 |
+
ImageFile._save(im, fp, [("eps", (0, 0) + im.size, 0, None)])
|
| 462 |
+
|
| 463 |
+
fp.write(b"\n%%%%EndBinary\n")
|
| 464 |
+
fp.write(b"grestore end\n")
|
| 465 |
+
if hasattr(fp, "flush"):
|
| 466 |
+
fp.flush()
|
| 467 |
+
|
| 468 |
+
|
| 469 |
+
# --------------------------------------------------------------------
|
| 470 |
+
|
| 471 |
+
|
| 472 |
+
Image.register_open(EpsImageFile.format, EpsImageFile, _accept)
|
| 473 |
+
|
| 474 |
+
Image.register_save(EpsImageFile.format, _save)
|
| 475 |
+
|
| 476 |
+
Image.register_extensions(EpsImageFile.format, [".ps", ".eps"])
|
| 477 |
+
|
| 478 |
+
Image.register_mime(EpsImageFile.format, "application/postscript")
|
.venv/lib/python3.11/site-packages/PIL/FitsImagePlugin.py
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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information on usage and redistribution.
|
| 10 |
+
#
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
import gzip
|
| 14 |
+
import math
|
| 15 |
+
|
| 16 |
+
from . import Image, ImageFile
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def _accept(prefix: bytes) -> bool:
|
| 20 |
+
return prefix[:6] == b"SIMPLE"
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class FitsImageFile(ImageFile.ImageFile):
|
| 24 |
+
format = "FITS"
|
| 25 |
+
format_description = "FITS"
|
| 26 |
+
|
| 27 |
+
def _open(self) -> None:
|
| 28 |
+
assert self.fp is not None
|
| 29 |
+
|
| 30 |
+
headers: dict[bytes, bytes] = {}
|
| 31 |
+
header_in_progress = False
|
| 32 |
+
decoder_name = ""
|
| 33 |
+
while True:
|
| 34 |
+
header = self.fp.read(80)
|
| 35 |
+
if not header:
|
| 36 |
+
msg = "Truncated FITS file"
|
| 37 |
+
raise OSError(msg)
|
| 38 |
+
keyword = header[:8].strip()
|
| 39 |
+
if keyword in (b"SIMPLE", b"XTENSION"):
|
| 40 |
+
header_in_progress = True
|
| 41 |
+
elif headers and not header_in_progress:
|
| 42 |
+
# This is now a data unit
|
| 43 |
+
break
|
| 44 |
+
elif keyword == b"END":
|
| 45 |
+
# Seek to the end of the header unit
|
| 46 |
+
self.fp.seek(math.ceil(self.fp.tell() / 2880) * 2880)
|
| 47 |
+
if not decoder_name:
|
| 48 |
+
decoder_name, offset, args = self._parse_headers(headers)
|
| 49 |
+
|
| 50 |
+
header_in_progress = False
|
| 51 |
+
continue
|
| 52 |
+
|
| 53 |
+
if decoder_name:
|
| 54 |
+
# Keep going to read past the headers
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
value = header[8:].split(b"/")[0].strip()
|
| 58 |
+
if value.startswith(b"="):
|
| 59 |
+
value = value[1:].strip()
|
| 60 |
+
if not headers and (not _accept(keyword) or value != b"T"):
|
| 61 |
+
msg = "Not a FITS file"
|
| 62 |
+
raise SyntaxError(msg)
|
| 63 |
+
headers[keyword] = value
|
| 64 |
+
|
| 65 |
+
if not decoder_name:
|
| 66 |
+
msg = "No image data"
|
| 67 |
+
raise ValueError(msg)
|
| 68 |
+
|
| 69 |
+
offset += self.fp.tell() - 80
|
| 70 |
+
self.tile = [(decoder_name, (0, 0) + self.size, offset, args)]
|
| 71 |
+
|
| 72 |
+
def _get_size(
|
| 73 |
+
self, headers: dict[bytes, bytes], prefix: bytes
|
| 74 |
+
) -> tuple[int, int] | None:
|
| 75 |
+
naxis = int(headers[prefix + b"NAXIS"])
|
| 76 |
+
if naxis == 0:
|
| 77 |
+
return None
|
| 78 |
+
|
| 79 |
+
if naxis == 1:
|
| 80 |
+
return 1, int(headers[prefix + b"NAXIS1"])
|
| 81 |
+
else:
|
| 82 |
+
return int(headers[prefix + b"NAXIS1"]), int(headers[prefix + b"NAXIS2"])
|
| 83 |
+
|
| 84 |
+
def _parse_headers(
|
| 85 |
+
self, headers: dict[bytes, bytes]
|
| 86 |
+
) -> tuple[str, int, tuple[str | int, ...]]:
|
| 87 |
+
prefix = b""
|
| 88 |
+
decoder_name = "raw"
|
| 89 |
+
offset = 0
|
| 90 |
+
if (
|
| 91 |
+
headers.get(b"XTENSION") == b"'BINTABLE'"
|
| 92 |
+
and headers.get(b"ZIMAGE") == b"T"
|
| 93 |
+
and headers[b"ZCMPTYPE"] == b"'GZIP_1 '"
|
| 94 |
+
):
|
| 95 |
+
no_prefix_size = self._get_size(headers, prefix) or (0, 0)
|
| 96 |
+
number_of_bits = int(headers[b"BITPIX"])
|
| 97 |
+
offset = no_prefix_size[0] * no_prefix_size[1] * (number_of_bits // 8)
|
| 98 |
+
|
| 99 |
+
prefix = b"Z"
|
| 100 |
+
decoder_name = "fits_gzip"
|
| 101 |
+
|
| 102 |
+
size = self._get_size(headers, prefix)
|
| 103 |
+
if not size:
|
| 104 |
+
return "", 0, ()
|
| 105 |
+
|
| 106 |
+
self._size = size
|
| 107 |
+
|
| 108 |
+
number_of_bits = int(headers[prefix + b"BITPIX"])
|
| 109 |
+
if number_of_bits == 8:
|
| 110 |
+
self._mode = "L"
|
| 111 |
+
elif number_of_bits == 16:
|
| 112 |
+
self._mode = "I;16"
|
| 113 |
+
elif number_of_bits == 32:
|
| 114 |
+
self._mode = "I"
|
| 115 |
+
elif number_of_bits in (-32, -64):
|
| 116 |
+
self._mode = "F"
|
| 117 |
+
|
| 118 |
+
args: tuple[str | int, ...]
|
| 119 |
+
if decoder_name == "raw":
|
| 120 |
+
args = (self.mode, 0, -1)
|
| 121 |
+
else:
|
| 122 |
+
args = (number_of_bits,)
|
| 123 |
+
return decoder_name, offset, args
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
class FitsGzipDecoder(ImageFile.PyDecoder):
|
| 127 |
+
_pulls_fd = True
|
| 128 |
+
|
| 129 |
+
def decode(self, buffer: bytes) -> tuple[int, int]:
|
| 130 |
+
assert self.fd is not None
|
| 131 |
+
value = gzip.decompress(self.fd.read())
|
| 132 |
+
|
| 133 |
+
rows = []
|
| 134 |
+
offset = 0
|
| 135 |
+
number_of_bits = min(self.args[0] // 8, 4)
|
| 136 |
+
for y in range(self.state.ysize):
|
| 137 |
+
row = bytearray()
|
| 138 |
+
for x in range(self.state.xsize):
|
| 139 |
+
row += value[offset + (4 - number_of_bits) : offset + 4]
|
| 140 |
+
offset += 4
|
| 141 |
+
rows.append(row)
|
| 142 |
+
self.set_as_raw(bytes([pixel for row in rows[::-1] for pixel in row]))
|
| 143 |
+
return -1, 0
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
# --------------------------------------------------------------------
|
| 147 |
+
# Registry
|
| 148 |
+
|
| 149 |
+
Image.register_open(FitsImageFile.format, FitsImageFile, _accept)
|
| 150 |
+
Image.register_decoder("fits_gzip", FitsGzipDecoder)
|
| 151 |
+
|
| 152 |
+
Image.register_extensions(FitsImageFile.format, [".fit", ".fits"])
|
.venv/lib/python3.11/site-packages/PIL/FliImagePlugin.py
ADDED
|
@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# FLI/FLC file handling.
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 95-09-01 fl Created
|
| 9 |
+
# 97-01-03 fl Fixed parser, setup decoder tile
|
| 10 |
+
# 98-07-15 fl Renamed offset attribute to avoid name clash
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) Secret Labs AB 1997-98.
|
| 13 |
+
# Copyright (c) Fredrik Lundh 1995-97.
|
| 14 |
+
#
|
| 15 |
+
# See the README file for information on usage and redistribution.
|
| 16 |
+
#
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import os
|
| 20 |
+
|
| 21 |
+
from . import Image, ImageFile, ImagePalette
|
| 22 |
+
from ._binary import i16le as i16
|
| 23 |
+
from ._binary import i32le as i32
|
| 24 |
+
from ._binary import o8
|
| 25 |
+
|
| 26 |
+
#
|
| 27 |
+
# decoder
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def _accept(prefix: bytes) -> bool:
|
| 31 |
+
return (
|
| 32 |
+
len(prefix) >= 6
|
| 33 |
+
and i16(prefix, 4) in [0xAF11, 0xAF12]
|
| 34 |
+
and i16(prefix, 14) in [0, 3] # flags
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
##
|
| 39 |
+
# Image plugin for the FLI/FLC animation format. Use the <b>seek</b>
|
| 40 |
+
# method to load individual frames.
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
class FliImageFile(ImageFile.ImageFile):
|
| 44 |
+
format = "FLI"
|
| 45 |
+
format_description = "Autodesk FLI/FLC Animation"
|
| 46 |
+
_close_exclusive_fp_after_loading = False
|
| 47 |
+
|
| 48 |
+
def _open(self):
|
| 49 |
+
# HEAD
|
| 50 |
+
s = self.fp.read(128)
|
| 51 |
+
if not (_accept(s) and s[20:22] == b"\x00\x00"):
|
| 52 |
+
msg = "not an FLI/FLC file"
|
| 53 |
+
raise SyntaxError(msg)
|
| 54 |
+
|
| 55 |
+
# frames
|
| 56 |
+
self.n_frames = i16(s, 6)
|
| 57 |
+
self.is_animated = self.n_frames > 1
|
| 58 |
+
|
| 59 |
+
# image characteristics
|
| 60 |
+
self._mode = "P"
|
| 61 |
+
self._size = i16(s, 8), i16(s, 10)
|
| 62 |
+
|
| 63 |
+
# animation speed
|
| 64 |
+
duration = i32(s, 16)
|
| 65 |
+
magic = i16(s, 4)
|
| 66 |
+
if magic == 0xAF11:
|
| 67 |
+
duration = (duration * 1000) // 70
|
| 68 |
+
self.info["duration"] = duration
|
| 69 |
+
|
| 70 |
+
# look for palette
|
| 71 |
+
palette = [(a, a, a) for a in range(256)]
|
| 72 |
+
|
| 73 |
+
s = self.fp.read(16)
|
| 74 |
+
|
| 75 |
+
self.__offset = 128
|
| 76 |
+
|
| 77 |
+
if i16(s, 4) == 0xF100:
|
| 78 |
+
# prefix chunk; ignore it
|
| 79 |
+
self.__offset = self.__offset + i32(s)
|
| 80 |
+
self.fp.seek(self.__offset)
|
| 81 |
+
s = self.fp.read(16)
|
| 82 |
+
|
| 83 |
+
if i16(s, 4) == 0xF1FA:
|
| 84 |
+
# look for palette chunk
|
| 85 |
+
number_of_subchunks = i16(s, 6)
|
| 86 |
+
chunk_size = None
|
| 87 |
+
for _ in range(number_of_subchunks):
|
| 88 |
+
if chunk_size is not None:
|
| 89 |
+
self.fp.seek(chunk_size - 6, os.SEEK_CUR)
|
| 90 |
+
s = self.fp.read(6)
|
| 91 |
+
chunk_type = i16(s, 4)
|
| 92 |
+
if chunk_type in (4, 11):
|
| 93 |
+
self._palette(palette, 2 if chunk_type == 11 else 0)
|
| 94 |
+
break
|
| 95 |
+
chunk_size = i32(s)
|
| 96 |
+
if not chunk_size:
|
| 97 |
+
break
|
| 98 |
+
|
| 99 |
+
palette = [o8(r) + o8(g) + o8(b) for (r, g, b) in palette]
|
| 100 |
+
self.palette = ImagePalette.raw("RGB", b"".join(palette))
|
| 101 |
+
|
| 102 |
+
# set things up to decode first frame
|
| 103 |
+
self.__frame = -1
|
| 104 |
+
self._fp = self.fp
|
| 105 |
+
self.__rewind = self.fp.tell()
|
| 106 |
+
self.seek(0)
|
| 107 |
+
|
| 108 |
+
def _palette(self, palette, shift):
|
| 109 |
+
# load palette
|
| 110 |
+
|
| 111 |
+
i = 0
|
| 112 |
+
for e in range(i16(self.fp.read(2))):
|
| 113 |
+
s = self.fp.read(2)
|
| 114 |
+
i = i + s[0]
|
| 115 |
+
n = s[1]
|
| 116 |
+
if n == 0:
|
| 117 |
+
n = 256
|
| 118 |
+
s = self.fp.read(n * 3)
|
| 119 |
+
for n in range(0, len(s), 3):
|
| 120 |
+
r = s[n] << shift
|
| 121 |
+
g = s[n + 1] << shift
|
| 122 |
+
b = s[n + 2] << shift
|
| 123 |
+
palette[i] = (r, g, b)
|
| 124 |
+
i += 1
|
| 125 |
+
|
| 126 |
+
def seek(self, frame: int) -> None:
|
| 127 |
+
if not self._seek_check(frame):
|
| 128 |
+
return
|
| 129 |
+
if frame < self.__frame:
|
| 130 |
+
self._seek(0)
|
| 131 |
+
|
| 132 |
+
for f in range(self.__frame + 1, frame + 1):
|
| 133 |
+
self._seek(f)
|
| 134 |
+
|
| 135 |
+
def _seek(self, frame: int) -> None:
|
| 136 |
+
if frame == 0:
|
| 137 |
+
self.__frame = -1
|
| 138 |
+
self._fp.seek(self.__rewind)
|
| 139 |
+
self.__offset = 128
|
| 140 |
+
else:
|
| 141 |
+
# ensure that the previous frame was loaded
|
| 142 |
+
self.load()
|
| 143 |
+
|
| 144 |
+
if frame != self.__frame + 1:
|
| 145 |
+
msg = f"cannot seek to frame {frame}"
|
| 146 |
+
raise ValueError(msg)
|
| 147 |
+
self.__frame = frame
|
| 148 |
+
|
| 149 |
+
# move to next frame
|
| 150 |
+
self.fp = self._fp
|
| 151 |
+
self.fp.seek(self.__offset)
|
| 152 |
+
|
| 153 |
+
s = self.fp.read(4)
|
| 154 |
+
if not s:
|
| 155 |
+
msg = "missing frame size"
|
| 156 |
+
raise EOFError(msg)
|
| 157 |
+
|
| 158 |
+
framesize = i32(s)
|
| 159 |
+
|
| 160 |
+
self.decodermaxblock = framesize
|
| 161 |
+
self.tile = [("fli", (0, 0) + self.size, self.__offset, None)]
|
| 162 |
+
|
| 163 |
+
self.__offset += framesize
|
| 164 |
+
|
| 165 |
+
def tell(self) -> int:
|
| 166 |
+
return self.__frame
|
| 167 |
+
|
| 168 |
+
|
| 169 |
+
#
|
| 170 |
+
# registry
|
| 171 |
+
|
| 172 |
+
Image.register_open(FliImageFile.format, FliImageFile, _accept)
|
| 173 |
+
|
| 174 |
+
Image.register_extensions(FliImageFile.format, [".fli", ".flc"])
|
.venv/lib/python3.11/site-packages/PIL/FtexImagePlugin.py
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
A Pillow loader for .ftc and .ftu files (FTEX)
|
| 3 |
+
Jerome Leclanche <jerome@leclan.ch>
|
| 4 |
+
|
| 5 |
+
The contents of this file are hereby released in the public domain (CC0)
|
| 6 |
+
Full text of the CC0 license:
|
| 7 |
+
https://creativecommons.org/publicdomain/zero/1.0/
|
| 8 |
+
|
| 9 |
+
Independence War 2: Edge Of Chaos - Texture File Format - 16 October 2001
|
| 10 |
+
|
| 11 |
+
The textures used for 3D objects in Independence War 2: Edge Of Chaos are in a
|
| 12 |
+
packed custom format called FTEX. This file format uses file extensions FTC
|
| 13 |
+
and FTU.
|
| 14 |
+
* FTC files are compressed textures (using standard texture compression).
|
| 15 |
+
* FTU files are not compressed.
|
| 16 |
+
Texture File Format
|
| 17 |
+
The FTC and FTU texture files both use the same format. This
|
| 18 |
+
has the following structure:
|
| 19 |
+
{header}
|
| 20 |
+
{format_directory}
|
| 21 |
+
{data}
|
| 22 |
+
Where:
|
| 23 |
+
{header} = {
|
| 24 |
+
u32:magic,
|
| 25 |
+
u32:version,
|
| 26 |
+
u32:width,
|
| 27 |
+
u32:height,
|
| 28 |
+
u32:mipmap_count,
|
| 29 |
+
u32:format_count
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
* The "magic" number is "FTEX".
|
| 33 |
+
* "width" and "height" are the dimensions of the texture.
|
| 34 |
+
* "mipmap_count" is the number of mipmaps in the texture.
|
| 35 |
+
* "format_count" is the number of texture formats (different versions of the
|
| 36 |
+
same texture) in this file.
|
| 37 |
+
|
| 38 |
+
{format_directory} = format_count * { u32:format, u32:where }
|
| 39 |
+
|
| 40 |
+
The format value is 0 for DXT1 compressed textures and 1 for 24-bit RGB
|
| 41 |
+
uncompressed textures.
|
| 42 |
+
The texture data for a format starts at the position "where" in the file.
|
| 43 |
+
|
| 44 |
+
Each set of texture data in the file has the following structure:
|
| 45 |
+
{data} = format_count * { u32:mipmap_size, mipmap_size * { u8 } }
|
| 46 |
+
* "mipmap_size" is the number of bytes in that mip level. For compressed
|
| 47 |
+
textures this is the size of the texture data compressed with DXT1. For 24 bit
|
| 48 |
+
uncompressed textures, this is 3 * width * height. Following this are the image
|
| 49 |
+
bytes for that mipmap level.
|
| 50 |
+
|
| 51 |
+
Note: All data is stored in little-Endian (Intel) byte order.
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
from __future__ import annotations
|
| 55 |
+
|
| 56 |
+
import struct
|
| 57 |
+
from enum import IntEnum
|
| 58 |
+
from io import BytesIO
|
| 59 |
+
|
| 60 |
+
from . import Image, ImageFile
|
| 61 |
+
|
| 62 |
+
MAGIC = b"FTEX"
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class Format(IntEnum):
|
| 66 |
+
DXT1 = 0
|
| 67 |
+
UNCOMPRESSED = 1
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
class FtexImageFile(ImageFile.ImageFile):
|
| 71 |
+
format = "FTEX"
|
| 72 |
+
format_description = "Texture File Format (IW2:EOC)"
|
| 73 |
+
|
| 74 |
+
def _open(self) -> None:
|
| 75 |
+
if not _accept(self.fp.read(4)):
|
| 76 |
+
msg = "not an FTEX file"
|
| 77 |
+
raise SyntaxError(msg)
|
| 78 |
+
struct.unpack("<i", self.fp.read(4)) # version
|
| 79 |
+
self._size = struct.unpack("<2i", self.fp.read(8))
|
| 80 |
+
mipmap_count, format_count = struct.unpack("<2i", self.fp.read(8))
|
| 81 |
+
|
| 82 |
+
self._mode = "RGB"
|
| 83 |
+
|
| 84 |
+
# Only support single-format files.
|
| 85 |
+
# I don't know of any multi-format file.
|
| 86 |
+
assert format_count == 1
|
| 87 |
+
|
| 88 |
+
format, where = struct.unpack("<2i", self.fp.read(8))
|
| 89 |
+
self.fp.seek(where)
|
| 90 |
+
(mipmap_size,) = struct.unpack("<i", self.fp.read(4))
|
| 91 |
+
|
| 92 |
+
data = self.fp.read(mipmap_size)
|
| 93 |
+
|
| 94 |
+
if format == Format.DXT1:
|
| 95 |
+
self._mode = "RGBA"
|
| 96 |
+
self.tile = [("bcn", (0, 0) + self.size, 0, 1)]
|
| 97 |
+
elif format == Format.UNCOMPRESSED:
|
| 98 |
+
self.tile = [("raw", (0, 0) + self.size, 0, ("RGB", 0, 1))]
|
| 99 |
+
else:
|
| 100 |
+
msg = f"Invalid texture compression format: {repr(format)}"
|
| 101 |
+
raise ValueError(msg)
|
| 102 |
+
|
| 103 |
+
self.fp.close()
|
| 104 |
+
self.fp = BytesIO(data)
|
| 105 |
+
|
| 106 |
+
def load_seek(self, pos: int) -> None:
|
| 107 |
+
pass
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
def _accept(prefix: bytes) -> bool:
|
| 111 |
+
return prefix[:4] == MAGIC
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
Image.register_open(FtexImageFile.format, FtexImageFile, _accept)
|
| 115 |
+
Image.register_extensions(FtexImageFile.format, [".ftc", ".ftu"])
|
.venv/lib/python3.11/site-packages/PIL/GbrImagePlugin.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
#
|
| 4 |
+
# load a GIMP brush file
|
| 5 |
+
#
|
| 6 |
+
# History:
|
| 7 |
+
# 96-03-14 fl Created
|
| 8 |
+
# 16-01-08 es Version 2
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 11 |
+
# Copyright (c) Fredrik Lundh 1996.
|
| 12 |
+
# Copyright (c) Eric Soroos 2016.
|
| 13 |
+
#
|
| 14 |
+
# See the README file for information on usage and redistribution.
|
| 15 |
+
#
|
| 16 |
+
#
|
| 17 |
+
# See https://github.com/GNOME/gimp/blob/mainline/devel-docs/gbr.txt for
|
| 18 |
+
# format documentation.
|
| 19 |
+
#
|
| 20 |
+
# This code Interprets version 1 and 2 .gbr files.
|
| 21 |
+
# Version 1 files are obsolete, and should not be used for new
|
| 22 |
+
# brushes.
|
| 23 |
+
# Version 2 files are saved by GIMP v2.8 (at least)
|
| 24 |
+
# Version 3 files have a format specifier of 18 for 16bit floats in
|
| 25 |
+
# the color depth field. This is currently unsupported by Pillow.
|
| 26 |
+
from __future__ import annotations
|
| 27 |
+
|
| 28 |
+
from . import Image, ImageFile
|
| 29 |
+
from ._binary import i32be as i32
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _accept(prefix: bytes) -> bool:
|
| 33 |
+
return len(prefix) >= 8 and i32(prefix, 0) >= 20 and i32(prefix, 4) in (1, 2)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
##
|
| 37 |
+
# Image plugin for the GIMP brush format.
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class GbrImageFile(ImageFile.ImageFile):
|
| 41 |
+
format = "GBR"
|
| 42 |
+
format_description = "GIMP brush file"
|
| 43 |
+
|
| 44 |
+
def _open(self) -> None:
|
| 45 |
+
header_size = i32(self.fp.read(4))
|
| 46 |
+
if header_size < 20:
|
| 47 |
+
msg = "not a GIMP brush"
|
| 48 |
+
raise SyntaxError(msg)
|
| 49 |
+
version = i32(self.fp.read(4))
|
| 50 |
+
if version not in (1, 2):
|
| 51 |
+
msg = f"Unsupported GIMP brush version: {version}"
|
| 52 |
+
raise SyntaxError(msg)
|
| 53 |
+
|
| 54 |
+
width = i32(self.fp.read(4))
|
| 55 |
+
height = i32(self.fp.read(4))
|
| 56 |
+
color_depth = i32(self.fp.read(4))
|
| 57 |
+
if width <= 0 or height <= 0:
|
| 58 |
+
msg = "not a GIMP brush"
|
| 59 |
+
raise SyntaxError(msg)
|
| 60 |
+
if color_depth not in (1, 4):
|
| 61 |
+
msg = f"Unsupported GIMP brush color depth: {color_depth}"
|
| 62 |
+
raise SyntaxError(msg)
|
| 63 |
+
|
| 64 |
+
if version == 1:
|
| 65 |
+
comment_length = header_size - 20
|
| 66 |
+
else:
|
| 67 |
+
comment_length = header_size - 28
|
| 68 |
+
magic_number = self.fp.read(4)
|
| 69 |
+
if magic_number != b"GIMP":
|
| 70 |
+
msg = "not a GIMP brush, bad magic number"
|
| 71 |
+
raise SyntaxError(msg)
|
| 72 |
+
self.info["spacing"] = i32(self.fp.read(4))
|
| 73 |
+
|
| 74 |
+
comment = self.fp.read(comment_length)[:-1]
|
| 75 |
+
|
| 76 |
+
if color_depth == 1:
|
| 77 |
+
self._mode = "L"
|
| 78 |
+
else:
|
| 79 |
+
self._mode = "RGBA"
|
| 80 |
+
|
| 81 |
+
self._size = width, height
|
| 82 |
+
|
| 83 |
+
self.info["comment"] = comment
|
| 84 |
+
|
| 85 |
+
# Image might not be small
|
| 86 |
+
Image._decompression_bomb_check(self.size)
|
| 87 |
+
|
| 88 |
+
# Data is an uncompressed block of w * h * bytes/pixel
|
| 89 |
+
self._data_size = width * height * color_depth
|
| 90 |
+
|
| 91 |
+
def load(self):
|
| 92 |
+
if not self.im:
|
| 93 |
+
self.im = Image.core.new(self.mode, self.size)
|
| 94 |
+
self.frombytes(self.fp.read(self._data_size))
|
| 95 |
+
return Image.Image.load(self)
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
#
|
| 99 |
+
# registry
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
Image.register_open(GbrImageFile.format, GbrImageFile, _accept)
|
| 103 |
+
Image.register_extension(GbrImageFile.format, ".gbr")
|
.venv/lib/python3.11/site-packages/PIL/GdImageFile.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# GD file handling
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1996-04-12 fl Created
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) 1997 by Secret Labs AB.
|
| 11 |
+
# Copyright (c) 1996 by Fredrik Lundh.
|
| 12 |
+
#
|
| 13 |
+
# See the README file for information on usage and redistribution.
|
| 14 |
+
#
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
"""
|
| 18 |
+
.. note::
|
| 19 |
+
This format cannot be automatically recognized, so the
|
| 20 |
+
class is not registered for use with :py:func:`PIL.Image.open()`. To open a
|
| 21 |
+
gd file, use the :py:func:`PIL.GdImageFile.open()` function instead.
|
| 22 |
+
|
| 23 |
+
.. warning::
|
| 24 |
+
THE GD FORMAT IS NOT DESIGNED FOR DATA INTERCHANGE. This
|
| 25 |
+
implementation is provided for convenience and demonstrational
|
| 26 |
+
purposes only.
|
| 27 |
+
"""
|
| 28 |
+
from __future__ import annotations
|
| 29 |
+
|
| 30 |
+
from typing import IO
|
| 31 |
+
|
| 32 |
+
from . import ImageFile, ImagePalette, UnidentifiedImageError
|
| 33 |
+
from ._binary import i16be as i16
|
| 34 |
+
from ._binary import i32be as i32
|
| 35 |
+
from ._typing import StrOrBytesPath
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class GdImageFile(ImageFile.ImageFile):
|
| 39 |
+
"""
|
| 40 |
+
Image plugin for the GD uncompressed format. Note that this format
|
| 41 |
+
is not supported by the standard :py:func:`PIL.Image.open()` function. To use
|
| 42 |
+
this plugin, you have to import the :py:mod:`PIL.GdImageFile` module and
|
| 43 |
+
use the :py:func:`PIL.GdImageFile.open()` function.
|
| 44 |
+
"""
|
| 45 |
+
|
| 46 |
+
format = "GD"
|
| 47 |
+
format_description = "GD uncompressed images"
|
| 48 |
+
|
| 49 |
+
def _open(self) -> None:
|
| 50 |
+
# Header
|
| 51 |
+
assert self.fp is not None
|
| 52 |
+
|
| 53 |
+
s = self.fp.read(1037)
|
| 54 |
+
|
| 55 |
+
if i16(s) not in [65534, 65535]:
|
| 56 |
+
msg = "Not a valid GD 2.x .gd file"
|
| 57 |
+
raise SyntaxError(msg)
|
| 58 |
+
|
| 59 |
+
self._mode = "L" # FIXME: "P"
|
| 60 |
+
self._size = i16(s, 2), i16(s, 4)
|
| 61 |
+
|
| 62 |
+
true_color = s[6]
|
| 63 |
+
true_color_offset = 2 if true_color else 0
|
| 64 |
+
|
| 65 |
+
# transparency index
|
| 66 |
+
tindex = i32(s, 7 + true_color_offset)
|
| 67 |
+
if tindex < 256:
|
| 68 |
+
self.info["transparency"] = tindex
|
| 69 |
+
|
| 70 |
+
self.palette = ImagePalette.raw(
|
| 71 |
+
"XBGR", s[7 + true_color_offset + 4 : 7 + true_color_offset + 4 + 256 * 4]
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
self.tile = [
|
| 75 |
+
(
|
| 76 |
+
"raw",
|
| 77 |
+
(0, 0) + self.size,
|
| 78 |
+
7 + true_color_offset + 4 + 256 * 4,
|
| 79 |
+
("L", 0, 1),
|
| 80 |
+
)
|
| 81 |
+
]
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def open(fp: StrOrBytesPath | IO[bytes], mode: str = "r") -> GdImageFile:
|
| 85 |
+
"""
|
| 86 |
+
Load texture from a GD image file.
|
| 87 |
+
|
| 88 |
+
:param fp: GD file name, or an opened file handle.
|
| 89 |
+
:param mode: Optional mode. In this version, if the mode argument
|
| 90 |
+
is given, it must be "r".
|
| 91 |
+
:returns: An image instance.
|
| 92 |
+
:raises OSError: If the image could not be read.
|
| 93 |
+
"""
|
| 94 |
+
if mode != "r":
|
| 95 |
+
msg = "bad mode"
|
| 96 |
+
raise ValueError(msg)
|
| 97 |
+
|
| 98 |
+
try:
|
| 99 |
+
return GdImageFile(fp)
|
| 100 |
+
except SyntaxError as e:
|
| 101 |
+
msg = "cannot identify this image file"
|
| 102 |
+
raise UnidentifiedImageError(msg) from e
|
.venv/lib/python3.11/site-packages/PIL/GimpGradientFile.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# stuff to read (and render) GIMP gradient files
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 97-08-23 fl Created
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 11 |
+
# Copyright (c) Fredrik Lundh 1997.
|
| 12 |
+
#
|
| 13 |
+
# See the README file for information on usage and redistribution.
|
| 14 |
+
#
|
| 15 |
+
|
| 16 |
+
"""
|
| 17 |
+
Stuff to translate curve segments to palette values (derived from
|
| 18 |
+
the corresponding code in GIMP, written by Federico Mena Quintero.
|
| 19 |
+
See the GIMP distribution for more information.)
|
| 20 |
+
"""
|
| 21 |
+
from __future__ import annotations
|
| 22 |
+
|
| 23 |
+
from math import log, pi, sin, sqrt
|
| 24 |
+
from typing import IO, Callable
|
| 25 |
+
|
| 26 |
+
from ._binary import o8
|
| 27 |
+
|
| 28 |
+
EPSILON = 1e-10
|
| 29 |
+
"""""" # Enable auto-doc for data member
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def linear(middle: float, pos: float) -> float:
|
| 33 |
+
if pos <= middle:
|
| 34 |
+
if middle < EPSILON:
|
| 35 |
+
return 0.0
|
| 36 |
+
else:
|
| 37 |
+
return 0.5 * pos / middle
|
| 38 |
+
else:
|
| 39 |
+
pos = pos - middle
|
| 40 |
+
middle = 1.0 - middle
|
| 41 |
+
if middle < EPSILON:
|
| 42 |
+
return 1.0
|
| 43 |
+
else:
|
| 44 |
+
return 0.5 + 0.5 * pos / middle
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
def curved(middle: float, pos: float) -> float:
|
| 48 |
+
return pos ** (log(0.5) / log(max(middle, EPSILON)))
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def sine(middle: float, pos: float) -> float:
|
| 52 |
+
return (sin((-pi / 2.0) + pi * linear(middle, pos)) + 1.0) / 2.0
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
def sphere_increasing(middle: float, pos: float) -> float:
|
| 56 |
+
return sqrt(1.0 - (linear(middle, pos) - 1.0) ** 2)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
def sphere_decreasing(middle: float, pos: float) -> float:
|
| 60 |
+
return 1.0 - sqrt(1.0 - linear(middle, pos) ** 2)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
SEGMENTS = [linear, curved, sine, sphere_increasing, sphere_decreasing]
|
| 64 |
+
"""""" # Enable auto-doc for data member
|
| 65 |
+
|
| 66 |
+
|
| 67 |
+
class GradientFile:
|
| 68 |
+
gradient: (
|
| 69 |
+
list[
|
| 70 |
+
tuple[
|
| 71 |
+
float,
|
| 72 |
+
float,
|
| 73 |
+
float,
|
| 74 |
+
list[float],
|
| 75 |
+
list[float],
|
| 76 |
+
Callable[[float, float], float],
|
| 77 |
+
]
|
| 78 |
+
]
|
| 79 |
+
| None
|
| 80 |
+
) = None
|
| 81 |
+
|
| 82 |
+
def getpalette(self, entries: int = 256) -> tuple[bytes, str]:
|
| 83 |
+
assert self.gradient is not None
|
| 84 |
+
palette = []
|
| 85 |
+
|
| 86 |
+
ix = 0
|
| 87 |
+
x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
|
| 88 |
+
|
| 89 |
+
for i in range(entries):
|
| 90 |
+
x = i / (entries - 1)
|
| 91 |
+
|
| 92 |
+
while x1 < x:
|
| 93 |
+
ix += 1
|
| 94 |
+
x0, x1, xm, rgb0, rgb1, segment = self.gradient[ix]
|
| 95 |
+
|
| 96 |
+
w = x1 - x0
|
| 97 |
+
|
| 98 |
+
if w < EPSILON:
|
| 99 |
+
scale = segment(0.5, 0.5)
|
| 100 |
+
else:
|
| 101 |
+
scale = segment((xm - x0) / w, (x - x0) / w)
|
| 102 |
+
|
| 103 |
+
# expand to RGBA
|
| 104 |
+
r = o8(int(255 * ((rgb1[0] - rgb0[0]) * scale + rgb0[0]) + 0.5))
|
| 105 |
+
g = o8(int(255 * ((rgb1[1] - rgb0[1]) * scale + rgb0[1]) + 0.5))
|
| 106 |
+
b = o8(int(255 * ((rgb1[2] - rgb0[2]) * scale + rgb0[2]) + 0.5))
|
| 107 |
+
a = o8(int(255 * ((rgb1[3] - rgb0[3]) * scale + rgb0[3]) + 0.5))
|
| 108 |
+
|
| 109 |
+
# add to palette
|
| 110 |
+
palette.append(r + g + b + a)
|
| 111 |
+
|
| 112 |
+
return b"".join(palette), "RGBA"
|
| 113 |
+
|
| 114 |
+
|
| 115 |
+
class GimpGradientFile(GradientFile):
|
| 116 |
+
"""File handler for GIMP's gradient format."""
|
| 117 |
+
|
| 118 |
+
def __init__(self, fp: IO[bytes]) -> None:
|
| 119 |
+
if fp.readline()[:13] != b"GIMP Gradient":
|
| 120 |
+
msg = "not a GIMP gradient file"
|
| 121 |
+
raise SyntaxError(msg)
|
| 122 |
+
|
| 123 |
+
line = fp.readline()
|
| 124 |
+
|
| 125 |
+
# GIMP 1.2 gradient files don't contain a name, but GIMP 1.3 files do
|
| 126 |
+
if line.startswith(b"Name: "):
|
| 127 |
+
line = fp.readline().strip()
|
| 128 |
+
|
| 129 |
+
count = int(line)
|
| 130 |
+
|
| 131 |
+
self.gradient = []
|
| 132 |
+
|
| 133 |
+
for i in range(count):
|
| 134 |
+
s = fp.readline().split()
|
| 135 |
+
w = [float(x) for x in s[:11]]
|
| 136 |
+
|
| 137 |
+
x0, x1 = w[0], w[2]
|
| 138 |
+
xm = w[1]
|
| 139 |
+
rgb0 = w[3:7]
|
| 140 |
+
rgb1 = w[7:11]
|
| 141 |
+
|
| 142 |
+
segment = SEGMENTS[int(s[11])]
|
| 143 |
+
cspace = int(s[12])
|
| 144 |
+
|
| 145 |
+
if cspace != 0:
|
| 146 |
+
msg = "cannot handle HSV colour space"
|
| 147 |
+
raise OSError(msg)
|
| 148 |
+
|
| 149 |
+
self.gradient.append((x0, x1, xm, rgb0, rgb1, segment))
|
.venv/lib/python3.11/site-packages/PIL/GimpPaletteFile.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# stuff to read GIMP palette files
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1997-08-23 fl Created
|
| 9 |
+
# 2004-09-07 fl Support GIMP 2.0 palette files.
|
| 10 |
+
#
|
| 11 |
+
# Copyright (c) Secret Labs AB 1997-2004. All rights reserved.
|
| 12 |
+
# Copyright (c) Fredrik Lundh 1997-2004.
|
| 13 |
+
#
|
| 14 |
+
# See the README file for information on usage and redistribution.
|
| 15 |
+
#
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import re
|
| 19 |
+
from typing import IO
|
| 20 |
+
|
| 21 |
+
from ._binary import o8
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class GimpPaletteFile:
|
| 25 |
+
"""File handler for GIMP's palette format."""
|
| 26 |
+
|
| 27 |
+
rawmode = "RGB"
|
| 28 |
+
|
| 29 |
+
def __init__(self, fp: IO[bytes]) -> None:
|
| 30 |
+
palette = [o8(i) * 3 for i in range(256)]
|
| 31 |
+
|
| 32 |
+
if fp.readline()[:12] != b"GIMP Palette":
|
| 33 |
+
msg = "not a GIMP palette file"
|
| 34 |
+
raise SyntaxError(msg)
|
| 35 |
+
|
| 36 |
+
for i in range(256):
|
| 37 |
+
s = fp.readline()
|
| 38 |
+
if not s:
|
| 39 |
+
break
|
| 40 |
+
|
| 41 |
+
# skip fields and comment lines
|
| 42 |
+
if re.match(rb"\w+:|#", s):
|
| 43 |
+
continue
|
| 44 |
+
if len(s) > 100:
|
| 45 |
+
msg = "bad palette file"
|
| 46 |
+
raise SyntaxError(msg)
|
| 47 |
+
|
| 48 |
+
v = tuple(map(int, s.split()[:3]))
|
| 49 |
+
if len(v) != 3:
|
| 50 |
+
msg = "bad palette entry"
|
| 51 |
+
raise ValueError(msg)
|
| 52 |
+
|
| 53 |
+
palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])
|
| 54 |
+
|
| 55 |
+
self.palette = b"".join(palette)
|
| 56 |
+
|
| 57 |
+
def getpalette(self) -> tuple[bytes, str]:
|
| 58 |
+
return self.palette, self.rawmode
|
.venv/lib/python3.11/site-packages/PIL/GribStubImagePlugin.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# GRIB stub adapter
|
| 6 |
+
#
|
| 7 |
+
# Copyright (c) 1996-2003 by Fredrik Lundh
|
| 8 |
+
#
|
| 9 |
+
# See the README file for information on usage and redistribution.
|
| 10 |
+
#
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
from typing import IO
|
| 14 |
+
|
| 15 |
+
from . import Image, ImageFile
|
| 16 |
+
|
| 17 |
+
_handler = None
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def register_handler(handler: ImageFile.StubHandler | None) -> None:
|
| 21 |
+
"""
|
| 22 |
+
Install application-specific GRIB image handler.
|
| 23 |
+
|
| 24 |
+
:param handler: Handler object.
|
| 25 |
+
"""
|
| 26 |
+
global _handler
|
| 27 |
+
_handler = handler
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# --------------------------------------------------------------------
|
| 31 |
+
# Image adapter
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _accept(prefix: bytes) -> bool:
|
| 35 |
+
return prefix[:4] == b"GRIB" and prefix[7] == 1
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class GribStubImageFile(ImageFile.StubImageFile):
|
| 39 |
+
format = "GRIB"
|
| 40 |
+
format_description = "GRIB"
|
| 41 |
+
|
| 42 |
+
def _open(self) -> None:
|
| 43 |
+
offset = self.fp.tell()
|
| 44 |
+
|
| 45 |
+
if not _accept(self.fp.read(8)):
|
| 46 |
+
msg = "Not a GRIB file"
|
| 47 |
+
raise SyntaxError(msg)
|
| 48 |
+
|
| 49 |
+
self.fp.seek(offset)
|
| 50 |
+
|
| 51 |
+
# make something up
|
| 52 |
+
self._mode = "F"
|
| 53 |
+
self._size = 1, 1
|
| 54 |
+
|
| 55 |
+
loader = self._load()
|
| 56 |
+
if loader:
|
| 57 |
+
loader.open(self)
|
| 58 |
+
|
| 59 |
+
def _load(self) -> ImageFile.StubHandler | None:
|
| 60 |
+
return _handler
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 64 |
+
if _handler is None or not hasattr(_handler, "save"):
|
| 65 |
+
msg = "GRIB save handler not installed"
|
| 66 |
+
raise OSError(msg)
|
| 67 |
+
_handler.save(im, fp, filename)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# --------------------------------------------------------------------
|
| 71 |
+
# Registry
|
| 72 |
+
|
| 73 |
+
Image.register_open(GribStubImageFile.format, GribStubImageFile, _accept)
|
| 74 |
+
Image.register_save(GribStubImageFile.format, _save)
|
| 75 |
+
|
| 76 |
+
Image.register_extension(GribStubImageFile.format, ".grib")
|
.venv/lib/python3.11/site-packages/PIL/Hdf5StubImagePlugin.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# HDF5 stub adapter
|
| 6 |
+
#
|
| 7 |
+
# Copyright (c) 2000-2003 by Fredrik Lundh
|
| 8 |
+
#
|
| 9 |
+
# See the README file for information on usage and redistribution.
|
| 10 |
+
#
|
| 11 |
+
from __future__ import annotations
|
| 12 |
+
|
| 13 |
+
from typing import IO
|
| 14 |
+
|
| 15 |
+
from . import Image, ImageFile
|
| 16 |
+
|
| 17 |
+
_handler = None
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def register_handler(handler: ImageFile.StubHandler | None) -> None:
|
| 21 |
+
"""
|
| 22 |
+
Install application-specific HDF5 image handler.
|
| 23 |
+
|
| 24 |
+
:param handler: Handler object.
|
| 25 |
+
"""
|
| 26 |
+
global _handler
|
| 27 |
+
_handler = handler
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
# --------------------------------------------------------------------
|
| 31 |
+
# Image adapter
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def _accept(prefix: bytes) -> bool:
|
| 35 |
+
return prefix[:8] == b"\x89HDF\r\n\x1a\n"
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
class HDF5StubImageFile(ImageFile.StubImageFile):
|
| 39 |
+
format = "HDF5"
|
| 40 |
+
format_description = "HDF5"
|
| 41 |
+
|
| 42 |
+
def _open(self) -> None:
|
| 43 |
+
offset = self.fp.tell()
|
| 44 |
+
|
| 45 |
+
if not _accept(self.fp.read(8)):
|
| 46 |
+
msg = "Not an HDF file"
|
| 47 |
+
raise SyntaxError(msg)
|
| 48 |
+
|
| 49 |
+
self.fp.seek(offset)
|
| 50 |
+
|
| 51 |
+
# make something up
|
| 52 |
+
self._mode = "F"
|
| 53 |
+
self._size = 1, 1
|
| 54 |
+
|
| 55 |
+
loader = self._load()
|
| 56 |
+
if loader:
|
| 57 |
+
loader.open(self)
|
| 58 |
+
|
| 59 |
+
def _load(self) -> ImageFile.StubHandler | None:
|
| 60 |
+
return _handler
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 64 |
+
if _handler is None or not hasattr(_handler, "save"):
|
| 65 |
+
msg = "HDF5 save handler not installed"
|
| 66 |
+
raise OSError(msg)
|
| 67 |
+
_handler.save(im, fp, filename)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# --------------------------------------------------------------------
|
| 71 |
+
# Registry
|
| 72 |
+
|
| 73 |
+
Image.register_open(HDF5StubImageFile.format, HDF5StubImageFile, _accept)
|
| 74 |
+
Image.register_save(HDF5StubImageFile.format, _save)
|
| 75 |
+
|
| 76 |
+
Image.register_extensions(HDF5StubImageFile.format, [".h5", ".hdf"])
|
.venv/lib/python3.11/site-packages/PIL/IcoImagePlugin.py
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# Windows Icon support for PIL
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 96-05-27 fl Created
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 11 |
+
# Copyright (c) Fredrik Lundh 1996.
|
| 12 |
+
#
|
| 13 |
+
# See the README file for information on usage and redistribution.
|
| 14 |
+
#
|
| 15 |
+
|
| 16 |
+
# This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
|
| 17 |
+
# <casadebender@gmail.com>.
|
| 18 |
+
# https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki
|
| 19 |
+
#
|
| 20 |
+
# Icon format references:
|
| 21 |
+
# * https://en.wikipedia.org/wiki/ICO_(file_format)
|
| 22 |
+
# * https://msdn.microsoft.com/en-us/library/ms997538.aspx
|
| 23 |
+
from __future__ import annotations
|
| 24 |
+
|
| 25 |
+
import warnings
|
| 26 |
+
from io import BytesIO
|
| 27 |
+
from math import ceil, log
|
| 28 |
+
from typing import IO
|
| 29 |
+
|
| 30 |
+
from . import BmpImagePlugin, Image, ImageFile, PngImagePlugin
|
| 31 |
+
from ._binary import i16le as i16
|
| 32 |
+
from ._binary import i32le as i32
|
| 33 |
+
from ._binary import o8
|
| 34 |
+
from ._binary import o16le as o16
|
| 35 |
+
from ._binary import o32le as o32
|
| 36 |
+
|
| 37 |
+
#
|
| 38 |
+
# --------------------------------------------------------------------
|
| 39 |
+
|
| 40 |
+
_MAGIC = b"\0\0\1\0"
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 44 |
+
fp.write(_MAGIC) # (2+2)
|
| 45 |
+
bmp = im.encoderinfo.get("bitmap_format") == "bmp"
|
| 46 |
+
sizes = im.encoderinfo.get(
|
| 47 |
+
"sizes",
|
| 48 |
+
[(16, 16), (24, 24), (32, 32), (48, 48), (64, 64), (128, 128), (256, 256)],
|
| 49 |
+
)
|
| 50 |
+
frames = []
|
| 51 |
+
provided_ims = [im] + im.encoderinfo.get("append_images", [])
|
| 52 |
+
width, height = im.size
|
| 53 |
+
for size in sorted(set(sizes)):
|
| 54 |
+
if size[0] > width or size[1] > height or size[0] > 256 or size[1] > 256:
|
| 55 |
+
continue
|
| 56 |
+
|
| 57 |
+
for provided_im in provided_ims:
|
| 58 |
+
if provided_im.size != size:
|
| 59 |
+
continue
|
| 60 |
+
frames.append(provided_im)
|
| 61 |
+
if bmp:
|
| 62 |
+
bits = BmpImagePlugin.SAVE[provided_im.mode][1]
|
| 63 |
+
bits_used = [bits]
|
| 64 |
+
for other_im in provided_ims:
|
| 65 |
+
if other_im.size != size:
|
| 66 |
+
continue
|
| 67 |
+
bits = BmpImagePlugin.SAVE[other_im.mode][1]
|
| 68 |
+
if bits not in bits_used:
|
| 69 |
+
# Another image has been supplied for this size
|
| 70 |
+
# with a different bit depth
|
| 71 |
+
frames.append(other_im)
|
| 72 |
+
bits_used.append(bits)
|
| 73 |
+
break
|
| 74 |
+
else:
|
| 75 |
+
# TODO: invent a more convenient method for proportional scalings
|
| 76 |
+
frame = provided_im.copy()
|
| 77 |
+
frame.thumbnail(size, Image.Resampling.LANCZOS, reducing_gap=None)
|
| 78 |
+
frames.append(frame)
|
| 79 |
+
fp.write(o16(len(frames))) # idCount(2)
|
| 80 |
+
offset = fp.tell() + len(frames) * 16
|
| 81 |
+
for frame in frames:
|
| 82 |
+
width, height = frame.size
|
| 83 |
+
# 0 means 256
|
| 84 |
+
fp.write(o8(width if width < 256 else 0)) # bWidth(1)
|
| 85 |
+
fp.write(o8(height if height < 256 else 0)) # bHeight(1)
|
| 86 |
+
|
| 87 |
+
bits, colors = BmpImagePlugin.SAVE[frame.mode][1:] if bmp else (32, 0)
|
| 88 |
+
fp.write(o8(colors)) # bColorCount(1)
|
| 89 |
+
fp.write(b"\0") # bReserved(1)
|
| 90 |
+
fp.write(b"\0\0") # wPlanes(2)
|
| 91 |
+
fp.write(o16(bits)) # wBitCount(2)
|
| 92 |
+
|
| 93 |
+
image_io = BytesIO()
|
| 94 |
+
if bmp:
|
| 95 |
+
frame.save(image_io, "dib")
|
| 96 |
+
|
| 97 |
+
if bits != 32:
|
| 98 |
+
and_mask = Image.new("1", size)
|
| 99 |
+
ImageFile._save(
|
| 100 |
+
and_mask, image_io, [("raw", (0, 0) + size, 0, ("1", 0, -1))]
|
| 101 |
+
)
|
| 102 |
+
else:
|
| 103 |
+
frame.save(image_io, "png")
|
| 104 |
+
image_io.seek(0)
|
| 105 |
+
image_bytes = image_io.read()
|
| 106 |
+
if bmp:
|
| 107 |
+
image_bytes = image_bytes[:8] + o32(height * 2) + image_bytes[12:]
|
| 108 |
+
bytes_len = len(image_bytes)
|
| 109 |
+
fp.write(o32(bytes_len)) # dwBytesInRes(4)
|
| 110 |
+
fp.write(o32(offset)) # dwImageOffset(4)
|
| 111 |
+
current = fp.tell()
|
| 112 |
+
fp.seek(offset)
|
| 113 |
+
fp.write(image_bytes)
|
| 114 |
+
offset = offset + bytes_len
|
| 115 |
+
fp.seek(current)
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
def _accept(prefix: bytes) -> bool:
|
| 119 |
+
return prefix[:4] == _MAGIC
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
class IcoFile:
|
| 123 |
+
def __init__(self, buf):
|
| 124 |
+
"""
|
| 125 |
+
Parse image from file-like object containing ico file data
|
| 126 |
+
"""
|
| 127 |
+
|
| 128 |
+
# check magic
|
| 129 |
+
s = buf.read(6)
|
| 130 |
+
if not _accept(s):
|
| 131 |
+
msg = "not an ICO file"
|
| 132 |
+
raise SyntaxError(msg)
|
| 133 |
+
|
| 134 |
+
self.buf = buf
|
| 135 |
+
self.entry = []
|
| 136 |
+
|
| 137 |
+
# Number of items in file
|
| 138 |
+
self.nb_items = i16(s, 4)
|
| 139 |
+
|
| 140 |
+
# Get headers for each item
|
| 141 |
+
for i in range(self.nb_items):
|
| 142 |
+
s = buf.read(16)
|
| 143 |
+
|
| 144 |
+
icon_header = {
|
| 145 |
+
"width": s[0],
|
| 146 |
+
"height": s[1],
|
| 147 |
+
"nb_color": s[2], # No. of colors in image (0 if >=8bpp)
|
| 148 |
+
"reserved": s[3],
|
| 149 |
+
"planes": i16(s, 4),
|
| 150 |
+
"bpp": i16(s, 6),
|
| 151 |
+
"size": i32(s, 8),
|
| 152 |
+
"offset": i32(s, 12),
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
# See Wikipedia
|
| 156 |
+
for j in ("width", "height"):
|
| 157 |
+
if not icon_header[j]:
|
| 158 |
+
icon_header[j] = 256
|
| 159 |
+
|
| 160 |
+
# See Wikipedia notes about color depth.
|
| 161 |
+
# We need this just to differ images with equal sizes
|
| 162 |
+
icon_header["color_depth"] = (
|
| 163 |
+
icon_header["bpp"]
|
| 164 |
+
or (
|
| 165 |
+
icon_header["nb_color"] != 0
|
| 166 |
+
and ceil(log(icon_header["nb_color"], 2))
|
| 167 |
+
)
|
| 168 |
+
or 256
|
| 169 |
+
)
|
| 170 |
+
|
| 171 |
+
icon_header["dim"] = (icon_header["width"], icon_header["height"])
|
| 172 |
+
icon_header["square"] = icon_header["width"] * icon_header["height"]
|
| 173 |
+
|
| 174 |
+
self.entry.append(icon_header)
|
| 175 |
+
|
| 176 |
+
self.entry = sorted(self.entry, key=lambda x: x["color_depth"])
|
| 177 |
+
# ICO images are usually squares
|
| 178 |
+
self.entry = sorted(self.entry, key=lambda x: x["square"], reverse=True)
|
| 179 |
+
|
| 180 |
+
def sizes(self):
|
| 181 |
+
"""
|
| 182 |
+
Get a list of all available icon sizes and color depths.
|
| 183 |
+
"""
|
| 184 |
+
return {(h["width"], h["height"]) for h in self.entry}
|
| 185 |
+
|
| 186 |
+
def getentryindex(self, size, bpp=False):
|
| 187 |
+
for i, h in enumerate(self.entry):
|
| 188 |
+
if size == h["dim"] and (bpp is False or bpp == h["color_depth"]):
|
| 189 |
+
return i
|
| 190 |
+
return 0
|
| 191 |
+
|
| 192 |
+
def getimage(self, size, bpp=False):
|
| 193 |
+
"""
|
| 194 |
+
Get an image from the icon
|
| 195 |
+
"""
|
| 196 |
+
return self.frame(self.getentryindex(size, bpp))
|
| 197 |
+
|
| 198 |
+
def frame(self, idx: int) -> Image.Image:
|
| 199 |
+
"""
|
| 200 |
+
Get an image from frame idx
|
| 201 |
+
"""
|
| 202 |
+
|
| 203 |
+
header = self.entry[idx]
|
| 204 |
+
|
| 205 |
+
self.buf.seek(header["offset"])
|
| 206 |
+
data = self.buf.read(8)
|
| 207 |
+
self.buf.seek(header["offset"])
|
| 208 |
+
|
| 209 |
+
im: Image.Image
|
| 210 |
+
if data[:8] == PngImagePlugin._MAGIC:
|
| 211 |
+
# png frame
|
| 212 |
+
im = PngImagePlugin.PngImageFile(self.buf)
|
| 213 |
+
Image._decompression_bomb_check(im.size)
|
| 214 |
+
else:
|
| 215 |
+
# XOR + AND mask bmp frame
|
| 216 |
+
im = BmpImagePlugin.DibImageFile(self.buf)
|
| 217 |
+
Image._decompression_bomb_check(im.size)
|
| 218 |
+
|
| 219 |
+
# change tile dimension to only encompass XOR image
|
| 220 |
+
im._size = (im.size[0], int(im.size[1] / 2))
|
| 221 |
+
d, e, o, a = im.tile[0]
|
| 222 |
+
im.tile[0] = d, (0, 0) + im.size, o, a
|
| 223 |
+
|
| 224 |
+
# figure out where AND mask image starts
|
| 225 |
+
bpp = header["bpp"]
|
| 226 |
+
if 32 == bpp:
|
| 227 |
+
# 32-bit color depth icon image allows semitransparent areas
|
| 228 |
+
# PIL's DIB format ignores transparency bits, recover them.
|
| 229 |
+
# The DIB is packed in BGRX byte order where X is the alpha
|
| 230 |
+
# channel.
|
| 231 |
+
|
| 232 |
+
# Back up to start of bmp data
|
| 233 |
+
self.buf.seek(o)
|
| 234 |
+
# extract every 4th byte (eg. 3,7,11,15,...)
|
| 235 |
+
alpha_bytes = self.buf.read(im.size[0] * im.size[1] * 4)[3::4]
|
| 236 |
+
|
| 237 |
+
# convert to an 8bpp grayscale image
|
| 238 |
+
mask = Image.frombuffer(
|
| 239 |
+
"L", # 8bpp
|
| 240 |
+
im.size, # (w, h)
|
| 241 |
+
alpha_bytes, # source chars
|
| 242 |
+
"raw", # raw decoder
|
| 243 |
+
("L", 0, -1), # 8bpp inverted, unpadded, reversed
|
| 244 |
+
)
|
| 245 |
+
else:
|
| 246 |
+
# get AND image from end of bitmap
|
| 247 |
+
w = im.size[0]
|
| 248 |
+
if (w % 32) > 0:
|
| 249 |
+
# bitmap row data is aligned to word boundaries
|
| 250 |
+
w += 32 - (im.size[0] % 32)
|
| 251 |
+
|
| 252 |
+
# the total mask data is
|
| 253 |
+
# padded row size * height / bits per char
|
| 254 |
+
|
| 255 |
+
total_bytes = int((w * im.size[1]) / 8)
|
| 256 |
+
and_mask_offset = header["offset"] + header["size"] - total_bytes
|
| 257 |
+
|
| 258 |
+
self.buf.seek(and_mask_offset)
|
| 259 |
+
mask_data = self.buf.read(total_bytes)
|
| 260 |
+
|
| 261 |
+
# convert raw data to image
|
| 262 |
+
mask = Image.frombuffer(
|
| 263 |
+
"1", # 1 bpp
|
| 264 |
+
im.size, # (w, h)
|
| 265 |
+
mask_data, # source chars
|
| 266 |
+
"raw", # raw decoder
|
| 267 |
+
("1;I", int(w / 8), -1), # 1bpp inverted, padded, reversed
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
# now we have two images, im is XOR image and mask is AND image
|
| 271 |
+
|
| 272 |
+
# apply mask image as alpha channel
|
| 273 |
+
im = im.convert("RGBA")
|
| 274 |
+
im.putalpha(mask)
|
| 275 |
+
|
| 276 |
+
return im
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
##
|
| 280 |
+
# Image plugin for Windows Icon files.
|
| 281 |
+
|
| 282 |
+
|
| 283 |
+
class IcoImageFile(ImageFile.ImageFile):
|
| 284 |
+
"""
|
| 285 |
+
PIL read-only image support for Microsoft Windows .ico files.
|
| 286 |
+
|
| 287 |
+
By default the largest resolution image in the file will be loaded. This
|
| 288 |
+
can be changed by altering the 'size' attribute before calling 'load'.
|
| 289 |
+
|
| 290 |
+
The info dictionary has a key 'sizes' that is a list of the sizes available
|
| 291 |
+
in the icon file.
|
| 292 |
+
|
| 293 |
+
Handles classic, XP and Vista icon formats.
|
| 294 |
+
|
| 295 |
+
When saving, PNG compression is used. Support for this was only added in
|
| 296 |
+
Windows Vista. If you are unable to view the icon in Windows, convert the
|
| 297 |
+
image to "RGBA" mode before saving.
|
| 298 |
+
|
| 299 |
+
This plugin is a refactored version of Win32IconImagePlugin by Bryan Davis
|
| 300 |
+
<casadebender@gmail.com>.
|
| 301 |
+
https://code.google.com/archive/p/casadebender/wikis/Win32IconImagePlugin.wiki
|
| 302 |
+
"""
|
| 303 |
+
|
| 304 |
+
format = "ICO"
|
| 305 |
+
format_description = "Windows Icon"
|
| 306 |
+
|
| 307 |
+
def _open(self) -> None:
|
| 308 |
+
self.ico = IcoFile(self.fp)
|
| 309 |
+
self.info["sizes"] = self.ico.sizes()
|
| 310 |
+
self.size = self.ico.entry[0]["dim"]
|
| 311 |
+
self.load()
|
| 312 |
+
|
| 313 |
+
@property
|
| 314 |
+
def size(self):
|
| 315 |
+
return self._size
|
| 316 |
+
|
| 317 |
+
@size.setter
|
| 318 |
+
def size(self, value):
|
| 319 |
+
if value not in self.info["sizes"]:
|
| 320 |
+
msg = "This is not one of the allowed sizes of this image"
|
| 321 |
+
raise ValueError(msg)
|
| 322 |
+
self._size = value
|
| 323 |
+
|
| 324 |
+
def load(self):
|
| 325 |
+
if self.im is not None and self.im.size == self.size:
|
| 326 |
+
# Already loaded
|
| 327 |
+
return Image.Image.load(self)
|
| 328 |
+
im = self.ico.getimage(self.size)
|
| 329 |
+
# if tile is PNG, it won't really be loaded yet
|
| 330 |
+
im.load()
|
| 331 |
+
self.im = im.im
|
| 332 |
+
self.pyaccess = None
|
| 333 |
+
self._mode = im.mode
|
| 334 |
+
if im.palette:
|
| 335 |
+
self.palette = im.palette
|
| 336 |
+
if im.size != self.size:
|
| 337 |
+
warnings.warn("Image was not the expected size")
|
| 338 |
+
|
| 339 |
+
index = self.ico.getentryindex(self.size)
|
| 340 |
+
sizes = list(self.info["sizes"])
|
| 341 |
+
sizes[index] = im.size
|
| 342 |
+
self.info["sizes"] = set(sizes)
|
| 343 |
+
|
| 344 |
+
self.size = im.size
|
| 345 |
+
|
| 346 |
+
def load_seek(self, pos: int) -> None:
|
| 347 |
+
# Flag the ImageFile.Parser so that it
|
| 348 |
+
# just does all the decode at the end.
|
| 349 |
+
pass
|
| 350 |
+
|
| 351 |
+
|
| 352 |
+
#
|
| 353 |
+
# --------------------------------------------------------------------
|
| 354 |
+
|
| 355 |
+
|
| 356 |
+
Image.register_open(IcoImageFile.format, IcoImageFile, _accept)
|
| 357 |
+
Image.register_save(IcoImageFile.format, _save)
|
| 358 |
+
Image.register_extension(IcoImageFile.format, ".ico")
|
| 359 |
+
|
| 360 |
+
Image.register_mime(IcoImageFile.format, "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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# IFUNC IM file handling for PIL
|
| 6 |
+
#
|
| 7 |
+
# history:
|
| 8 |
+
# 1995-09-01 fl Created.
|
| 9 |
+
# 1997-01-03 fl Save palette images
|
| 10 |
+
# 1997-01-08 fl Added sequence support
|
| 11 |
+
# 1997-01-23 fl Added P and RGB save support
|
| 12 |
+
# 1997-05-31 fl Read floating point images
|
| 13 |
+
# 1997-06-22 fl Save floating point images
|
| 14 |
+
# 1997-08-27 fl Read and save 1-bit images
|
| 15 |
+
# 1998-06-25 fl Added support for RGB+LUT images
|
| 16 |
+
# 1998-07-02 fl Added support for YCC images
|
| 17 |
+
# 1998-07-15 fl Renamed offset attribute to avoid name clash
|
| 18 |
+
# 1998-12-29 fl Added I;16 support
|
| 19 |
+
# 2001-02-17 fl Use 're' instead of 'regex' (Python 2.1) (0.7)
|
| 20 |
+
# 2003-09-26 fl Added LA/PA support
|
| 21 |
+
#
|
| 22 |
+
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 23 |
+
# Copyright (c) 1995-2001 by Fredrik Lundh.
|
| 24 |
+
#
|
| 25 |
+
# See the README file for information on usage and redistribution.
|
| 26 |
+
#
|
| 27 |
+
from __future__ import annotations
|
| 28 |
+
|
| 29 |
+
import os
|
| 30 |
+
import re
|
| 31 |
+
from typing import IO, Any
|
| 32 |
+
|
| 33 |
+
from . import Image, ImageFile, ImagePalette
|
| 34 |
+
|
| 35 |
+
# --------------------------------------------------------------------
|
| 36 |
+
# Standard tags
|
| 37 |
+
|
| 38 |
+
COMMENT = "Comment"
|
| 39 |
+
DATE = "Date"
|
| 40 |
+
EQUIPMENT = "Digitalization equipment"
|
| 41 |
+
FRAMES = "File size (no of images)"
|
| 42 |
+
LUT = "Lut"
|
| 43 |
+
NAME = "Name"
|
| 44 |
+
SCALE = "Scale (x,y)"
|
| 45 |
+
SIZE = "Image size (x*y)"
|
| 46 |
+
MODE = "Image type"
|
| 47 |
+
|
| 48 |
+
TAGS = {
|
| 49 |
+
COMMENT: 0,
|
| 50 |
+
DATE: 0,
|
| 51 |
+
EQUIPMENT: 0,
|
| 52 |
+
FRAMES: 0,
|
| 53 |
+
LUT: 0,
|
| 54 |
+
NAME: 0,
|
| 55 |
+
SCALE: 0,
|
| 56 |
+
SIZE: 0,
|
| 57 |
+
MODE: 0,
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
OPEN = {
|
| 61 |
+
# ifunc93/p3cfunc formats
|
| 62 |
+
"0 1 image": ("1", "1"),
|
| 63 |
+
"L 1 image": ("1", "1"),
|
| 64 |
+
"Greyscale image": ("L", "L"),
|
| 65 |
+
"Grayscale image": ("L", "L"),
|
| 66 |
+
"RGB image": ("RGB", "RGB;L"),
|
| 67 |
+
"RLB image": ("RGB", "RLB"),
|
| 68 |
+
"RYB image": ("RGB", "RLB"),
|
| 69 |
+
"B1 image": ("1", "1"),
|
| 70 |
+
"B2 image": ("P", "P;2"),
|
| 71 |
+
"B4 image": ("P", "P;4"),
|
| 72 |
+
"X 24 image": ("RGB", "RGB"),
|
| 73 |
+
"L 32 S image": ("I", "I;32"),
|
| 74 |
+
"L 32 F image": ("F", "F;32"),
|
| 75 |
+
# old p3cfunc formats
|
| 76 |
+
"RGB3 image": ("RGB", "RGB;T"),
|
| 77 |
+
"RYB3 image": ("RGB", "RYB;T"),
|
| 78 |
+
# extensions
|
| 79 |
+
"LA image": ("LA", "LA;L"),
|
| 80 |
+
"PA image": ("LA", "PA;L"),
|
| 81 |
+
"RGBA image": ("RGBA", "RGBA;L"),
|
| 82 |
+
"RGBX image": ("RGB", "RGBX;L"),
|
| 83 |
+
"CMYK image": ("CMYK", "CMYK;L"),
|
| 84 |
+
"YCC image": ("YCbCr", "YCbCr;L"),
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
# ifunc95 extensions
|
| 88 |
+
for i in ["8", "8S", "16", "16S", "32", "32F"]:
|
| 89 |
+
OPEN[f"L {i} image"] = ("F", f"F;{i}")
|
| 90 |
+
OPEN[f"L*{i} image"] = ("F", f"F;{i}")
|
| 91 |
+
for i in ["16", "16L", "16B"]:
|
| 92 |
+
OPEN[f"L {i} image"] = (f"I;{i}", f"I;{i}")
|
| 93 |
+
OPEN[f"L*{i} image"] = (f"I;{i}", f"I;{i}")
|
| 94 |
+
for i in ["32S"]:
|
| 95 |
+
OPEN[f"L {i} image"] = ("I", f"I;{i}")
|
| 96 |
+
OPEN[f"L*{i} image"] = ("I", f"I;{i}")
|
| 97 |
+
for j in range(2, 33):
|
| 98 |
+
OPEN[f"L*{j} image"] = ("F", f"F;{j}")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
+
# --------------------------------------------------------------------
|
| 102 |
+
# Read IM directory
|
| 103 |
+
|
| 104 |
+
split = re.compile(rb"^([A-Za-z][^:]*):[ \t]*(.*)[ \t]*$")
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def number(s: Any) -> float:
|
| 108 |
+
try:
|
| 109 |
+
return int(s)
|
| 110 |
+
except ValueError:
|
| 111 |
+
return float(s)
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
##
|
| 115 |
+
# Image plugin for the IFUNC IM file format.
|
| 116 |
+
|
| 117 |
+
|
| 118 |
+
class ImImageFile(ImageFile.ImageFile):
|
| 119 |
+
format = "IM"
|
| 120 |
+
format_description = "IFUNC Image Memory"
|
| 121 |
+
_close_exclusive_fp_after_loading = False
|
| 122 |
+
|
| 123 |
+
def _open(self) -> None:
|
| 124 |
+
# Quick rejection: if there's not an LF among the first
|
| 125 |
+
# 100 bytes, this is (probably) not a text header.
|
| 126 |
+
|
| 127 |
+
if b"\n" not in self.fp.read(100):
|
| 128 |
+
msg = "not an IM file"
|
| 129 |
+
raise SyntaxError(msg)
|
| 130 |
+
self.fp.seek(0)
|
| 131 |
+
|
| 132 |
+
n = 0
|
| 133 |
+
|
| 134 |
+
# Default values
|
| 135 |
+
self.info[MODE] = "L"
|
| 136 |
+
self.info[SIZE] = (512, 512)
|
| 137 |
+
self.info[FRAMES] = 1
|
| 138 |
+
|
| 139 |
+
self.rawmode = "L"
|
| 140 |
+
|
| 141 |
+
while True:
|
| 142 |
+
s = self.fp.read(1)
|
| 143 |
+
|
| 144 |
+
# Some versions of IFUNC uses \n\r instead of \r\n...
|
| 145 |
+
if s == b"\r":
|
| 146 |
+
continue
|
| 147 |
+
|
| 148 |
+
if not s or s == b"\0" or s == b"\x1A":
|
| 149 |
+
break
|
| 150 |
+
|
| 151 |
+
# FIXME: this may read whole file if not a text file
|
| 152 |
+
s = s + self.fp.readline()
|
| 153 |
+
|
| 154 |
+
if len(s) > 100:
|
| 155 |
+
msg = "not an IM file"
|
| 156 |
+
raise SyntaxError(msg)
|
| 157 |
+
|
| 158 |
+
if s[-2:] == b"\r\n":
|
| 159 |
+
s = s[:-2]
|
| 160 |
+
elif s[-1:] == b"\n":
|
| 161 |
+
s = s[:-1]
|
| 162 |
+
|
| 163 |
+
try:
|
| 164 |
+
m = split.match(s)
|
| 165 |
+
except re.error as e:
|
| 166 |
+
msg = "not an IM file"
|
| 167 |
+
raise SyntaxError(msg) from e
|
| 168 |
+
|
| 169 |
+
if m:
|
| 170 |
+
k, v = m.group(1, 2)
|
| 171 |
+
|
| 172 |
+
# Don't know if this is the correct encoding,
|
| 173 |
+
# but a decent guess (I guess)
|
| 174 |
+
k = k.decode("latin-1", "replace")
|
| 175 |
+
v = v.decode("latin-1", "replace")
|
| 176 |
+
|
| 177 |
+
# Convert value as appropriate
|
| 178 |
+
if k in [FRAMES, SCALE, SIZE]:
|
| 179 |
+
v = v.replace("*", ",")
|
| 180 |
+
v = tuple(map(number, v.split(",")))
|
| 181 |
+
if len(v) == 1:
|
| 182 |
+
v = v[0]
|
| 183 |
+
elif k == MODE and v in OPEN:
|
| 184 |
+
v, self.rawmode = OPEN[v]
|
| 185 |
+
|
| 186 |
+
# Add to dictionary. Note that COMMENT tags are
|
| 187 |
+
# combined into a list of strings.
|
| 188 |
+
if k == COMMENT:
|
| 189 |
+
if k in self.info:
|
| 190 |
+
self.info[k].append(v)
|
| 191 |
+
else:
|
| 192 |
+
self.info[k] = [v]
|
| 193 |
+
else:
|
| 194 |
+
self.info[k] = v
|
| 195 |
+
|
| 196 |
+
if k in TAGS:
|
| 197 |
+
n += 1
|
| 198 |
+
|
| 199 |
+
else:
|
| 200 |
+
msg = f"Syntax error in IM header: {s.decode('ascii', 'replace')}"
|
| 201 |
+
raise SyntaxError(msg)
|
| 202 |
+
|
| 203 |
+
if not n:
|
| 204 |
+
msg = "Not an IM file"
|
| 205 |
+
raise SyntaxError(msg)
|
| 206 |
+
|
| 207 |
+
# Basic attributes
|
| 208 |
+
self._size = self.info[SIZE]
|
| 209 |
+
self._mode = self.info[MODE]
|
| 210 |
+
|
| 211 |
+
# Skip forward to start of image data
|
| 212 |
+
while s and s[:1] != b"\x1A":
|
| 213 |
+
s = self.fp.read(1)
|
| 214 |
+
if not s:
|
| 215 |
+
msg = "File truncated"
|
| 216 |
+
raise SyntaxError(msg)
|
| 217 |
+
|
| 218 |
+
if LUT in self.info:
|
| 219 |
+
# convert lookup table to palette or lut attribute
|
| 220 |
+
palette = self.fp.read(768)
|
| 221 |
+
greyscale = 1 # greyscale palette
|
| 222 |
+
linear = 1 # linear greyscale palette
|
| 223 |
+
for i in range(256):
|
| 224 |
+
if palette[i] == palette[i + 256] == palette[i + 512]:
|
| 225 |
+
if palette[i] != i:
|
| 226 |
+
linear = 0
|
| 227 |
+
else:
|
| 228 |
+
greyscale = 0
|
| 229 |
+
if self.mode in ["L", "LA", "P", "PA"]:
|
| 230 |
+
if greyscale:
|
| 231 |
+
if not linear:
|
| 232 |
+
self.lut = list(palette[:256])
|
| 233 |
+
else:
|
| 234 |
+
if self.mode in ["L", "P"]:
|
| 235 |
+
self._mode = self.rawmode = "P"
|
| 236 |
+
elif self.mode in ["LA", "PA"]:
|
| 237 |
+
self._mode = "PA"
|
| 238 |
+
self.rawmode = "PA;L"
|
| 239 |
+
self.palette = ImagePalette.raw("RGB;L", palette)
|
| 240 |
+
elif self.mode == "RGB":
|
| 241 |
+
if not greyscale or not linear:
|
| 242 |
+
self.lut = list(palette)
|
| 243 |
+
|
| 244 |
+
self.frame = 0
|
| 245 |
+
|
| 246 |
+
self.__offset = offs = self.fp.tell()
|
| 247 |
+
|
| 248 |
+
self._fp = self.fp # FIXME: hack
|
| 249 |
+
|
| 250 |
+
if self.rawmode[:2] == "F;":
|
| 251 |
+
# ifunc95 formats
|
| 252 |
+
try:
|
| 253 |
+
# use bit decoder (if necessary)
|
| 254 |
+
bits = int(self.rawmode[2:])
|
| 255 |
+
if bits not in [8, 16, 32]:
|
| 256 |
+
self.tile = [("bit", (0, 0) + self.size, offs, (bits, 8, 3, 0, -1))]
|
| 257 |
+
return
|
| 258 |
+
except ValueError:
|
| 259 |
+
pass
|
| 260 |
+
|
| 261 |
+
if self.rawmode in ["RGB;T", "RYB;T"]:
|
| 262 |
+
# Old LabEye/3PC files. Would be very surprised if anyone
|
| 263 |
+
# ever stumbled upon such a file ;-)
|
| 264 |
+
size = self.size[0] * self.size[1]
|
| 265 |
+
self.tile = [
|
| 266 |
+
("raw", (0, 0) + self.size, offs, ("G", 0, -1)),
|
| 267 |
+
("raw", (0, 0) + self.size, offs + size, ("R", 0, -1)),
|
| 268 |
+
("raw", (0, 0) + self.size, offs + 2 * size, ("B", 0, -1)),
|
| 269 |
+
]
|
| 270 |
+
else:
|
| 271 |
+
# LabEye/IFUNC files
|
| 272 |
+
self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))]
|
| 273 |
+
|
| 274 |
+
@property
|
| 275 |
+
def n_frames(self) -> int:
|
| 276 |
+
return self.info[FRAMES]
|
| 277 |
+
|
| 278 |
+
@property
|
| 279 |
+
def is_animated(self) -> bool:
|
| 280 |
+
return self.info[FRAMES] > 1
|
| 281 |
+
|
| 282 |
+
def seek(self, frame: int) -> None:
|
| 283 |
+
if not self._seek_check(frame):
|
| 284 |
+
return
|
| 285 |
+
|
| 286 |
+
self.frame = frame
|
| 287 |
+
|
| 288 |
+
if self.mode == "1":
|
| 289 |
+
bits = 1
|
| 290 |
+
else:
|
| 291 |
+
bits = 8 * len(self.mode)
|
| 292 |
+
|
| 293 |
+
size = ((self.size[0] * bits + 7) // 8) * self.size[1]
|
| 294 |
+
offs = self.__offset + frame * size
|
| 295 |
+
|
| 296 |
+
self.fp = self._fp
|
| 297 |
+
|
| 298 |
+
self.tile = [("raw", (0, 0) + self.size, offs, (self.rawmode, 0, -1))]
|
| 299 |
+
|
| 300 |
+
def tell(self) -> int:
|
| 301 |
+
return self.frame
|
| 302 |
+
|
| 303 |
+
|
| 304 |
+
#
|
| 305 |
+
# --------------------------------------------------------------------
|
| 306 |
+
# Save IM files
|
| 307 |
+
|
| 308 |
+
|
| 309 |
+
SAVE = {
|
| 310 |
+
# mode: (im type, raw mode)
|
| 311 |
+
"1": ("0 1", "1"),
|
| 312 |
+
"L": ("Greyscale", "L"),
|
| 313 |
+
"LA": ("LA", "LA;L"),
|
| 314 |
+
"P": ("Greyscale", "P"),
|
| 315 |
+
"PA": ("LA", "PA;L"),
|
| 316 |
+
"I": ("L 32S", "I;32S"),
|
| 317 |
+
"I;16": ("L 16", "I;16"),
|
| 318 |
+
"I;16L": ("L 16L", "I;16L"),
|
| 319 |
+
"I;16B": ("L 16B", "I;16B"),
|
| 320 |
+
"F": ("L 32F", "F;32F"),
|
| 321 |
+
"RGB": ("RGB", "RGB;L"),
|
| 322 |
+
"RGBA": ("RGBA", "RGBA;L"),
|
| 323 |
+
"RGBX": ("RGBX", "RGBX;L"),
|
| 324 |
+
"CMYK": ("CMYK", "CMYK;L"),
|
| 325 |
+
"YCbCr": ("YCC", "YCbCr;L"),
|
| 326 |
+
}
|
| 327 |
+
|
| 328 |
+
|
| 329 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 330 |
+
try:
|
| 331 |
+
image_type, rawmode = SAVE[im.mode]
|
| 332 |
+
except KeyError as e:
|
| 333 |
+
msg = f"Cannot save {im.mode} images as IM"
|
| 334 |
+
raise ValueError(msg) from e
|
| 335 |
+
|
| 336 |
+
frames = im.encoderinfo.get("frames", 1)
|
| 337 |
+
|
| 338 |
+
fp.write(f"Image type: {image_type} image\r\n".encode("ascii"))
|
| 339 |
+
if filename:
|
| 340 |
+
# Each line must be 100 characters or less,
|
| 341 |
+
# or: SyntaxError("not an IM file")
|
| 342 |
+
# 8 characters are used for "Name: " and "\r\n"
|
| 343 |
+
# Keep just the filename, ditch the potentially overlong path
|
| 344 |
+
if isinstance(filename, bytes):
|
| 345 |
+
filename = filename.decode("ascii")
|
| 346 |
+
name, ext = os.path.splitext(os.path.basename(filename))
|
| 347 |
+
name = "".join([name[: 92 - len(ext)], ext])
|
| 348 |
+
|
| 349 |
+
fp.write(f"Name: {name}\r\n".encode("ascii"))
|
| 350 |
+
fp.write(("Image size (x*y): %d*%d\r\n" % im.size).encode("ascii"))
|
| 351 |
+
fp.write(f"File size (no of images): {frames}\r\n".encode("ascii"))
|
| 352 |
+
if im.mode in ["P", "PA"]:
|
| 353 |
+
fp.write(b"Lut: 1\r\n")
|
| 354 |
+
fp.write(b"\000" * (511 - fp.tell()) + b"\032")
|
| 355 |
+
if im.mode in ["P", "PA"]:
|
| 356 |
+
im_palette = im.im.getpalette("RGB", "RGB;L")
|
| 357 |
+
colors = len(im_palette) // 3
|
| 358 |
+
palette = b""
|
| 359 |
+
for i in range(3):
|
| 360 |
+
palette += im_palette[colors * i : colors * (i + 1)]
|
| 361 |
+
palette += b"\x00" * (256 - colors)
|
| 362 |
+
fp.write(palette) # 768 bytes
|
| 363 |
+
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, -1))])
|
| 364 |
+
|
| 365 |
+
|
| 366 |
+
#
|
| 367 |
+
# --------------------------------------------------------------------
|
| 368 |
+
# Registry
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
Image.register_open(ImImageFile.format, ImImageFile)
|
| 372 |
+
Image.register_save(ImImageFile.format, _save)
|
| 373 |
+
|
| 374 |
+
Image.register_extension(ImImageFile.format, ".im")
|
.venv/lib/python3.11/site-packages/PIL/ImageChops.py
ADDED
|
@@ -0,0 +1,311 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# standard channel operations
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1996-03-24 fl Created
|
| 9 |
+
# 1996-08-13 fl Added logical operations (for "1" images)
|
| 10 |
+
# 2000-10-12 fl Added offset method (from Image.py)
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) 1997-2000 by Secret Labs AB
|
| 13 |
+
# Copyright (c) 1996-2000 by Fredrik Lundh
|
| 14 |
+
#
|
| 15 |
+
# See the README file for information on usage and redistribution.
|
| 16 |
+
#
|
| 17 |
+
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
from . import Image
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def constant(image: Image.Image, value: int) -> Image.Image:
|
| 24 |
+
"""Fill a channel with a given gray level.
|
| 25 |
+
|
| 26 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 27 |
+
"""
|
| 28 |
+
|
| 29 |
+
return Image.new("L", image.size, value)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def duplicate(image: Image.Image) -> Image.Image:
|
| 33 |
+
"""Copy a channel. Alias for :py:meth:`PIL.Image.Image.copy`.
|
| 34 |
+
|
| 35 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 36 |
+
"""
|
| 37 |
+
|
| 38 |
+
return image.copy()
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
def invert(image: Image.Image) -> Image.Image:
|
| 42 |
+
"""
|
| 43 |
+
Invert an image (channel). ::
|
| 44 |
+
|
| 45 |
+
out = MAX - image
|
| 46 |
+
|
| 47 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 48 |
+
"""
|
| 49 |
+
|
| 50 |
+
image.load()
|
| 51 |
+
return image._new(image.im.chop_invert())
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def lighter(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 55 |
+
"""
|
| 56 |
+
Compares the two images, pixel by pixel, and returns a new image containing
|
| 57 |
+
the lighter values. ::
|
| 58 |
+
|
| 59 |
+
out = max(image1, image2)
|
| 60 |
+
|
| 61 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
image1.load()
|
| 65 |
+
image2.load()
|
| 66 |
+
return image1._new(image1.im.chop_lighter(image2.im))
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def darker(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 70 |
+
"""
|
| 71 |
+
Compares the two images, pixel by pixel, and returns a new image containing
|
| 72 |
+
the darker values. ::
|
| 73 |
+
|
| 74 |
+
out = min(image1, image2)
|
| 75 |
+
|
| 76 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 77 |
+
"""
|
| 78 |
+
|
| 79 |
+
image1.load()
|
| 80 |
+
image2.load()
|
| 81 |
+
return image1._new(image1.im.chop_darker(image2.im))
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
def difference(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 85 |
+
"""
|
| 86 |
+
Returns the absolute value of the pixel-by-pixel difference between the two
|
| 87 |
+
images. ::
|
| 88 |
+
|
| 89 |
+
out = abs(image1 - image2)
|
| 90 |
+
|
| 91 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 92 |
+
"""
|
| 93 |
+
|
| 94 |
+
image1.load()
|
| 95 |
+
image2.load()
|
| 96 |
+
return image1._new(image1.im.chop_difference(image2.im))
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
def multiply(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 100 |
+
"""
|
| 101 |
+
Superimposes two images on top of each other.
|
| 102 |
+
|
| 103 |
+
If you multiply an image with a solid black image, the result is black. If
|
| 104 |
+
you multiply with a solid white image, the image is unaffected. ::
|
| 105 |
+
|
| 106 |
+
out = image1 * image2 / MAX
|
| 107 |
+
|
| 108 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 109 |
+
"""
|
| 110 |
+
|
| 111 |
+
image1.load()
|
| 112 |
+
image2.load()
|
| 113 |
+
return image1._new(image1.im.chop_multiply(image2.im))
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def screen(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 117 |
+
"""
|
| 118 |
+
Superimposes two inverted images on top of each other. ::
|
| 119 |
+
|
| 120 |
+
out = MAX - ((MAX - image1) * (MAX - image2) / MAX)
|
| 121 |
+
|
| 122 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
image1.load()
|
| 126 |
+
image2.load()
|
| 127 |
+
return image1._new(image1.im.chop_screen(image2.im))
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def soft_light(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 131 |
+
"""
|
| 132 |
+
Superimposes two images on top of each other using the Soft Light algorithm
|
| 133 |
+
|
| 134 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 135 |
+
"""
|
| 136 |
+
|
| 137 |
+
image1.load()
|
| 138 |
+
image2.load()
|
| 139 |
+
return image1._new(image1.im.chop_soft_light(image2.im))
|
| 140 |
+
|
| 141 |
+
|
| 142 |
+
def hard_light(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 143 |
+
"""
|
| 144 |
+
Superimposes two images on top of each other using the Hard Light algorithm
|
| 145 |
+
|
| 146 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 147 |
+
"""
|
| 148 |
+
|
| 149 |
+
image1.load()
|
| 150 |
+
image2.load()
|
| 151 |
+
return image1._new(image1.im.chop_hard_light(image2.im))
|
| 152 |
+
|
| 153 |
+
|
| 154 |
+
def overlay(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 155 |
+
"""
|
| 156 |
+
Superimposes two images on top of each other using the Overlay algorithm
|
| 157 |
+
|
| 158 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 159 |
+
"""
|
| 160 |
+
|
| 161 |
+
image1.load()
|
| 162 |
+
image2.load()
|
| 163 |
+
return image1._new(image1.im.chop_overlay(image2.im))
|
| 164 |
+
|
| 165 |
+
|
| 166 |
+
def add(
|
| 167 |
+
image1: Image.Image, image2: Image.Image, scale: float = 1.0, offset: float = 0
|
| 168 |
+
) -> Image.Image:
|
| 169 |
+
"""
|
| 170 |
+
Adds two images, dividing the result by scale and adding the
|
| 171 |
+
offset. If omitted, scale defaults to 1.0, and offset to 0.0. ::
|
| 172 |
+
|
| 173 |
+
out = ((image1 + image2) / scale + offset)
|
| 174 |
+
|
| 175 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
image1.load()
|
| 179 |
+
image2.load()
|
| 180 |
+
return image1._new(image1.im.chop_add(image2.im, scale, offset))
|
| 181 |
+
|
| 182 |
+
|
| 183 |
+
def subtract(
|
| 184 |
+
image1: Image.Image, image2: Image.Image, scale: float = 1.0, offset: float = 0
|
| 185 |
+
) -> Image.Image:
|
| 186 |
+
"""
|
| 187 |
+
Subtracts two images, dividing the result by scale and adding the offset.
|
| 188 |
+
If omitted, scale defaults to 1.0, and offset to 0.0. ::
|
| 189 |
+
|
| 190 |
+
out = ((image1 - image2) / scale + offset)
|
| 191 |
+
|
| 192 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 193 |
+
"""
|
| 194 |
+
|
| 195 |
+
image1.load()
|
| 196 |
+
image2.load()
|
| 197 |
+
return image1._new(image1.im.chop_subtract(image2.im, scale, offset))
|
| 198 |
+
|
| 199 |
+
|
| 200 |
+
def add_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 201 |
+
"""Add two images, without clipping the result. ::
|
| 202 |
+
|
| 203 |
+
out = ((image1 + image2) % MAX)
|
| 204 |
+
|
| 205 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 206 |
+
"""
|
| 207 |
+
|
| 208 |
+
image1.load()
|
| 209 |
+
image2.load()
|
| 210 |
+
return image1._new(image1.im.chop_add_modulo(image2.im))
|
| 211 |
+
|
| 212 |
+
|
| 213 |
+
def subtract_modulo(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 214 |
+
"""Subtract two images, without clipping the result. ::
|
| 215 |
+
|
| 216 |
+
out = ((image1 - image2) % MAX)
|
| 217 |
+
|
| 218 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 219 |
+
"""
|
| 220 |
+
|
| 221 |
+
image1.load()
|
| 222 |
+
image2.load()
|
| 223 |
+
return image1._new(image1.im.chop_subtract_modulo(image2.im))
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def logical_and(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 227 |
+
"""Logical AND between two images.
|
| 228 |
+
|
| 229 |
+
Both of the images must have mode "1". If you would like to perform a
|
| 230 |
+
logical AND on an image with a mode other than "1", try
|
| 231 |
+
:py:meth:`~PIL.ImageChops.multiply` instead, using a black-and-white mask
|
| 232 |
+
as the second image. ::
|
| 233 |
+
|
| 234 |
+
out = ((image1 and image2) % MAX)
|
| 235 |
+
|
| 236 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 237 |
+
"""
|
| 238 |
+
|
| 239 |
+
image1.load()
|
| 240 |
+
image2.load()
|
| 241 |
+
return image1._new(image1.im.chop_and(image2.im))
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
def logical_or(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 245 |
+
"""Logical OR between two images.
|
| 246 |
+
|
| 247 |
+
Both of the images must have mode "1". ::
|
| 248 |
+
|
| 249 |
+
out = ((image1 or image2) % MAX)
|
| 250 |
+
|
| 251 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 252 |
+
"""
|
| 253 |
+
|
| 254 |
+
image1.load()
|
| 255 |
+
image2.load()
|
| 256 |
+
return image1._new(image1.im.chop_or(image2.im))
|
| 257 |
+
|
| 258 |
+
|
| 259 |
+
def logical_xor(image1: Image.Image, image2: Image.Image) -> Image.Image:
|
| 260 |
+
"""Logical XOR between two images.
|
| 261 |
+
|
| 262 |
+
Both of the images must have mode "1". ::
|
| 263 |
+
|
| 264 |
+
out = ((bool(image1) != bool(image2)) % MAX)
|
| 265 |
+
|
| 266 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 267 |
+
"""
|
| 268 |
+
|
| 269 |
+
image1.load()
|
| 270 |
+
image2.load()
|
| 271 |
+
return image1._new(image1.im.chop_xor(image2.im))
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def blend(image1: Image.Image, image2: Image.Image, alpha: float) -> Image.Image:
|
| 275 |
+
"""Blend images using constant transparency weight. Alias for
|
| 276 |
+
:py:func:`PIL.Image.blend`.
|
| 277 |
+
|
| 278 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 279 |
+
"""
|
| 280 |
+
|
| 281 |
+
return Image.blend(image1, image2, alpha)
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
def composite(
|
| 285 |
+
image1: Image.Image, image2: Image.Image, mask: Image.Image
|
| 286 |
+
) -> Image.Image:
|
| 287 |
+
"""Create composite using transparency mask. Alias for
|
| 288 |
+
:py:func:`PIL.Image.composite`.
|
| 289 |
+
|
| 290 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 291 |
+
"""
|
| 292 |
+
|
| 293 |
+
return Image.composite(image1, image2, mask)
|
| 294 |
+
|
| 295 |
+
|
| 296 |
+
def offset(image: Image.Image, xoffset: int, yoffset: int | None = None) -> Image.Image:
|
| 297 |
+
"""Returns a copy of the image where data has been offset by the given
|
| 298 |
+
distances. Data wraps around the edges. If ``yoffset`` is omitted, it
|
| 299 |
+
is assumed to be equal to ``xoffset``.
|
| 300 |
+
|
| 301 |
+
:param image: Input image.
|
| 302 |
+
:param xoffset: The horizontal distance.
|
| 303 |
+
:param yoffset: The vertical distance. If omitted, both
|
| 304 |
+
distances are set to the same value.
|
| 305 |
+
:rtype: :py:class:`~PIL.Image.Image`
|
| 306 |
+
"""
|
| 307 |
+
|
| 308 |
+
if yoffset is None:
|
| 309 |
+
yoffset = xoffset
|
| 310 |
+
image.load()
|
| 311 |
+
return image._new(image.im.offset(xoffset, yoffset))
|
.venv/lib/python3.11/site-packages/PIL/ImageDraw.py
ADDED
|
@@ -0,0 +1,1206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
|
| 34 |
+
import math
|
| 35 |
+
import numbers
|
| 36 |
+
import struct
|
| 37 |
+
from types import ModuleType
|
| 38 |
+
from typing import TYPE_CHECKING, AnyStr, Callable, List, Sequence, Tuple, Union, cast
|
| 39 |
+
|
| 40 |
+
from . import Image, ImageColor
|
| 41 |
+
from ._deprecate import deprecate
|
| 42 |
+
from ._typing import Coords
|
| 43 |
+
|
| 44 |
+
# experimental access to the outline API
|
| 45 |
+
Outline: Callable[[], Image.core._Outline] | None
|
| 46 |
+
try:
|
| 47 |
+
Outline = Image.core.outline
|
| 48 |
+
except AttributeError:
|
| 49 |
+
Outline = None
|
| 50 |
+
|
| 51 |
+
if TYPE_CHECKING:
|
| 52 |
+
from . import ImageDraw2, ImageFont
|
| 53 |
+
|
| 54 |
+
_Ink = Union[float, Tuple[int, ...], str]
|
| 55 |
+
|
| 56 |
+
"""
|
| 57 |
+
A simple 2D drawing interface for PIL images.
|
| 58 |
+
<p>
|
| 59 |
+
Application code should use the <b>Draw</b> factory, instead of
|
| 60 |
+
directly.
|
| 61 |
+
"""
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
class ImageDraw:
|
| 65 |
+
font: (
|
| 66 |
+
ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont | None
|
| 67 |
+
) = None
|
| 68 |
+
|
| 69 |
+
def __init__(self, im: Image.Image, mode: str | None = None) -> None:
|
| 70 |
+
"""
|
| 71 |
+
Create a drawing instance.
|
| 72 |
+
|
| 73 |
+
:param im: The image to draw in.
|
| 74 |
+
:param mode: Optional mode to use for color values. For RGB
|
| 75 |
+
images, this argument can be RGB or RGBA (to blend the
|
| 76 |
+
drawing into the image). For all other modes, this argument
|
| 77 |
+
must be the same as the image mode. If omitted, the mode
|
| 78 |
+
defaults to the mode of the image.
|
| 79 |
+
"""
|
| 80 |
+
im.load()
|
| 81 |
+
if im.readonly:
|
| 82 |
+
im._copy() # make it writeable
|
| 83 |
+
blend = 0
|
| 84 |
+
if mode is None:
|
| 85 |
+
mode = im.mode
|
| 86 |
+
if mode != im.mode:
|
| 87 |
+
if mode == "RGBA" and im.mode == "RGB":
|
| 88 |
+
blend = 1
|
| 89 |
+
else:
|
| 90 |
+
msg = "mode mismatch"
|
| 91 |
+
raise ValueError(msg)
|
| 92 |
+
if mode == "P":
|
| 93 |
+
self.palette = im.palette
|
| 94 |
+
else:
|
| 95 |
+
self.palette = None
|
| 96 |
+
self._image = im
|
| 97 |
+
self.im = im.im
|
| 98 |
+
self.draw = Image.core.draw(self.im, blend)
|
| 99 |
+
self.mode = mode
|
| 100 |
+
if mode in ("I", "F"):
|
| 101 |
+
self.ink = self.draw.draw_ink(1)
|
| 102 |
+
else:
|
| 103 |
+
self.ink = self.draw.draw_ink(-1)
|
| 104 |
+
if mode in ("1", "P", "I", "F"):
|
| 105 |
+
# FIXME: fix Fill2 to properly support matte for I+F images
|
| 106 |
+
self.fontmode = "1"
|
| 107 |
+
else:
|
| 108 |
+
self.fontmode = "L" # aliasing is okay for other modes
|
| 109 |
+
self.fill = False
|
| 110 |
+
|
| 111 |
+
def getfont(
|
| 112 |
+
self,
|
| 113 |
+
) -> ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont:
|
| 114 |
+
"""
|
| 115 |
+
Get the current default font.
|
| 116 |
+
|
| 117 |
+
To set the default font for this ImageDraw instance::
|
| 118 |
+
|
| 119 |
+
from PIL import ImageDraw, ImageFont
|
| 120 |
+
draw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
|
| 121 |
+
|
| 122 |
+
To set the default font for all future ImageDraw instances::
|
| 123 |
+
|
| 124 |
+
from PIL import ImageDraw, ImageFont
|
| 125 |
+
ImageDraw.ImageDraw.font = ImageFont.truetype("Tests/fonts/FreeMono.ttf")
|
| 126 |
+
|
| 127 |
+
If the current default font is ``None``,
|
| 128 |
+
it is initialized with ``ImageFont.load_default()``.
|
| 129 |
+
|
| 130 |
+
:returns: An image font."""
|
| 131 |
+
if not self.font:
|
| 132 |
+
# FIXME: should add a font repository
|
| 133 |
+
from . import ImageFont
|
| 134 |
+
|
| 135 |
+
self.font = ImageFont.load_default()
|
| 136 |
+
return self.font
|
| 137 |
+
|
| 138 |
+
def _getfont(
|
| 139 |
+
self, font_size: float | None
|
| 140 |
+
) -> ImageFont.ImageFont | ImageFont.FreeTypeFont | ImageFont.TransposedFont:
|
| 141 |
+
if font_size is not None:
|
| 142 |
+
from . import ImageFont
|
| 143 |
+
|
| 144 |
+
return ImageFont.load_default(font_size)
|
| 145 |
+
else:
|
| 146 |
+
return self.getfont()
|
| 147 |
+
|
| 148 |
+
def _getink(
|
| 149 |
+
self, ink: _Ink | None, fill: _Ink | None = None
|
| 150 |
+
) -> tuple[int | None, int | None]:
|
| 151 |
+
result_ink = None
|
| 152 |
+
result_fill = None
|
| 153 |
+
if ink is None and fill is None:
|
| 154 |
+
if self.fill:
|
| 155 |
+
result_fill = self.ink
|
| 156 |
+
else:
|
| 157 |
+
result_ink = self.ink
|
| 158 |
+
else:
|
| 159 |
+
if ink is not None:
|
| 160 |
+
if isinstance(ink, str):
|
| 161 |
+
ink = ImageColor.getcolor(ink, self.mode)
|
| 162 |
+
if self.palette and not isinstance(ink, numbers.Number):
|
| 163 |
+
ink = self.palette.getcolor(ink, self._image)
|
| 164 |
+
result_ink = self.draw.draw_ink(ink)
|
| 165 |
+
if fill is not None:
|
| 166 |
+
if isinstance(fill, str):
|
| 167 |
+
fill = ImageColor.getcolor(fill, self.mode)
|
| 168 |
+
if self.palette and not isinstance(fill, numbers.Number):
|
| 169 |
+
fill = self.palette.getcolor(fill, self._image)
|
| 170 |
+
result_fill = self.draw.draw_ink(fill)
|
| 171 |
+
return result_ink, result_fill
|
| 172 |
+
|
| 173 |
+
def arc(
|
| 174 |
+
self,
|
| 175 |
+
xy: Coords,
|
| 176 |
+
start: float,
|
| 177 |
+
end: float,
|
| 178 |
+
fill: _Ink | None = None,
|
| 179 |
+
width: int = 1,
|
| 180 |
+
) -> None:
|
| 181 |
+
"""Draw an arc."""
|
| 182 |
+
ink, fill = self._getink(fill)
|
| 183 |
+
if ink is not None:
|
| 184 |
+
self.draw.draw_arc(xy, start, end, ink, width)
|
| 185 |
+
|
| 186 |
+
def bitmap(
|
| 187 |
+
self, xy: Sequence[int], bitmap: Image.Image, fill: _Ink | None = None
|
| 188 |
+
) -> None:
|
| 189 |
+
"""Draw a bitmap."""
|
| 190 |
+
bitmap.load()
|
| 191 |
+
ink, fill = self._getink(fill)
|
| 192 |
+
if ink is None:
|
| 193 |
+
ink = fill
|
| 194 |
+
if ink is not None:
|
| 195 |
+
self.draw.draw_bitmap(xy, bitmap.im, ink)
|
| 196 |
+
|
| 197 |
+
def chord(
|
| 198 |
+
self,
|
| 199 |
+
xy: Coords,
|
| 200 |
+
start: float,
|
| 201 |
+
end: float,
|
| 202 |
+
fill: _Ink | None = None,
|
| 203 |
+
outline: _Ink | None = None,
|
| 204 |
+
width: int = 1,
|
| 205 |
+
) -> None:
|
| 206 |
+
"""Draw a chord."""
|
| 207 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 208 |
+
if fill_ink is not None:
|
| 209 |
+
self.draw.draw_chord(xy, start, end, fill_ink, 1)
|
| 210 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 211 |
+
self.draw.draw_chord(xy, start, end, ink, 0, width)
|
| 212 |
+
|
| 213 |
+
def ellipse(
|
| 214 |
+
self,
|
| 215 |
+
xy: Coords,
|
| 216 |
+
fill: _Ink | None = None,
|
| 217 |
+
outline: _Ink | None = None,
|
| 218 |
+
width: int = 1,
|
| 219 |
+
) -> None:
|
| 220 |
+
"""Draw an ellipse."""
|
| 221 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 222 |
+
if fill_ink is not None:
|
| 223 |
+
self.draw.draw_ellipse(xy, fill_ink, 1)
|
| 224 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 225 |
+
self.draw.draw_ellipse(xy, ink, 0, width)
|
| 226 |
+
|
| 227 |
+
def circle(
|
| 228 |
+
self,
|
| 229 |
+
xy: Sequence[float],
|
| 230 |
+
radius: float,
|
| 231 |
+
fill: _Ink | None = None,
|
| 232 |
+
outline: _Ink | None = None,
|
| 233 |
+
width: int = 1,
|
| 234 |
+
) -> None:
|
| 235 |
+
"""Draw a circle given center coordinates and a radius."""
|
| 236 |
+
ellipse_xy = (xy[0] - radius, xy[1] - radius, xy[0] + radius, xy[1] + radius)
|
| 237 |
+
self.ellipse(ellipse_xy, fill, outline, width)
|
| 238 |
+
|
| 239 |
+
def line(
|
| 240 |
+
self,
|
| 241 |
+
xy: Coords,
|
| 242 |
+
fill: _Ink | None = None,
|
| 243 |
+
width: int = 0,
|
| 244 |
+
joint: str | None = None,
|
| 245 |
+
) -> None:
|
| 246 |
+
"""Draw a line, or a connected sequence of line segments."""
|
| 247 |
+
ink = self._getink(fill)[0]
|
| 248 |
+
if ink is not None:
|
| 249 |
+
self.draw.draw_lines(xy, ink, width)
|
| 250 |
+
if joint == "curve" and width > 4:
|
| 251 |
+
points: Sequence[Sequence[float]]
|
| 252 |
+
if isinstance(xy[0], (list, tuple)):
|
| 253 |
+
points = cast(Sequence[Sequence[float]], xy)
|
| 254 |
+
else:
|
| 255 |
+
points = [
|
| 256 |
+
cast(Sequence[float], tuple(xy[i : i + 2]))
|
| 257 |
+
for i in range(0, len(xy), 2)
|
| 258 |
+
]
|
| 259 |
+
for i in range(1, len(points) - 1):
|
| 260 |
+
point = points[i]
|
| 261 |
+
angles = [
|
| 262 |
+
math.degrees(math.atan2(end[0] - start[0], start[1] - end[1]))
|
| 263 |
+
% 360
|
| 264 |
+
for start, end in (
|
| 265 |
+
(points[i - 1], point),
|
| 266 |
+
(point, points[i + 1]),
|
| 267 |
+
)
|
| 268 |
+
]
|
| 269 |
+
if angles[0] == angles[1]:
|
| 270 |
+
# This is a straight line, so no joint is required
|
| 271 |
+
continue
|
| 272 |
+
|
| 273 |
+
def coord_at_angle(
|
| 274 |
+
coord: Sequence[float], angle: float
|
| 275 |
+
) -> tuple[float, ...]:
|
| 276 |
+
x, y = coord
|
| 277 |
+
angle -= 90
|
| 278 |
+
distance = width / 2 - 1
|
| 279 |
+
return tuple(
|
| 280 |
+
p + (math.floor(p_d) if p_d > 0 else math.ceil(p_d))
|
| 281 |
+
for p, p_d in (
|
| 282 |
+
(x, distance * math.cos(math.radians(angle))),
|
| 283 |
+
(y, distance * math.sin(math.radians(angle))),
|
| 284 |
+
)
|
| 285 |
+
)
|
| 286 |
+
|
| 287 |
+
flipped = (
|
| 288 |
+
angles[1] > angles[0] and angles[1] - 180 > angles[0]
|
| 289 |
+
) or (angles[1] < angles[0] and angles[1] + 180 > angles[0])
|
| 290 |
+
coords = [
|
| 291 |
+
(point[0] - width / 2 + 1, point[1] - width / 2 + 1),
|
| 292 |
+
(point[0] + width / 2 - 1, point[1] + width / 2 - 1),
|
| 293 |
+
]
|
| 294 |
+
if flipped:
|
| 295 |
+
start, end = (angles[1] + 90, angles[0] + 90)
|
| 296 |
+
else:
|
| 297 |
+
start, end = (angles[0] - 90, angles[1] - 90)
|
| 298 |
+
self.pieslice(coords, start - 90, end - 90, fill)
|
| 299 |
+
|
| 300 |
+
if width > 8:
|
| 301 |
+
# Cover potential gaps between the line and the joint
|
| 302 |
+
if flipped:
|
| 303 |
+
gap_coords = [
|
| 304 |
+
coord_at_angle(point, angles[0] + 90),
|
| 305 |
+
point,
|
| 306 |
+
coord_at_angle(point, angles[1] + 90),
|
| 307 |
+
]
|
| 308 |
+
else:
|
| 309 |
+
gap_coords = [
|
| 310 |
+
coord_at_angle(point, angles[0] - 90),
|
| 311 |
+
point,
|
| 312 |
+
coord_at_angle(point, angles[1] - 90),
|
| 313 |
+
]
|
| 314 |
+
self.line(gap_coords, fill, width=3)
|
| 315 |
+
|
| 316 |
+
def shape(
|
| 317 |
+
self,
|
| 318 |
+
shape: Image.core._Outline,
|
| 319 |
+
fill: _Ink | None = None,
|
| 320 |
+
outline: _Ink | None = None,
|
| 321 |
+
) -> None:
|
| 322 |
+
"""(Experimental) Draw a shape."""
|
| 323 |
+
shape.close()
|
| 324 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 325 |
+
if fill_ink is not None:
|
| 326 |
+
self.draw.draw_outline(shape, fill_ink, 1)
|
| 327 |
+
if ink is not None and ink != fill_ink:
|
| 328 |
+
self.draw.draw_outline(shape, ink, 0)
|
| 329 |
+
|
| 330 |
+
def pieslice(
|
| 331 |
+
self,
|
| 332 |
+
xy: Coords,
|
| 333 |
+
start: float,
|
| 334 |
+
end: float,
|
| 335 |
+
fill: _Ink | None = None,
|
| 336 |
+
outline: _Ink | None = None,
|
| 337 |
+
width: int = 1,
|
| 338 |
+
) -> None:
|
| 339 |
+
"""Draw a pieslice."""
|
| 340 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 341 |
+
if fill_ink is not None:
|
| 342 |
+
self.draw.draw_pieslice(xy, start, end, fill_ink, 1)
|
| 343 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 344 |
+
self.draw.draw_pieslice(xy, start, end, ink, 0, width)
|
| 345 |
+
|
| 346 |
+
def point(self, xy: Coords, fill: _Ink | None = None) -> None:
|
| 347 |
+
"""Draw one or more individual pixels."""
|
| 348 |
+
ink, fill = self._getink(fill)
|
| 349 |
+
if ink is not None:
|
| 350 |
+
self.draw.draw_points(xy, ink)
|
| 351 |
+
|
| 352 |
+
def polygon(
|
| 353 |
+
self,
|
| 354 |
+
xy: Coords,
|
| 355 |
+
fill: _Ink | None = None,
|
| 356 |
+
outline: _Ink | None = None,
|
| 357 |
+
width: int = 1,
|
| 358 |
+
) -> None:
|
| 359 |
+
"""Draw a polygon."""
|
| 360 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 361 |
+
if fill_ink is not None:
|
| 362 |
+
self.draw.draw_polygon(xy, fill_ink, 1)
|
| 363 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 364 |
+
if width == 1:
|
| 365 |
+
self.draw.draw_polygon(xy, ink, 0, width)
|
| 366 |
+
elif self.im is not None:
|
| 367 |
+
# To avoid expanding the polygon outwards,
|
| 368 |
+
# use the fill as a mask
|
| 369 |
+
mask = Image.new("1", self.im.size)
|
| 370 |
+
mask_ink = self._getink(1)[0]
|
| 371 |
+
|
| 372 |
+
fill_im = mask.copy()
|
| 373 |
+
draw = Draw(fill_im)
|
| 374 |
+
draw.draw.draw_polygon(xy, mask_ink, 1)
|
| 375 |
+
|
| 376 |
+
ink_im = mask.copy()
|
| 377 |
+
draw = Draw(ink_im)
|
| 378 |
+
width = width * 2 - 1
|
| 379 |
+
draw.draw.draw_polygon(xy, mask_ink, 0, width)
|
| 380 |
+
|
| 381 |
+
mask.paste(ink_im, mask=fill_im)
|
| 382 |
+
|
| 383 |
+
im = Image.new(self.mode, self.im.size)
|
| 384 |
+
draw = Draw(im)
|
| 385 |
+
draw.draw.draw_polygon(xy, ink, 0, width)
|
| 386 |
+
self.im.paste(im.im, (0, 0) + im.size, mask.im)
|
| 387 |
+
|
| 388 |
+
def regular_polygon(
|
| 389 |
+
self,
|
| 390 |
+
bounding_circle: Sequence[Sequence[float] | float],
|
| 391 |
+
n_sides: int,
|
| 392 |
+
rotation: float = 0,
|
| 393 |
+
fill: _Ink | None = None,
|
| 394 |
+
outline: _Ink | None = None,
|
| 395 |
+
width: int = 1,
|
| 396 |
+
) -> None:
|
| 397 |
+
"""Draw a regular polygon."""
|
| 398 |
+
xy = _compute_regular_polygon_vertices(bounding_circle, n_sides, rotation)
|
| 399 |
+
self.polygon(xy, fill, outline, width)
|
| 400 |
+
|
| 401 |
+
def rectangle(
|
| 402 |
+
self,
|
| 403 |
+
xy: Coords,
|
| 404 |
+
fill: _Ink | None = None,
|
| 405 |
+
outline: _Ink | None = None,
|
| 406 |
+
width: int = 1,
|
| 407 |
+
) -> None:
|
| 408 |
+
"""Draw a rectangle."""
|
| 409 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 410 |
+
if fill_ink is not None:
|
| 411 |
+
self.draw.draw_rectangle(xy, fill_ink, 1)
|
| 412 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 413 |
+
self.draw.draw_rectangle(xy, ink, 0, width)
|
| 414 |
+
|
| 415 |
+
def rounded_rectangle(
|
| 416 |
+
self,
|
| 417 |
+
xy: Coords,
|
| 418 |
+
radius: float = 0,
|
| 419 |
+
fill: _Ink | None = None,
|
| 420 |
+
outline: _Ink | None = None,
|
| 421 |
+
width: int = 1,
|
| 422 |
+
*,
|
| 423 |
+
corners: tuple[bool, bool, bool, bool] | None = None,
|
| 424 |
+
) -> None:
|
| 425 |
+
"""Draw a rounded rectangle."""
|
| 426 |
+
if isinstance(xy[0], (list, tuple)):
|
| 427 |
+
(x0, y0), (x1, y1) = cast(Sequence[Sequence[float]], xy)
|
| 428 |
+
else:
|
| 429 |
+
x0, y0, x1, y1 = cast(Sequence[float], xy)
|
| 430 |
+
if x1 < x0:
|
| 431 |
+
msg = "x1 must be greater than or equal to x0"
|
| 432 |
+
raise ValueError(msg)
|
| 433 |
+
if y1 < y0:
|
| 434 |
+
msg = "y1 must be greater than or equal to y0"
|
| 435 |
+
raise ValueError(msg)
|
| 436 |
+
if corners is None:
|
| 437 |
+
corners = (True, True, True, True)
|
| 438 |
+
|
| 439 |
+
d = radius * 2
|
| 440 |
+
|
| 441 |
+
x0 = round(x0)
|
| 442 |
+
y0 = round(y0)
|
| 443 |
+
x1 = round(x1)
|
| 444 |
+
y1 = round(y1)
|
| 445 |
+
full_x, full_y = False, False
|
| 446 |
+
if all(corners):
|
| 447 |
+
full_x = d >= x1 - x0 - 1
|
| 448 |
+
if full_x:
|
| 449 |
+
# The two left and two right corners are joined
|
| 450 |
+
d = x1 - x0
|
| 451 |
+
full_y = d >= y1 - y0 - 1
|
| 452 |
+
if full_y:
|
| 453 |
+
# The two top and two bottom corners are joined
|
| 454 |
+
d = y1 - y0
|
| 455 |
+
if full_x and full_y:
|
| 456 |
+
# If all corners are joined, that is a circle
|
| 457 |
+
return self.ellipse(xy, fill, outline, width)
|
| 458 |
+
|
| 459 |
+
if d == 0 or not any(corners):
|
| 460 |
+
# If the corners have no curve,
|
| 461 |
+
# or there are no corners,
|
| 462 |
+
# that is a rectangle
|
| 463 |
+
return self.rectangle(xy, fill, outline, width)
|
| 464 |
+
|
| 465 |
+
r = int(d // 2)
|
| 466 |
+
ink, fill_ink = self._getink(outline, fill)
|
| 467 |
+
|
| 468 |
+
def draw_corners(pieslice: bool) -> None:
|
| 469 |
+
parts: tuple[tuple[tuple[float, float, float, float], int, int], ...]
|
| 470 |
+
if full_x:
|
| 471 |
+
# Draw top and bottom halves
|
| 472 |
+
parts = (
|
| 473 |
+
((x0, y0, x0 + d, y0 + d), 180, 360),
|
| 474 |
+
((x0, y1 - d, x0 + d, y1), 0, 180),
|
| 475 |
+
)
|
| 476 |
+
elif full_y:
|
| 477 |
+
# Draw left and right halves
|
| 478 |
+
parts = (
|
| 479 |
+
((x0, y0, x0 + d, y0 + d), 90, 270),
|
| 480 |
+
((x1 - d, y0, x1, y0 + d), 270, 90),
|
| 481 |
+
)
|
| 482 |
+
else:
|
| 483 |
+
# Draw four separate corners
|
| 484 |
+
parts = tuple(
|
| 485 |
+
part
|
| 486 |
+
for i, part in enumerate(
|
| 487 |
+
(
|
| 488 |
+
((x0, y0, x0 + d, y0 + d), 180, 270),
|
| 489 |
+
((x1 - d, y0, x1, y0 + d), 270, 360),
|
| 490 |
+
((x1 - d, y1 - d, x1, y1), 0, 90),
|
| 491 |
+
((x0, y1 - d, x0 + d, y1), 90, 180),
|
| 492 |
+
)
|
| 493 |
+
)
|
| 494 |
+
if corners[i]
|
| 495 |
+
)
|
| 496 |
+
for part in parts:
|
| 497 |
+
if pieslice:
|
| 498 |
+
self.draw.draw_pieslice(*(part + (fill_ink, 1)))
|
| 499 |
+
else:
|
| 500 |
+
self.draw.draw_arc(*(part + (ink, width)))
|
| 501 |
+
|
| 502 |
+
if fill_ink is not None:
|
| 503 |
+
draw_corners(True)
|
| 504 |
+
|
| 505 |
+
if full_x:
|
| 506 |
+
self.draw.draw_rectangle((x0, y0 + r + 1, x1, y1 - r - 1), fill_ink, 1)
|
| 507 |
+
else:
|
| 508 |
+
self.draw.draw_rectangle((x0 + r + 1, y0, x1 - r - 1, y1), fill_ink, 1)
|
| 509 |
+
if not full_x and not full_y:
|
| 510 |
+
left = [x0, y0, x0 + r, y1]
|
| 511 |
+
if corners[0]:
|
| 512 |
+
left[1] += r + 1
|
| 513 |
+
if corners[3]:
|
| 514 |
+
left[3] -= r + 1
|
| 515 |
+
self.draw.draw_rectangle(left, fill_ink, 1)
|
| 516 |
+
|
| 517 |
+
right = [x1 - r, y0, x1, y1]
|
| 518 |
+
if corners[1]:
|
| 519 |
+
right[1] += r + 1
|
| 520 |
+
if corners[2]:
|
| 521 |
+
right[3] -= r + 1
|
| 522 |
+
self.draw.draw_rectangle(right, fill_ink, 1)
|
| 523 |
+
if ink is not None and ink != fill_ink and width != 0:
|
| 524 |
+
draw_corners(False)
|
| 525 |
+
|
| 526 |
+
if not full_x:
|
| 527 |
+
top = [x0, y0, x1, y0 + width - 1]
|
| 528 |
+
if corners[0]:
|
| 529 |
+
top[0] += r + 1
|
| 530 |
+
if corners[1]:
|
| 531 |
+
top[2] -= r + 1
|
| 532 |
+
self.draw.draw_rectangle(top, ink, 1)
|
| 533 |
+
|
| 534 |
+
bottom = [x0, y1 - width + 1, x1, y1]
|
| 535 |
+
if corners[3]:
|
| 536 |
+
bottom[0] += r + 1
|
| 537 |
+
if corners[2]:
|
| 538 |
+
bottom[2] -= r + 1
|
| 539 |
+
self.draw.draw_rectangle(bottom, ink, 1)
|
| 540 |
+
if not full_y:
|
| 541 |
+
left = [x0, y0, x0 + width - 1, y1]
|
| 542 |
+
if corners[0]:
|
| 543 |
+
left[1] += r + 1
|
| 544 |
+
if corners[3]:
|
| 545 |
+
left[3] -= r + 1
|
| 546 |
+
self.draw.draw_rectangle(left, ink, 1)
|
| 547 |
+
|
| 548 |
+
right = [x1 - width + 1, y0, x1, y1]
|
| 549 |
+
if corners[1]:
|
| 550 |
+
right[1] += r + 1
|
| 551 |
+
if corners[2]:
|
| 552 |
+
right[3] -= r + 1
|
| 553 |
+
self.draw.draw_rectangle(right, ink, 1)
|
| 554 |
+
|
| 555 |
+
def _multiline_check(self, text: AnyStr) -> bool:
|
| 556 |
+
split_character = "\n" if isinstance(text, str) else b"\n"
|
| 557 |
+
|
| 558 |
+
return split_character in text
|
| 559 |
+
|
| 560 |
+
def _multiline_split(self, text: AnyStr) -> list[AnyStr]:
|
| 561 |
+
return text.split("\n" if isinstance(text, str) else b"\n")
|
| 562 |
+
|
| 563 |
+
def _multiline_spacing(self, font, spacing, stroke_width):
|
| 564 |
+
return (
|
| 565 |
+
self.textbbox((0, 0), "A", font, stroke_width=stroke_width)[3]
|
| 566 |
+
+ stroke_width
|
| 567 |
+
+ spacing
|
| 568 |
+
)
|
| 569 |
+
|
| 570 |
+
def text(
|
| 571 |
+
self,
|
| 572 |
+
xy: tuple[float, float],
|
| 573 |
+
text: str,
|
| 574 |
+
fill=None,
|
| 575 |
+
font: (
|
| 576 |
+
ImageFont.ImageFont
|
| 577 |
+
| ImageFont.FreeTypeFont
|
| 578 |
+
| ImageFont.TransposedFont
|
| 579 |
+
| None
|
| 580 |
+
) = None,
|
| 581 |
+
anchor=None,
|
| 582 |
+
spacing=4,
|
| 583 |
+
align="left",
|
| 584 |
+
direction=None,
|
| 585 |
+
features=None,
|
| 586 |
+
language=None,
|
| 587 |
+
stroke_width=0,
|
| 588 |
+
stroke_fill=None,
|
| 589 |
+
embedded_color=False,
|
| 590 |
+
*args,
|
| 591 |
+
**kwargs,
|
| 592 |
+
) -> None:
|
| 593 |
+
"""Draw text."""
|
| 594 |
+
if embedded_color and self.mode not in ("RGB", "RGBA"):
|
| 595 |
+
msg = "Embedded color supported only in RGB and RGBA modes"
|
| 596 |
+
raise ValueError(msg)
|
| 597 |
+
|
| 598 |
+
if font is None:
|
| 599 |
+
font = self._getfont(kwargs.get("font_size"))
|
| 600 |
+
|
| 601 |
+
if self._multiline_check(text):
|
| 602 |
+
return self.multiline_text(
|
| 603 |
+
xy,
|
| 604 |
+
text,
|
| 605 |
+
fill,
|
| 606 |
+
font,
|
| 607 |
+
anchor,
|
| 608 |
+
spacing,
|
| 609 |
+
align,
|
| 610 |
+
direction,
|
| 611 |
+
features,
|
| 612 |
+
language,
|
| 613 |
+
stroke_width,
|
| 614 |
+
stroke_fill,
|
| 615 |
+
embedded_color,
|
| 616 |
+
)
|
| 617 |
+
|
| 618 |
+
def getink(fill: _Ink | None) -> int:
|
| 619 |
+
ink, fill_ink = self._getink(fill)
|
| 620 |
+
if ink is None:
|
| 621 |
+
assert fill_ink is not None
|
| 622 |
+
return fill_ink
|
| 623 |
+
return ink
|
| 624 |
+
|
| 625 |
+
def draw_text(ink, stroke_width=0, stroke_offset=None) -> None:
|
| 626 |
+
mode = self.fontmode
|
| 627 |
+
if stroke_width == 0 and embedded_color:
|
| 628 |
+
mode = "RGBA"
|
| 629 |
+
coord = []
|
| 630 |
+
start = []
|
| 631 |
+
for i in range(2):
|
| 632 |
+
coord.append(int(xy[i]))
|
| 633 |
+
start.append(math.modf(xy[i])[0])
|
| 634 |
+
try:
|
| 635 |
+
mask, offset = font.getmask2( # type: ignore[union-attr,misc]
|
| 636 |
+
text,
|
| 637 |
+
mode,
|
| 638 |
+
direction=direction,
|
| 639 |
+
features=features,
|
| 640 |
+
language=language,
|
| 641 |
+
stroke_width=stroke_width,
|
| 642 |
+
anchor=anchor,
|
| 643 |
+
ink=ink,
|
| 644 |
+
start=start,
|
| 645 |
+
*args,
|
| 646 |
+
**kwargs,
|
| 647 |
+
)
|
| 648 |
+
coord = [coord[0] + offset[0], coord[1] + offset[1]]
|
| 649 |
+
except AttributeError:
|
| 650 |
+
try:
|
| 651 |
+
mask = font.getmask( # type: ignore[misc]
|
| 652 |
+
text,
|
| 653 |
+
mode,
|
| 654 |
+
direction,
|
| 655 |
+
features,
|
| 656 |
+
language,
|
| 657 |
+
stroke_width,
|
| 658 |
+
anchor,
|
| 659 |
+
ink,
|
| 660 |
+
start=start,
|
| 661 |
+
*args,
|
| 662 |
+
**kwargs,
|
| 663 |
+
)
|
| 664 |
+
except TypeError:
|
| 665 |
+
mask = font.getmask(text)
|
| 666 |
+
if stroke_offset:
|
| 667 |
+
coord = [coord[0] + stroke_offset[0], coord[1] + stroke_offset[1]]
|
| 668 |
+
if mode == "RGBA":
|
| 669 |
+
# font.getmask2(mode="RGBA") returns color in RGB bands and mask in A
|
| 670 |
+
# extract mask and set text alpha
|
| 671 |
+
color, mask = mask, mask.getband(3)
|
| 672 |
+
ink_alpha = struct.pack("i", ink)[3]
|
| 673 |
+
color.fillband(3, ink_alpha)
|
| 674 |
+
x, y = coord
|
| 675 |
+
if self.im is not None:
|
| 676 |
+
self.im.paste(
|
| 677 |
+
color, (x, y, x + mask.size[0], y + mask.size[1]), mask
|
| 678 |
+
)
|
| 679 |
+
else:
|
| 680 |
+
self.draw.draw_bitmap(coord, mask, ink)
|
| 681 |
+
|
| 682 |
+
ink = getink(fill)
|
| 683 |
+
if ink is not None:
|
| 684 |
+
stroke_ink = None
|
| 685 |
+
if stroke_width:
|
| 686 |
+
stroke_ink = getink(stroke_fill) if stroke_fill is not None else ink
|
| 687 |
+
|
| 688 |
+
if stroke_ink is not None:
|
| 689 |
+
# Draw stroked text
|
| 690 |
+
draw_text(stroke_ink, stroke_width)
|
| 691 |
+
|
| 692 |
+
# Draw normal text
|
| 693 |
+
draw_text(ink, 0)
|
| 694 |
+
else:
|
| 695 |
+
# Only draw normal text
|
| 696 |
+
draw_text(ink)
|
| 697 |
+
|
| 698 |
+
def multiline_text(
|
| 699 |
+
self,
|
| 700 |
+
xy: tuple[float, float],
|
| 701 |
+
text: str,
|
| 702 |
+
fill=None,
|
| 703 |
+
font: (
|
| 704 |
+
ImageFont.ImageFont
|
| 705 |
+
| ImageFont.FreeTypeFont
|
| 706 |
+
| ImageFont.TransposedFont
|
| 707 |
+
| None
|
| 708 |
+
) = None,
|
| 709 |
+
anchor=None,
|
| 710 |
+
spacing=4,
|
| 711 |
+
align="left",
|
| 712 |
+
direction=None,
|
| 713 |
+
features=None,
|
| 714 |
+
language=None,
|
| 715 |
+
stroke_width=0,
|
| 716 |
+
stroke_fill=None,
|
| 717 |
+
embedded_color=False,
|
| 718 |
+
*,
|
| 719 |
+
font_size=None,
|
| 720 |
+
) -> None:
|
| 721 |
+
if direction == "ttb":
|
| 722 |
+
msg = "ttb direction is unsupported for multiline text"
|
| 723 |
+
raise ValueError(msg)
|
| 724 |
+
|
| 725 |
+
if anchor is None:
|
| 726 |
+
anchor = "la"
|
| 727 |
+
elif len(anchor) != 2:
|
| 728 |
+
msg = "anchor must be a 2 character string"
|
| 729 |
+
raise ValueError(msg)
|
| 730 |
+
elif anchor[1] in "tb":
|
| 731 |
+
msg = "anchor not supported for multiline text"
|
| 732 |
+
raise ValueError(msg)
|
| 733 |
+
|
| 734 |
+
if font is None:
|
| 735 |
+
font = self._getfont(font_size)
|
| 736 |
+
|
| 737 |
+
widths = []
|
| 738 |
+
max_width: float = 0
|
| 739 |
+
lines = self._multiline_split(text)
|
| 740 |
+
line_spacing = self._multiline_spacing(font, spacing, stroke_width)
|
| 741 |
+
for line in lines:
|
| 742 |
+
line_width = self.textlength(
|
| 743 |
+
line, font, direction=direction, features=features, language=language
|
| 744 |
+
)
|
| 745 |
+
widths.append(line_width)
|
| 746 |
+
max_width = max(max_width, line_width)
|
| 747 |
+
|
| 748 |
+
top = xy[1]
|
| 749 |
+
if anchor[1] == "m":
|
| 750 |
+
top -= (len(lines) - 1) * line_spacing / 2.0
|
| 751 |
+
elif anchor[1] == "d":
|
| 752 |
+
top -= (len(lines) - 1) * line_spacing
|
| 753 |
+
|
| 754 |
+
for idx, line in enumerate(lines):
|
| 755 |
+
left = xy[0]
|
| 756 |
+
width_difference = max_width - widths[idx]
|
| 757 |
+
|
| 758 |
+
# first align left by anchor
|
| 759 |
+
if anchor[0] == "m":
|
| 760 |
+
left -= width_difference / 2.0
|
| 761 |
+
elif anchor[0] == "r":
|
| 762 |
+
left -= width_difference
|
| 763 |
+
|
| 764 |
+
# then align by align parameter
|
| 765 |
+
if align == "left":
|
| 766 |
+
pass
|
| 767 |
+
elif align == "center":
|
| 768 |
+
left += width_difference / 2.0
|
| 769 |
+
elif align == "right":
|
| 770 |
+
left += width_difference
|
| 771 |
+
else:
|
| 772 |
+
msg = 'align must be "left", "center" or "right"'
|
| 773 |
+
raise ValueError(msg)
|
| 774 |
+
|
| 775 |
+
self.text(
|
| 776 |
+
(left, top),
|
| 777 |
+
line,
|
| 778 |
+
fill,
|
| 779 |
+
font,
|
| 780 |
+
anchor,
|
| 781 |
+
direction=direction,
|
| 782 |
+
features=features,
|
| 783 |
+
language=language,
|
| 784 |
+
stroke_width=stroke_width,
|
| 785 |
+
stroke_fill=stroke_fill,
|
| 786 |
+
embedded_color=embedded_color,
|
| 787 |
+
)
|
| 788 |
+
top += line_spacing
|
| 789 |
+
|
| 790 |
+
def textlength(
|
| 791 |
+
self,
|
| 792 |
+
text: str,
|
| 793 |
+
font: (
|
| 794 |
+
ImageFont.ImageFont
|
| 795 |
+
| ImageFont.FreeTypeFont
|
| 796 |
+
| ImageFont.TransposedFont
|
| 797 |
+
| None
|
| 798 |
+
) = None,
|
| 799 |
+
direction=None,
|
| 800 |
+
features=None,
|
| 801 |
+
language=None,
|
| 802 |
+
embedded_color=False,
|
| 803 |
+
*,
|
| 804 |
+
font_size=None,
|
| 805 |
+
) -> float:
|
| 806 |
+
"""Get the length of a given string, in pixels with 1/64 precision."""
|
| 807 |
+
if self._multiline_check(text):
|
| 808 |
+
msg = "can't measure length of multiline text"
|
| 809 |
+
raise ValueError(msg)
|
| 810 |
+
if embedded_color and self.mode not in ("RGB", "RGBA"):
|
| 811 |
+
msg = "Embedded color supported only in RGB and RGBA modes"
|
| 812 |
+
raise ValueError(msg)
|
| 813 |
+
|
| 814 |
+
if font is None:
|
| 815 |
+
font = self._getfont(font_size)
|
| 816 |
+
mode = "RGBA" if embedded_color else self.fontmode
|
| 817 |
+
return font.getlength(text, mode, direction, features, language)
|
| 818 |
+
|
| 819 |
+
def textbbox(
|
| 820 |
+
self,
|
| 821 |
+
xy,
|
| 822 |
+
text,
|
| 823 |
+
font=None,
|
| 824 |
+
anchor=None,
|
| 825 |
+
spacing=4,
|
| 826 |
+
align="left",
|
| 827 |
+
direction=None,
|
| 828 |
+
features=None,
|
| 829 |
+
language=None,
|
| 830 |
+
stroke_width=0,
|
| 831 |
+
embedded_color=False,
|
| 832 |
+
*,
|
| 833 |
+
font_size=None,
|
| 834 |
+
) -> tuple[int, int, int, int]:
|
| 835 |
+
"""Get the bounding box of a given string, in pixels."""
|
| 836 |
+
if embedded_color and self.mode not in ("RGB", "RGBA"):
|
| 837 |
+
msg = "Embedded color supported only in RGB and RGBA modes"
|
| 838 |
+
raise ValueError(msg)
|
| 839 |
+
|
| 840 |
+
if font is None:
|
| 841 |
+
font = self._getfont(font_size)
|
| 842 |
+
|
| 843 |
+
if self._multiline_check(text):
|
| 844 |
+
return self.multiline_textbbox(
|
| 845 |
+
xy,
|
| 846 |
+
text,
|
| 847 |
+
font,
|
| 848 |
+
anchor,
|
| 849 |
+
spacing,
|
| 850 |
+
align,
|
| 851 |
+
direction,
|
| 852 |
+
features,
|
| 853 |
+
language,
|
| 854 |
+
stroke_width,
|
| 855 |
+
embedded_color,
|
| 856 |
+
)
|
| 857 |
+
|
| 858 |
+
mode = "RGBA" if embedded_color else self.fontmode
|
| 859 |
+
bbox = font.getbbox(
|
| 860 |
+
text, mode, direction, features, language, stroke_width, anchor
|
| 861 |
+
)
|
| 862 |
+
return bbox[0] + xy[0], bbox[1] + xy[1], bbox[2] + xy[0], bbox[3] + xy[1]
|
| 863 |
+
|
| 864 |
+
def multiline_textbbox(
|
| 865 |
+
self,
|
| 866 |
+
xy,
|
| 867 |
+
text,
|
| 868 |
+
font=None,
|
| 869 |
+
anchor=None,
|
| 870 |
+
spacing=4,
|
| 871 |
+
align="left",
|
| 872 |
+
direction=None,
|
| 873 |
+
features=None,
|
| 874 |
+
language=None,
|
| 875 |
+
stroke_width=0,
|
| 876 |
+
embedded_color=False,
|
| 877 |
+
*,
|
| 878 |
+
font_size=None,
|
| 879 |
+
) -> tuple[int, int, int, int]:
|
| 880 |
+
if direction == "ttb":
|
| 881 |
+
msg = "ttb direction is unsupported for multiline text"
|
| 882 |
+
raise ValueError(msg)
|
| 883 |
+
|
| 884 |
+
if anchor is None:
|
| 885 |
+
anchor = "la"
|
| 886 |
+
elif len(anchor) != 2:
|
| 887 |
+
msg = "anchor must be a 2 character string"
|
| 888 |
+
raise ValueError(msg)
|
| 889 |
+
elif anchor[1] in "tb":
|
| 890 |
+
msg = "anchor not supported for multiline text"
|
| 891 |
+
raise ValueError(msg)
|
| 892 |
+
|
| 893 |
+
if font is None:
|
| 894 |
+
font = self._getfont(font_size)
|
| 895 |
+
|
| 896 |
+
widths = []
|
| 897 |
+
max_width: float = 0
|
| 898 |
+
lines = self._multiline_split(text)
|
| 899 |
+
line_spacing = self._multiline_spacing(font, spacing, stroke_width)
|
| 900 |
+
for line in lines:
|
| 901 |
+
line_width = self.textlength(
|
| 902 |
+
line,
|
| 903 |
+
font,
|
| 904 |
+
direction=direction,
|
| 905 |
+
features=features,
|
| 906 |
+
language=language,
|
| 907 |
+
embedded_color=embedded_color,
|
| 908 |
+
)
|
| 909 |
+
widths.append(line_width)
|
| 910 |
+
max_width = max(max_width, line_width)
|
| 911 |
+
|
| 912 |
+
top = xy[1]
|
| 913 |
+
if anchor[1] == "m":
|
| 914 |
+
top -= (len(lines) - 1) * line_spacing / 2.0
|
| 915 |
+
elif anchor[1] == "d":
|
| 916 |
+
top -= (len(lines) - 1) * line_spacing
|
| 917 |
+
|
| 918 |
+
bbox: tuple[int, int, int, int] | None = None
|
| 919 |
+
|
| 920 |
+
for idx, line in enumerate(lines):
|
| 921 |
+
left = xy[0]
|
| 922 |
+
width_difference = max_width - widths[idx]
|
| 923 |
+
|
| 924 |
+
# first align left by anchor
|
| 925 |
+
if anchor[0] == "m":
|
| 926 |
+
left -= width_difference / 2.0
|
| 927 |
+
elif anchor[0] == "r":
|
| 928 |
+
left -= width_difference
|
| 929 |
+
|
| 930 |
+
# then align by align parameter
|
| 931 |
+
if align == "left":
|
| 932 |
+
pass
|
| 933 |
+
elif align == "center":
|
| 934 |
+
left += width_difference / 2.0
|
| 935 |
+
elif align == "right":
|
| 936 |
+
left += width_difference
|
| 937 |
+
else:
|
| 938 |
+
msg = 'align must be "left", "center" or "right"'
|
| 939 |
+
raise ValueError(msg)
|
| 940 |
+
|
| 941 |
+
bbox_line = self.textbbox(
|
| 942 |
+
(left, top),
|
| 943 |
+
line,
|
| 944 |
+
font,
|
| 945 |
+
anchor,
|
| 946 |
+
direction=direction,
|
| 947 |
+
features=features,
|
| 948 |
+
language=language,
|
| 949 |
+
stroke_width=stroke_width,
|
| 950 |
+
embedded_color=embedded_color,
|
| 951 |
+
)
|
| 952 |
+
if bbox is None:
|
| 953 |
+
bbox = bbox_line
|
| 954 |
+
else:
|
| 955 |
+
bbox = (
|
| 956 |
+
min(bbox[0], bbox_line[0]),
|
| 957 |
+
min(bbox[1], bbox_line[1]),
|
| 958 |
+
max(bbox[2], bbox_line[2]),
|
| 959 |
+
max(bbox[3], bbox_line[3]),
|
| 960 |
+
)
|
| 961 |
+
|
| 962 |
+
top += line_spacing
|
| 963 |
+
|
| 964 |
+
if bbox is None:
|
| 965 |
+
return xy[0], xy[1], xy[0], xy[1]
|
| 966 |
+
return bbox
|
| 967 |
+
|
| 968 |
+
|
| 969 |
+
def Draw(im: Image.Image, mode: str | None = None) -> ImageDraw:
|
| 970 |
+
"""
|
| 971 |
+
A simple 2D drawing interface for PIL images.
|
| 972 |
+
|
| 973 |
+
:param im: The image to draw in.
|
| 974 |
+
:param mode: Optional mode to use for color values. For RGB
|
| 975 |
+
images, this argument can be RGB or RGBA (to blend the
|
| 976 |
+
drawing into the image). For all other modes, this argument
|
| 977 |
+
must be the same as the image mode. If omitted, the mode
|
| 978 |
+
defaults to the mode of the image.
|
| 979 |
+
"""
|
| 980 |
+
try:
|
| 981 |
+
return getattr(im, "getdraw")(mode)
|
| 982 |
+
except AttributeError:
|
| 983 |
+
return ImageDraw(im, mode)
|
| 984 |
+
|
| 985 |
+
|
| 986 |
+
def getdraw(
|
| 987 |
+
im: Image.Image | None = None, hints: list[str] | None = None
|
| 988 |
+
) -> tuple[ImageDraw2.Draw | None, ModuleType]:
|
| 989 |
+
"""
|
| 990 |
+
:param im: The image to draw in.
|
| 991 |
+
:param hints: An optional list of hints. Deprecated.
|
| 992 |
+
:returns: A (drawing context, drawing resource factory) tuple.
|
| 993 |
+
"""
|
| 994 |
+
if hints is not None:
|
| 995 |
+
deprecate("'hints' parameter", 12)
|
| 996 |
+
from . import ImageDraw2
|
| 997 |
+
|
| 998 |
+
draw = ImageDraw2.Draw(im) if im is not None else None
|
| 999 |
+
return draw, ImageDraw2
|
| 1000 |
+
|
| 1001 |
+
|
| 1002 |
+
def floodfill(
|
| 1003 |
+
image: Image.Image,
|
| 1004 |
+
xy: tuple[int, int],
|
| 1005 |
+
value: float | tuple[int, ...],
|
| 1006 |
+
border: float | tuple[int, ...] | None = None,
|
| 1007 |
+
thresh: float = 0,
|
| 1008 |
+
) -> None:
|
| 1009 |
+
"""
|
| 1010 |
+
.. warning:: This method is experimental.
|
| 1011 |
+
|
| 1012 |
+
Fills a bounded region with a given color.
|
| 1013 |
+
|
| 1014 |
+
:param image: Target image.
|
| 1015 |
+
:param xy: Seed position (a 2-item coordinate tuple). See
|
| 1016 |
+
:ref:`coordinate-system`.
|
| 1017 |
+
:param value: Fill color.
|
| 1018 |
+
:param border: Optional border value. If given, the region consists of
|
| 1019 |
+
pixels with a color different from the border color. If not given,
|
| 1020 |
+
the region consists of pixels having the same color as the seed
|
| 1021 |
+
pixel.
|
| 1022 |
+
:param thresh: Optional threshold value which specifies a maximum
|
| 1023 |
+
tolerable difference of a pixel value from the 'background' in
|
| 1024 |
+
order for it to be replaced. Useful for filling regions of
|
| 1025 |
+
non-homogeneous, but similar, colors.
|
| 1026 |
+
"""
|
| 1027 |
+
# based on an implementation by Eric S. Raymond
|
| 1028 |
+
# amended by yo1995 @20180806
|
| 1029 |
+
pixel = image.load()
|
| 1030 |
+
assert pixel is not None
|
| 1031 |
+
x, y = xy
|
| 1032 |
+
try:
|
| 1033 |
+
background = pixel[x, y]
|
| 1034 |
+
if _color_diff(value, background) <= thresh:
|
| 1035 |
+
return # seed point already has fill color
|
| 1036 |
+
pixel[x, y] = value
|
| 1037 |
+
except (ValueError, IndexError):
|
| 1038 |
+
return # seed point outside image
|
| 1039 |
+
edge = {(x, y)}
|
| 1040 |
+
# use a set to keep record of current and previous edge pixels
|
| 1041 |
+
# to reduce memory consumption
|
| 1042 |
+
full_edge = set()
|
| 1043 |
+
while edge:
|
| 1044 |
+
new_edge = set()
|
| 1045 |
+
for x, y in edge: # 4 adjacent method
|
| 1046 |
+
for s, t in ((x + 1, y), (x - 1, y), (x, y + 1), (x, y - 1)):
|
| 1047 |
+
# If already processed, or if a coordinate is negative, skip
|
| 1048 |
+
if (s, t) in full_edge or s < 0 or t < 0:
|
| 1049 |
+
continue
|
| 1050 |
+
try:
|
| 1051 |
+
p = pixel[s, t]
|
| 1052 |
+
except (ValueError, IndexError):
|
| 1053 |
+
pass
|
| 1054 |
+
else:
|
| 1055 |
+
full_edge.add((s, t))
|
| 1056 |
+
if border is None:
|
| 1057 |
+
fill = _color_diff(p, background) <= thresh
|
| 1058 |
+
else:
|
| 1059 |
+
fill = p not in (value, border)
|
| 1060 |
+
if fill:
|
| 1061 |
+
pixel[s, t] = value
|
| 1062 |
+
new_edge.add((s, t))
|
| 1063 |
+
full_edge = edge # discard pixels processed
|
| 1064 |
+
edge = new_edge
|
| 1065 |
+
|
| 1066 |
+
|
| 1067 |
+
def _compute_regular_polygon_vertices(
|
| 1068 |
+
bounding_circle: Sequence[Sequence[float] | float], n_sides: int, rotation: float
|
| 1069 |
+
) -> list[tuple[float, float]]:
|
| 1070 |
+
"""
|
| 1071 |
+
Generate a list of vertices for a 2D regular polygon.
|
| 1072 |
+
|
| 1073 |
+
:param bounding_circle: The bounding circle is a sequence defined
|
| 1074 |
+
by a point and radius. The polygon is inscribed in this circle.
|
| 1075 |
+
(e.g. ``bounding_circle=(x, y, r)`` or ``((x, y), r)``)
|
| 1076 |
+
:param n_sides: Number of sides
|
| 1077 |
+
(e.g. ``n_sides=3`` for a triangle, ``6`` for a hexagon)
|
| 1078 |
+
:param rotation: Apply an arbitrary rotation to the polygon
|
| 1079 |
+
(e.g. ``rotation=90``, applies a 90 degree rotation)
|
| 1080 |
+
:return: List of regular polygon vertices
|
| 1081 |
+
(e.g. ``[(25, 50), (50, 50), (50, 25), (25, 25)]``)
|
| 1082 |
+
|
| 1083 |
+
How are the vertices computed?
|
| 1084 |
+
1. Compute the following variables
|
| 1085 |
+
- theta: Angle between the apothem & the nearest polygon vertex
|
| 1086 |
+
- side_length: Length of each polygon edge
|
| 1087 |
+
- centroid: Center of bounding circle (1st, 2nd elements of bounding_circle)
|
| 1088 |
+
- polygon_radius: Polygon radius (last element of bounding_circle)
|
| 1089 |
+
- angles: Location of each polygon vertex in polar grid
|
| 1090 |
+
(e.g. A square with 0 degree rotation => [225.0, 315.0, 45.0, 135.0])
|
| 1091 |
+
|
| 1092 |
+
2. For each angle in angles, get the polygon vertex at that angle
|
| 1093 |
+
The vertex is computed using the equation below.
|
| 1094 |
+
X= xcos(φ) + ysin(φ)
|
| 1095 |
+
Y= −xsin(φ) + ycos(φ)
|
| 1096 |
+
|
| 1097 |
+
Note:
|
| 1098 |
+
φ = angle in degrees
|
| 1099 |
+
x = 0
|
| 1100 |
+
y = polygon_radius
|
| 1101 |
+
|
| 1102 |
+
The formula above assumes rotation around the origin.
|
| 1103 |
+
In our case, we are rotating around the centroid.
|
| 1104 |
+
To account for this, we use the formula below
|
| 1105 |
+
X = xcos(φ) + ysin(φ) + centroid_x
|
| 1106 |
+
Y = −xsin(φ) + ycos(φ) + centroid_y
|
| 1107 |
+
"""
|
| 1108 |
+
# 1. Error Handling
|
| 1109 |
+
# 1.1 Check `n_sides` has an appropriate value
|
| 1110 |
+
if not isinstance(n_sides, int):
|
| 1111 |
+
msg = "n_sides should be an int" # type: ignore[unreachable]
|
| 1112 |
+
raise TypeError(msg)
|
| 1113 |
+
if n_sides < 3:
|
| 1114 |
+
msg = "n_sides should be an int > 2"
|
| 1115 |
+
raise ValueError(msg)
|
| 1116 |
+
|
| 1117 |
+
# 1.2 Check `bounding_circle` has an appropriate value
|
| 1118 |
+
if not isinstance(bounding_circle, (list, tuple)):
|
| 1119 |
+
msg = "bounding_circle should be a sequence"
|
| 1120 |
+
raise TypeError(msg)
|
| 1121 |
+
|
| 1122 |
+
if len(bounding_circle) == 3:
|
| 1123 |
+
if not all(isinstance(i, (int, float)) for i in bounding_circle):
|
| 1124 |
+
msg = "bounding_circle should only contain numeric data"
|
| 1125 |
+
raise ValueError(msg)
|
| 1126 |
+
|
| 1127 |
+
*centroid, polygon_radius = cast(List[float], list(bounding_circle))
|
| 1128 |
+
elif len(bounding_circle) == 2 and isinstance(bounding_circle[0], (list, tuple)):
|
| 1129 |
+
if not all(
|
| 1130 |
+
isinstance(i, (int, float)) for i in bounding_circle[0]
|
| 1131 |
+
) or not isinstance(bounding_circle[1], (int, float)):
|
| 1132 |
+
msg = "bounding_circle should only contain numeric data"
|
| 1133 |
+
raise ValueError(msg)
|
| 1134 |
+
|
| 1135 |
+
if len(bounding_circle[0]) != 2:
|
| 1136 |
+
msg = "bounding_circle centre should contain 2D coordinates (e.g. (x, y))"
|
| 1137 |
+
raise ValueError(msg)
|
| 1138 |
+
|
| 1139 |
+
centroid = cast(List[float], list(bounding_circle[0]))
|
| 1140 |
+
polygon_radius = cast(float, bounding_circle[1])
|
| 1141 |
+
else:
|
| 1142 |
+
msg = (
|
| 1143 |
+
"bounding_circle should contain 2D coordinates "
|
| 1144 |
+
"and a radius (e.g. (x, y, r) or ((x, y), r) )"
|
| 1145 |
+
)
|
| 1146 |
+
raise ValueError(msg)
|
| 1147 |
+
|
| 1148 |
+
if polygon_radius <= 0:
|
| 1149 |
+
msg = "bounding_circle radius should be > 0"
|
| 1150 |
+
raise ValueError(msg)
|
| 1151 |
+
|
| 1152 |
+
# 1.3 Check `rotation` has an appropriate value
|
| 1153 |
+
if not isinstance(rotation, (int, float)):
|
| 1154 |
+
msg = "rotation should be an int or float" # type: ignore[unreachable]
|
| 1155 |
+
raise ValueError(msg)
|
| 1156 |
+
|
| 1157 |
+
# 2. Define Helper Functions
|
| 1158 |
+
def _apply_rotation(point: list[float], degrees: float) -> tuple[float, float]:
|
| 1159 |
+
return (
|
| 1160 |
+
round(
|
| 1161 |
+
point[0] * math.cos(math.radians(360 - degrees))
|
| 1162 |
+
- point[1] * math.sin(math.radians(360 - degrees))
|
| 1163 |
+
+ centroid[0],
|
| 1164 |
+
2,
|
| 1165 |
+
),
|
| 1166 |
+
round(
|
| 1167 |
+
point[1] * math.cos(math.radians(360 - degrees))
|
| 1168 |
+
+ point[0] * math.sin(math.radians(360 - degrees))
|
| 1169 |
+
+ centroid[1],
|
| 1170 |
+
2,
|
| 1171 |
+
),
|
| 1172 |
+
)
|
| 1173 |
+
|
| 1174 |
+
def _compute_polygon_vertex(angle: float) -> tuple[float, float]:
|
| 1175 |
+
start_point = [polygon_radius, 0]
|
| 1176 |
+
return _apply_rotation(start_point, angle)
|
| 1177 |
+
|
| 1178 |
+
def _get_angles(n_sides: int, rotation: float) -> list[float]:
|
| 1179 |
+
angles = []
|
| 1180 |
+
degrees = 360 / n_sides
|
| 1181 |
+
# Start with the bottom left polygon vertex
|
| 1182 |
+
current_angle = (270 - 0.5 * degrees) + rotation
|
| 1183 |
+
for _ in range(0, n_sides):
|
| 1184 |
+
angles.append(current_angle)
|
| 1185 |
+
current_angle += degrees
|
| 1186 |
+
if current_angle > 360:
|
| 1187 |
+
current_angle -= 360
|
| 1188 |
+
return angles
|
| 1189 |
+
|
| 1190 |
+
# 3. Variable Declarations
|
| 1191 |
+
angles = _get_angles(n_sides, rotation)
|
| 1192 |
+
|
| 1193 |
+
# 4. Compute Vertices
|
| 1194 |
+
return [_compute_polygon_vertex(angle) for angle in angles]
|
| 1195 |
+
|
| 1196 |
+
|
| 1197 |
+
def _color_diff(
|
| 1198 |
+
color1: float | tuple[int, ...], color2: float | tuple[int, ...]
|
| 1199 |
+
) -> float:
|
| 1200 |
+
"""
|
| 1201 |
+
Uses 1-norm distance to calculate difference between two values.
|
| 1202 |
+
"""
|
| 1203 |
+
first = color1 if isinstance(color1, tuple) else (color1,)
|
| 1204 |
+
second = color2 if isinstance(color2, tuple) else (color2,)
|
| 1205 |
+
|
| 1206 |
+
return sum(abs(first[i] - second[i]) for i in range(0, len(second)))
|
.venv/lib/python3.11/site-packages/PIL/ImageDraw2.py
ADDED
|
@@ -0,0 +1,206 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# WCK-style drawing interface operations
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 2003-12-07 fl created
|
| 9 |
+
# 2005-05-15 fl updated; added to PIL as ImageDraw2
|
| 10 |
+
# 2005-05-15 fl added text support
|
| 11 |
+
# 2005-05-20 fl added arc/chord/pieslice support
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) 2003-2005 by Secret Labs AB
|
| 14 |
+
# Copyright (c) 2003-2005 by Fredrik Lundh
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
"""
|
| 21 |
+
(Experimental) WCK-style drawing interface operations
|
| 22 |
+
|
| 23 |
+
.. seealso:: :py:mod:`PIL.ImageDraw`
|
| 24 |
+
"""
|
| 25 |
+
from __future__ import annotations
|
| 26 |
+
|
| 27 |
+
from typing import BinaryIO
|
| 28 |
+
|
| 29 |
+
from . import Image, ImageColor, ImageDraw, ImageFont, ImagePath
|
| 30 |
+
from ._typing import StrOrBytesPath
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
class Pen:
|
| 34 |
+
"""Stores an outline color and width."""
|
| 35 |
+
|
| 36 |
+
def __init__(self, color: str, width: int = 1, opacity: int = 255) -> None:
|
| 37 |
+
self.color = ImageColor.getrgb(color)
|
| 38 |
+
self.width = width
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
class Brush:
|
| 42 |
+
"""Stores a fill color"""
|
| 43 |
+
|
| 44 |
+
def __init__(self, color: str, opacity: int = 255) -> None:
|
| 45 |
+
self.color = ImageColor.getrgb(color)
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
class Font:
|
| 49 |
+
"""Stores a TrueType font and color"""
|
| 50 |
+
|
| 51 |
+
def __init__(
|
| 52 |
+
self, color: str, file: StrOrBytesPath | BinaryIO, size: float = 12
|
| 53 |
+
) -> None:
|
| 54 |
+
# FIXME: add support for bitmap fonts
|
| 55 |
+
self.color = ImageColor.getrgb(color)
|
| 56 |
+
self.font = ImageFont.truetype(file, size)
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
class Draw:
|
| 60 |
+
"""
|
| 61 |
+
(Experimental) WCK-style drawing interface
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
def __init__(
|
| 65 |
+
self,
|
| 66 |
+
image: Image.Image | str,
|
| 67 |
+
size: tuple[int, int] | list[int] | None = None,
|
| 68 |
+
color: float | tuple[float, ...] | str | None = None,
|
| 69 |
+
) -> None:
|
| 70 |
+
if isinstance(image, str):
|
| 71 |
+
if size is None:
|
| 72 |
+
msg = "If image argument is mode string, size must be a list or tuple"
|
| 73 |
+
raise ValueError(msg)
|
| 74 |
+
image = Image.new(image, size, color)
|
| 75 |
+
self.draw = ImageDraw.Draw(image)
|
| 76 |
+
self.image = image
|
| 77 |
+
self.transform = None
|
| 78 |
+
|
| 79 |
+
def flush(self) -> Image.Image:
|
| 80 |
+
return self.image
|
| 81 |
+
|
| 82 |
+
def render(self, op, xy, pen, brush=None):
|
| 83 |
+
# handle color arguments
|
| 84 |
+
outline = fill = None
|
| 85 |
+
width = 1
|
| 86 |
+
if isinstance(pen, Pen):
|
| 87 |
+
outline = pen.color
|
| 88 |
+
width = pen.width
|
| 89 |
+
elif isinstance(brush, Pen):
|
| 90 |
+
outline = brush.color
|
| 91 |
+
width = brush.width
|
| 92 |
+
if isinstance(brush, Brush):
|
| 93 |
+
fill = brush.color
|
| 94 |
+
elif isinstance(pen, Brush):
|
| 95 |
+
fill = pen.color
|
| 96 |
+
# handle transformation
|
| 97 |
+
if self.transform:
|
| 98 |
+
xy = ImagePath.Path(xy)
|
| 99 |
+
xy.transform(self.transform)
|
| 100 |
+
# render the item
|
| 101 |
+
if op == "line":
|
| 102 |
+
self.draw.line(xy, fill=outline, width=width)
|
| 103 |
+
else:
|
| 104 |
+
getattr(self.draw, op)(xy, fill=fill, outline=outline)
|
| 105 |
+
|
| 106 |
+
def settransform(self, offset):
|
| 107 |
+
"""Sets a transformation offset."""
|
| 108 |
+
(xoffset, yoffset) = offset
|
| 109 |
+
self.transform = (1, 0, xoffset, 0, 1, yoffset)
|
| 110 |
+
|
| 111 |
+
def arc(self, xy, start, end, *options):
|
| 112 |
+
"""
|
| 113 |
+
Draws an arc (a portion of a circle outline) between the start and end
|
| 114 |
+
angles, inside the given bounding box.
|
| 115 |
+
|
| 116 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.arc`
|
| 117 |
+
"""
|
| 118 |
+
self.render("arc", xy, start, end, *options)
|
| 119 |
+
|
| 120 |
+
def chord(self, xy, start, end, *options):
|
| 121 |
+
"""
|
| 122 |
+
Same as :py:meth:`~PIL.ImageDraw2.Draw.arc`, but connects the end points
|
| 123 |
+
with a straight line.
|
| 124 |
+
|
| 125 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.chord`
|
| 126 |
+
"""
|
| 127 |
+
self.render("chord", xy, start, end, *options)
|
| 128 |
+
|
| 129 |
+
def ellipse(self, xy, *options):
|
| 130 |
+
"""
|
| 131 |
+
Draws an ellipse inside the given bounding box.
|
| 132 |
+
|
| 133 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.ellipse`
|
| 134 |
+
"""
|
| 135 |
+
self.render("ellipse", xy, *options)
|
| 136 |
+
|
| 137 |
+
def line(self, xy, *options):
|
| 138 |
+
"""
|
| 139 |
+
Draws a line between the coordinates in the ``xy`` list.
|
| 140 |
+
|
| 141 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.line`
|
| 142 |
+
"""
|
| 143 |
+
self.render("line", xy, *options)
|
| 144 |
+
|
| 145 |
+
def pieslice(self, xy, start, end, *options):
|
| 146 |
+
"""
|
| 147 |
+
Same as arc, but also draws straight lines between the end points and the
|
| 148 |
+
center of the bounding box.
|
| 149 |
+
|
| 150 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.pieslice`
|
| 151 |
+
"""
|
| 152 |
+
self.render("pieslice", xy, start, end, *options)
|
| 153 |
+
|
| 154 |
+
def polygon(self, xy, *options):
|
| 155 |
+
"""
|
| 156 |
+
Draws a polygon.
|
| 157 |
+
|
| 158 |
+
The polygon outline consists of straight lines between the given
|
| 159 |
+
coordinates, plus a straight line between the last and the first
|
| 160 |
+
coordinate.
|
| 161 |
+
|
| 162 |
+
|
| 163 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.polygon`
|
| 164 |
+
"""
|
| 165 |
+
self.render("polygon", xy, *options)
|
| 166 |
+
|
| 167 |
+
def rectangle(self, xy, *options):
|
| 168 |
+
"""
|
| 169 |
+
Draws a rectangle.
|
| 170 |
+
|
| 171 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.rectangle`
|
| 172 |
+
"""
|
| 173 |
+
self.render("rectangle", xy, *options)
|
| 174 |
+
|
| 175 |
+
def text(self, xy, text, font):
|
| 176 |
+
"""
|
| 177 |
+
Draws the string at the given position.
|
| 178 |
+
|
| 179 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.text`
|
| 180 |
+
"""
|
| 181 |
+
if self.transform:
|
| 182 |
+
xy = ImagePath.Path(xy)
|
| 183 |
+
xy.transform(self.transform)
|
| 184 |
+
self.draw.text(xy, text, font=font.font, fill=font.color)
|
| 185 |
+
|
| 186 |
+
def textbbox(self, xy, text, font):
|
| 187 |
+
"""
|
| 188 |
+
Returns bounding box (in pixels) of given text.
|
| 189 |
+
|
| 190 |
+
:return: ``(left, top, right, bottom)`` bounding box
|
| 191 |
+
|
| 192 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textbbox`
|
| 193 |
+
"""
|
| 194 |
+
if self.transform:
|
| 195 |
+
xy = ImagePath.Path(xy)
|
| 196 |
+
xy.transform(self.transform)
|
| 197 |
+
return self.draw.textbbox(xy, text, font=font.font)
|
| 198 |
+
|
| 199 |
+
def textlength(self, text, font):
|
| 200 |
+
"""
|
| 201 |
+
Returns length (in pixels) of given text.
|
| 202 |
+
This is the amount by which following text should be offset.
|
| 203 |
+
|
| 204 |
+
.. seealso:: :py:meth:`PIL.ImageDraw.ImageDraw.textlength`
|
| 205 |
+
"""
|
| 206 |
+
return self.draw.textlength(text, font=font.font)
|
.venv/lib/python3.11/site-packages/PIL/ImageGrab.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# screen grabber
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 2001-04-26 fl created
|
| 9 |
+
# 2001-09-17 fl use builtin driver, if present
|
| 10 |
+
# 2002-11-19 fl added grabclipboard support
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) 2001-2002 by Secret Labs AB
|
| 13 |
+
# Copyright (c) 2001-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 io
|
| 20 |
+
import os
|
| 21 |
+
import shutil
|
| 22 |
+
import subprocess
|
| 23 |
+
import sys
|
| 24 |
+
import tempfile
|
| 25 |
+
|
| 26 |
+
from . import Image
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def grab(
|
| 30 |
+
bbox: tuple[int, int, int, int] | None = None,
|
| 31 |
+
include_layered_windows: bool = False,
|
| 32 |
+
all_screens: bool = False,
|
| 33 |
+
xdisplay: str | None = None,
|
| 34 |
+
) -> Image.Image:
|
| 35 |
+
im: Image.Image
|
| 36 |
+
if xdisplay is None:
|
| 37 |
+
if sys.platform == "darwin":
|
| 38 |
+
fh, filepath = tempfile.mkstemp(".png")
|
| 39 |
+
os.close(fh)
|
| 40 |
+
args = ["screencapture"]
|
| 41 |
+
if bbox:
|
| 42 |
+
left, top, right, bottom = bbox
|
| 43 |
+
args += ["-R", f"{left},{top},{right-left},{bottom-top}"]
|
| 44 |
+
subprocess.call(args + ["-x", filepath])
|
| 45 |
+
im = Image.open(filepath)
|
| 46 |
+
im.load()
|
| 47 |
+
os.unlink(filepath)
|
| 48 |
+
if bbox:
|
| 49 |
+
im_resized = im.resize((right - left, bottom - top))
|
| 50 |
+
im.close()
|
| 51 |
+
return im_resized
|
| 52 |
+
return im
|
| 53 |
+
elif sys.platform == "win32":
|
| 54 |
+
offset, size, data = Image.core.grabscreen_win32(
|
| 55 |
+
include_layered_windows, all_screens
|
| 56 |
+
)
|
| 57 |
+
im = Image.frombytes(
|
| 58 |
+
"RGB",
|
| 59 |
+
size,
|
| 60 |
+
data,
|
| 61 |
+
# RGB, 32-bit line padding, origin lower left corner
|
| 62 |
+
"raw",
|
| 63 |
+
"BGR",
|
| 64 |
+
(size[0] * 3 + 3) & -4,
|
| 65 |
+
-1,
|
| 66 |
+
)
|
| 67 |
+
if bbox:
|
| 68 |
+
x0, y0 = offset
|
| 69 |
+
left, top, right, bottom = bbox
|
| 70 |
+
im = im.crop((left - x0, top - y0, right - x0, bottom - y0))
|
| 71 |
+
return im
|
| 72 |
+
# Cast to Optional[str] needed for Windows and macOS.
|
| 73 |
+
display_name: str | None = xdisplay
|
| 74 |
+
try:
|
| 75 |
+
if not Image.core.HAVE_XCB:
|
| 76 |
+
msg = "Pillow was built without XCB support"
|
| 77 |
+
raise OSError(msg)
|
| 78 |
+
size, data = Image.core.grabscreen_x11(display_name)
|
| 79 |
+
except OSError:
|
| 80 |
+
if (
|
| 81 |
+
display_name is None
|
| 82 |
+
and sys.platform not in ("darwin", "win32")
|
| 83 |
+
and shutil.which("gnome-screenshot")
|
| 84 |
+
):
|
| 85 |
+
fh, filepath = tempfile.mkstemp(".png")
|
| 86 |
+
os.close(fh)
|
| 87 |
+
subprocess.call(["gnome-screenshot", "-f", filepath])
|
| 88 |
+
im = Image.open(filepath)
|
| 89 |
+
im.load()
|
| 90 |
+
os.unlink(filepath)
|
| 91 |
+
if bbox:
|
| 92 |
+
im_cropped = im.crop(bbox)
|
| 93 |
+
im.close()
|
| 94 |
+
return im_cropped
|
| 95 |
+
return im
|
| 96 |
+
else:
|
| 97 |
+
raise
|
| 98 |
+
else:
|
| 99 |
+
im = Image.frombytes("RGB", size, data, "raw", "BGRX", size[0] * 4, 1)
|
| 100 |
+
if bbox:
|
| 101 |
+
im = im.crop(bbox)
|
| 102 |
+
return im
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
def grabclipboard() -> Image.Image | list[str] | None:
|
| 106 |
+
if sys.platform == "darwin":
|
| 107 |
+
fh, filepath = tempfile.mkstemp(".png")
|
| 108 |
+
os.close(fh)
|
| 109 |
+
commands = [
|
| 110 |
+
'set theFile to (open for access POSIX file "'
|
| 111 |
+
+ filepath
|
| 112 |
+
+ '" with write permission)',
|
| 113 |
+
"try",
|
| 114 |
+
" write (the clipboard as «class PNGf») to theFile",
|
| 115 |
+
"end try",
|
| 116 |
+
"close access theFile",
|
| 117 |
+
]
|
| 118 |
+
script = ["osascript"]
|
| 119 |
+
for command in commands:
|
| 120 |
+
script += ["-e", command]
|
| 121 |
+
subprocess.call(script)
|
| 122 |
+
|
| 123 |
+
im = None
|
| 124 |
+
if os.stat(filepath).st_size != 0:
|
| 125 |
+
im = Image.open(filepath)
|
| 126 |
+
im.load()
|
| 127 |
+
os.unlink(filepath)
|
| 128 |
+
return im
|
| 129 |
+
elif sys.platform == "win32":
|
| 130 |
+
fmt, data = Image.core.grabclipboard_win32()
|
| 131 |
+
if fmt == "file": # CF_HDROP
|
| 132 |
+
import struct
|
| 133 |
+
|
| 134 |
+
o = struct.unpack_from("I", data)[0]
|
| 135 |
+
if data[16] != 0:
|
| 136 |
+
files = data[o:].decode("utf-16le").split("\0")
|
| 137 |
+
else:
|
| 138 |
+
files = data[o:].decode("mbcs").split("\0")
|
| 139 |
+
return files[: files.index("")]
|
| 140 |
+
if isinstance(data, bytes):
|
| 141 |
+
data = io.BytesIO(data)
|
| 142 |
+
if fmt == "png":
|
| 143 |
+
from . import PngImagePlugin
|
| 144 |
+
|
| 145 |
+
return PngImagePlugin.PngImageFile(data)
|
| 146 |
+
elif fmt == "DIB":
|
| 147 |
+
from . import BmpImagePlugin
|
| 148 |
+
|
| 149 |
+
return BmpImagePlugin.DibImageFile(data)
|
| 150 |
+
return None
|
| 151 |
+
else:
|
| 152 |
+
if os.getenv("WAYLAND_DISPLAY"):
|
| 153 |
+
session_type = "wayland"
|
| 154 |
+
elif os.getenv("DISPLAY"):
|
| 155 |
+
session_type = "x11"
|
| 156 |
+
else: # Session type check failed
|
| 157 |
+
session_type = None
|
| 158 |
+
|
| 159 |
+
if shutil.which("wl-paste") and session_type in ("wayland", None):
|
| 160 |
+
args = ["wl-paste", "-t", "image"]
|
| 161 |
+
elif shutil.which("xclip") and session_type in ("x11", None):
|
| 162 |
+
args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"]
|
| 163 |
+
else:
|
| 164 |
+
msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
|
| 165 |
+
raise NotImplementedError(msg)
|
| 166 |
+
|
| 167 |
+
p = subprocess.run(args, capture_output=True)
|
| 168 |
+
if p.returncode != 0:
|
| 169 |
+
err = p.stderr
|
| 170 |
+
for silent_error in [
|
| 171 |
+
# wl-paste, when the clipboard is empty
|
| 172 |
+
b"Nothing is copied",
|
| 173 |
+
# Ubuntu/Debian wl-paste, when the clipboard is empty
|
| 174 |
+
b"No selection",
|
| 175 |
+
# Ubuntu/Debian wl-paste, when an image isn't available
|
| 176 |
+
b"No suitable type of content copied",
|
| 177 |
+
# wl-paste or Ubuntu/Debian xclip, when an image isn't available
|
| 178 |
+
b" not available",
|
| 179 |
+
# xclip, when an image isn't available
|
| 180 |
+
b"cannot convert ",
|
| 181 |
+
# xclip, when the clipboard isn't initialized
|
| 182 |
+
b"xclip: Error: There is no owner for the ",
|
| 183 |
+
]:
|
| 184 |
+
if silent_error in err:
|
| 185 |
+
return None
|
| 186 |
+
msg = f"{args[0]} error"
|
| 187 |
+
if err:
|
| 188 |
+
msg += f": {err.strip().decode()}"
|
| 189 |
+
raise ChildProcessError(msg)
|
| 190 |
+
|
| 191 |
+
data = io.BytesIO(p.stdout)
|
| 192 |
+
im = Image.open(data)
|
| 193 |
+
im.load()
|
| 194 |
+
return im
|
.venv/lib/python3.11/site-packages/PIL/ImageMath.py
ADDED
|
@@ -0,0 +1,357 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# a simple math add-on for the Python Imaging Library
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1999-02-15 fl Original PIL Plus release
|
| 9 |
+
# 2005-05-05 fl Simplified and cleaned up for PIL 1.1.6
|
| 10 |
+
# 2005-09-12 fl Fixed int() and float() for Python 2.4.1
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) 1999-2005 by Secret Labs AB
|
| 13 |
+
# Copyright (c) 2005 by Fredrik Lundh
|
| 14 |
+
#
|
| 15 |
+
# See the README file for information on usage and redistribution.
|
| 16 |
+
#
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import builtins
|
| 20 |
+
from types import CodeType
|
| 21 |
+
from typing import Any, Callable
|
| 22 |
+
|
| 23 |
+
from . import Image, _imagingmath
|
| 24 |
+
from ._deprecate import deprecate
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
class _Operand:
|
| 28 |
+
"""Wraps an image operand, providing standard operators"""
|
| 29 |
+
|
| 30 |
+
def __init__(self, im: Image.Image):
|
| 31 |
+
self.im = im
|
| 32 |
+
|
| 33 |
+
def __fixup(self, im1: _Operand | float) -> Image.Image:
|
| 34 |
+
# convert image to suitable mode
|
| 35 |
+
if isinstance(im1, _Operand):
|
| 36 |
+
# argument was an image.
|
| 37 |
+
if im1.im.mode in ("1", "L"):
|
| 38 |
+
return im1.im.convert("I")
|
| 39 |
+
elif im1.im.mode in ("I", "F"):
|
| 40 |
+
return im1.im
|
| 41 |
+
else:
|
| 42 |
+
msg = f"unsupported mode: {im1.im.mode}"
|
| 43 |
+
raise ValueError(msg)
|
| 44 |
+
else:
|
| 45 |
+
# argument was a constant
|
| 46 |
+
if isinstance(im1, (int, float)) and self.im.mode in ("1", "L", "I"):
|
| 47 |
+
return Image.new("I", self.im.size, im1)
|
| 48 |
+
else:
|
| 49 |
+
return Image.new("F", self.im.size, im1)
|
| 50 |
+
|
| 51 |
+
def apply(
|
| 52 |
+
self,
|
| 53 |
+
op: str,
|
| 54 |
+
im1: _Operand | float,
|
| 55 |
+
im2: _Operand | float | None = None,
|
| 56 |
+
mode: str | None = None,
|
| 57 |
+
) -> _Operand:
|
| 58 |
+
im_1 = self.__fixup(im1)
|
| 59 |
+
if im2 is None:
|
| 60 |
+
# unary operation
|
| 61 |
+
out = Image.new(mode or im_1.mode, im_1.size, None)
|
| 62 |
+
im_1.load()
|
| 63 |
+
try:
|
| 64 |
+
op = getattr(_imagingmath, f"{op}_{im_1.mode}")
|
| 65 |
+
except AttributeError as e:
|
| 66 |
+
msg = f"bad operand type for '{op}'"
|
| 67 |
+
raise TypeError(msg) from e
|
| 68 |
+
_imagingmath.unop(op, out.im.id, im_1.im.id)
|
| 69 |
+
else:
|
| 70 |
+
# binary operation
|
| 71 |
+
im_2 = self.__fixup(im2)
|
| 72 |
+
if im_1.mode != im_2.mode:
|
| 73 |
+
# convert both arguments to floating point
|
| 74 |
+
if im_1.mode != "F":
|
| 75 |
+
im_1 = im_1.convert("F")
|
| 76 |
+
if im_2.mode != "F":
|
| 77 |
+
im_2 = im_2.convert("F")
|
| 78 |
+
if im_1.size != im_2.size:
|
| 79 |
+
# crop both arguments to a common size
|
| 80 |
+
size = (
|
| 81 |
+
min(im_1.size[0], im_2.size[0]),
|
| 82 |
+
min(im_1.size[1], im_2.size[1]),
|
| 83 |
+
)
|
| 84 |
+
if im_1.size != size:
|
| 85 |
+
im_1 = im_1.crop((0, 0) + size)
|
| 86 |
+
if im_2.size != size:
|
| 87 |
+
im_2 = im_2.crop((0, 0) + size)
|
| 88 |
+
out = Image.new(mode or im_1.mode, im_1.size, None)
|
| 89 |
+
im_1.load()
|
| 90 |
+
im_2.load()
|
| 91 |
+
try:
|
| 92 |
+
op = getattr(_imagingmath, f"{op}_{im_1.mode}")
|
| 93 |
+
except AttributeError as e:
|
| 94 |
+
msg = f"bad operand type for '{op}'"
|
| 95 |
+
raise TypeError(msg) from e
|
| 96 |
+
_imagingmath.binop(op, out.im.id, im_1.im.id, im_2.im.id)
|
| 97 |
+
return _Operand(out)
|
| 98 |
+
|
| 99 |
+
# unary operators
|
| 100 |
+
def __bool__(self) -> bool:
|
| 101 |
+
# an image is "true" if it contains at least one non-zero pixel
|
| 102 |
+
return self.im.getbbox() is not None
|
| 103 |
+
|
| 104 |
+
def __abs__(self) -> _Operand:
|
| 105 |
+
return self.apply("abs", self)
|
| 106 |
+
|
| 107 |
+
def __pos__(self) -> _Operand:
|
| 108 |
+
return self
|
| 109 |
+
|
| 110 |
+
def __neg__(self) -> _Operand:
|
| 111 |
+
return self.apply("neg", self)
|
| 112 |
+
|
| 113 |
+
# binary operators
|
| 114 |
+
def __add__(self, other: _Operand | float) -> _Operand:
|
| 115 |
+
return self.apply("add", self, other)
|
| 116 |
+
|
| 117 |
+
def __radd__(self, other: _Operand | float) -> _Operand:
|
| 118 |
+
return self.apply("add", other, self)
|
| 119 |
+
|
| 120 |
+
def __sub__(self, other: _Operand | float) -> _Operand:
|
| 121 |
+
return self.apply("sub", self, other)
|
| 122 |
+
|
| 123 |
+
def __rsub__(self, other: _Operand | float) -> _Operand:
|
| 124 |
+
return self.apply("sub", other, self)
|
| 125 |
+
|
| 126 |
+
def __mul__(self, other: _Operand | float) -> _Operand:
|
| 127 |
+
return self.apply("mul", self, other)
|
| 128 |
+
|
| 129 |
+
def __rmul__(self, other: _Operand | float) -> _Operand:
|
| 130 |
+
return self.apply("mul", other, self)
|
| 131 |
+
|
| 132 |
+
def __truediv__(self, other: _Operand | float) -> _Operand:
|
| 133 |
+
return self.apply("div", self, other)
|
| 134 |
+
|
| 135 |
+
def __rtruediv__(self, other: _Operand | float) -> _Operand:
|
| 136 |
+
return self.apply("div", other, self)
|
| 137 |
+
|
| 138 |
+
def __mod__(self, other: _Operand | float) -> _Operand:
|
| 139 |
+
return self.apply("mod", self, other)
|
| 140 |
+
|
| 141 |
+
def __rmod__(self, other: _Operand | float) -> _Operand:
|
| 142 |
+
return self.apply("mod", other, self)
|
| 143 |
+
|
| 144 |
+
def __pow__(self, other: _Operand | float) -> _Operand:
|
| 145 |
+
return self.apply("pow", self, other)
|
| 146 |
+
|
| 147 |
+
def __rpow__(self, other: _Operand | float) -> _Operand:
|
| 148 |
+
return self.apply("pow", other, self)
|
| 149 |
+
|
| 150 |
+
# bitwise
|
| 151 |
+
def __invert__(self) -> _Operand:
|
| 152 |
+
return self.apply("invert", self)
|
| 153 |
+
|
| 154 |
+
def __and__(self, other: _Operand | float) -> _Operand:
|
| 155 |
+
return self.apply("and", self, other)
|
| 156 |
+
|
| 157 |
+
def __rand__(self, other: _Operand | float) -> _Operand:
|
| 158 |
+
return self.apply("and", other, self)
|
| 159 |
+
|
| 160 |
+
def __or__(self, other: _Operand | float) -> _Operand:
|
| 161 |
+
return self.apply("or", self, other)
|
| 162 |
+
|
| 163 |
+
def __ror__(self, other: _Operand | float) -> _Operand:
|
| 164 |
+
return self.apply("or", other, self)
|
| 165 |
+
|
| 166 |
+
def __xor__(self, other: _Operand | float) -> _Operand:
|
| 167 |
+
return self.apply("xor", self, other)
|
| 168 |
+
|
| 169 |
+
def __rxor__(self, other: _Operand | float) -> _Operand:
|
| 170 |
+
return self.apply("xor", other, self)
|
| 171 |
+
|
| 172 |
+
def __lshift__(self, other: _Operand | float) -> _Operand:
|
| 173 |
+
return self.apply("lshift", self, other)
|
| 174 |
+
|
| 175 |
+
def __rshift__(self, other: _Operand | float) -> _Operand:
|
| 176 |
+
return self.apply("rshift", self, other)
|
| 177 |
+
|
| 178 |
+
# logical
|
| 179 |
+
def __eq__(self, other):
|
| 180 |
+
return self.apply("eq", self, other)
|
| 181 |
+
|
| 182 |
+
def __ne__(self, other):
|
| 183 |
+
return self.apply("ne", self, other)
|
| 184 |
+
|
| 185 |
+
def __lt__(self, other: _Operand | float) -> _Operand:
|
| 186 |
+
return self.apply("lt", self, other)
|
| 187 |
+
|
| 188 |
+
def __le__(self, other: _Operand | float) -> _Operand:
|
| 189 |
+
return self.apply("le", self, other)
|
| 190 |
+
|
| 191 |
+
def __gt__(self, other: _Operand | float) -> _Operand:
|
| 192 |
+
return self.apply("gt", self, other)
|
| 193 |
+
|
| 194 |
+
def __ge__(self, other: _Operand | float) -> _Operand:
|
| 195 |
+
return self.apply("ge", self, other)
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
# conversions
|
| 199 |
+
def imagemath_int(self: _Operand) -> _Operand:
|
| 200 |
+
return _Operand(self.im.convert("I"))
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def imagemath_float(self: _Operand) -> _Operand:
|
| 204 |
+
return _Operand(self.im.convert("F"))
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
# logical
|
| 208 |
+
def imagemath_equal(self: _Operand, other: _Operand | float | None) -> _Operand:
|
| 209 |
+
return self.apply("eq", self, other, mode="I")
|
| 210 |
+
|
| 211 |
+
|
| 212 |
+
def imagemath_notequal(self: _Operand, other: _Operand | float | None) -> _Operand:
|
| 213 |
+
return self.apply("ne", self, other, mode="I")
|
| 214 |
+
|
| 215 |
+
|
| 216 |
+
def imagemath_min(self: _Operand, other: _Operand | float | None) -> _Operand:
|
| 217 |
+
return self.apply("min", self, other)
|
| 218 |
+
|
| 219 |
+
|
| 220 |
+
def imagemath_max(self: _Operand, other: _Operand | float | None) -> _Operand:
|
| 221 |
+
return self.apply("max", self, other)
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def imagemath_convert(self: _Operand, mode: str) -> _Operand:
|
| 225 |
+
return _Operand(self.im.convert(mode))
|
| 226 |
+
|
| 227 |
+
|
| 228 |
+
ops = {
|
| 229 |
+
"int": imagemath_int,
|
| 230 |
+
"float": imagemath_float,
|
| 231 |
+
"equal": imagemath_equal,
|
| 232 |
+
"notequal": imagemath_notequal,
|
| 233 |
+
"min": imagemath_min,
|
| 234 |
+
"max": imagemath_max,
|
| 235 |
+
"convert": imagemath_convert,
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
|
| 239 |
+
def lambda_eval(
|
| 240 |
+
expression: Callable[[dict[str, Any]], Any],
|
| 241 |
+
options: dict[str, Any] = {},
|
| 242 |
+
**kw: Any,
|
| 243 |
+
) -> Any:
|
| 244 |
+
"""
|
| 245 |
+
Returns the result of an image function.
|
| 246 |
+
|
| 247 |
+
:py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
|
| 248 |
+
images, use the :py:meth:`~PIL.Image.Image.split` method or
|
| 249 |
+
:py:func:`~PIL.Image.merge` function.
|
| 250 |
+
|
| 251 |
+
:param expression: A function that receives a dictionary.
|
| 252 |
+
:param options: Values to add to the function's dictionary. You
|
| 253 |
+
can either use a dictionary, or one or more keyword
|
| 254 |
+
arguments.
|
| 255 |
+
:return: The expression result. This is usually an image object, but can
|
| 256 |
+
also be an integer, a floating point value, or a pixel tuple,
|
| 257 |
+
depending on the expression.
|
| 258 |
+
"""
|
| 259 |
+
|
| 260 |
+
args: dict[str, Any] = ops.copy()
|
| 261 |
+
args.update(options)
|
| 262 |
+
args.update(kw)
|
| 263 |
+
for k, v in args.items():
|
| 264 |
+
if hasattr(v, "im"):
|
| 265 |
+
args[k] = _Operand(v)
|
| 266 |
+
|
| 267 |
+
out = expression(args)
|
| 268 |
+
try:
|
| 269 |
+
return out.im
|
| 270 |
+
except AttributeError:
|
| 271 |
+
return out
|
| 272 |
+
|
| 273 |
+
|
| 274 |
+
def unsafe_eval(
|
| 275 |
+
expression: str,
|
| 276 |
+
options: dict[str, Any] = {},
|
| 277 |
+
**kw: Any,
|
| 278 |
+
) -> Any:
|
| 279 |
+
"""
|
| 280 |
+
Evaluates an image expression. This uses Python's ``eval()`` function to process
|
| 281 |
+
the expression string, and carries the security risks of doing so. It is not
|
| 282 |
+
recommended to process expressions without considering this.
|
| 283 |
+
:py:meth:`~lambda_eval` is a more secure alternative.
|
| 284 |
+
|
| 285 |
+
:py:mod:`~PIL.ImageMath` only supports single-layer images. To process multi-band
|
| 286 |
+
images, use the :py:meth:`~PIL.Image.Image.split` method or
|
| 287 |
+
:py:func:`~PIL.Image.merge` function.
|
| 288 |
+
|
| 289 |
+
:param expression: A string containing a Python-style expression.
|
| 290 |
+
:param options: Values to add to the evaluation context. You
|
| 291 |
+
can either use a dictionary, or one or more keyword
|
| 292 |
+
arguments.
|
| 293 |
+
:return: The evaluated expression. This is usually an image object, but can
|
| 294 |
+
also be an integer, a floating point value, or a pixel tuple,
|
| 295 |
+
depending on the expression.
|
| 296 |
+
"""
|
| 297 |
+
|
| 298 |
+
# build execution namespace
|
| 299 |
+
args: dict[str, Any] = ops.copy()
|
| 300 |
+
for k in list(options.keys()) + list(kw.keys()):
|
| 301 |
+
if "__" in k or hasattr(builtins, k):
|
| 302 |
+
msg = f"'{k}' not allowed"
|
| 303 |
+
raise ValueError(msg)
|
| 304 |
+
|
| 305 |
+
args.update(options)
|
| 306 |
+
args.update(kw)
|
| 307 |
+
for k, v in args.items():
|
| 308 |
+
if hasattr(v, "im"):
|
| 309 |
+
args[k] = _Operand(v)
|
| 310 |
+
|
| 311 |
+
compiled_code = compile(expression, "<string>", "eval")
|
| 312 |
+
|
| 313 |
+
def scan(code: CodeType) -> None:
|
| 314 |
+
for const in code.co_consts:
|
| 315 |
+
if type(const) is type(compiled_code):
|
| 316 |
+
scan(const)
|
| 317 |
+
|
| 318 |
+
for name in code.co_names:
|
| 319 |
+
if name not in args and name != "abs":
|
| 320 |
+
msg = f"'{name}' not allowed"
|
| 321 |
+
raise ValueError(msg)
|
| 322 |
+
|
| 323 |
+
scan(compiled_code)
|
| 324 |
+
out = builtins.eval(expression, {"__builtins": {"abs": abs}}, args)
|
| 325 |
+
try:
|
| 326 |
+
return out.im
|
| 327 |
+
except AttributeError:
|
| 328 |
+
return out
|
| 329 |
+
|
| 330 |
+
|
| 331 |
+
def eval(
|
| 332 |
+
expression: str,
|
| 333 |
+
_dict: dict[str, Any] = {},
|
| 334 |
+
**kw: Any,
|
| 335 |
+
) -> Any:
|
| 336 |
+
"""
|
| 337 |
+
Evaluates an image expression.
|
| 338 |
+
|
| 339 |
+
Deprecated. Use lambda_eval() or unsafe_eval() instead.
|
| 340 |
+
|
| 341 |
+
:param expression: A string containing a Python-style expression.
|
| 342 |
+
:param _dict: Values to add to the evaluation context. You
|
| 343 |
+
can either use a dictionary, or one or more keyword
|
| 344 |
+
arguments.
|
| 345 |
+
:return: The evaluated expression. This is usually an image object, but can
|
| 346 |
+
also be an integer, a floating point value, or a pixel tuple,
|
| 347 |
+
depending on the expression.
|
| 348 |
+
|
| 349 |
+
.. deprecated:: 10.3.0
|
| 350 |
+
"""
|
| 351 |
+
|
| 352 |
+
deprecate(
|
| 353 |
+
"ImageMath.eval",
|
| 354 |
+
12,
|
| 355 |
+
"ImageMath.lambda_eval or ImageMath.unsafe_eval",
|
| 356 |
+
)
|
| 357 |
+
return unsafe_eval(expression, _dict, **kw)
|
.venv/lib/python3.11/site-packages/PIL/ImageMorph.py
ADDED
|
@@ -0,0 +1,265 @@
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# A binary morphology add-on for the Python Imaging Library
|
| 2 |
+
#
|
| 3 |
+
# History:
|
| 4 |
+
# 2014-06-04 Initial version.
|
| 5 |
+
#
|
| 6 |
+
# Copyright (c) 2014 Dov Grobgeld <dov.grobgeld@gmail.com>
|
| 7 |
+
from __future__ import annotations
|
| 8 |
+
|
| 9 |
+
import re
|
| 10 |
+
|
| 11 |
+
from . import Image, _imagingmorph
|
| 12 |
+
|
| 13 |
+
LUT_SIZE = 1 << 9
|
| 14 |
+
|
| 15 |
+
# fmt: off
|
| 16 |
+
ROTATION_MATRIX = [
|
| 17 |
+
6, 3, 0,
|
| 18 |
+
7, 4, 1,
|
| 19 |
+
8, 5, 2,
|
| 20 |
+
]
|
| 21 |
+
MIRROR_MATRIX = [
|
| 22 |
+
2, 1, 0,
|
| 23 |
+
5, 4, 3,
|
| 24 |
+
8, 7, 6,
|
| 25 |
+
]
|
| 26 |
+
# fmt: on
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class LutBuilder:
|
| 30 |
+
"""A class for building a MorphLut from a descriptive language
|
| 31 |
+
|
| 32 |
+
The input patterns is a list of a strings sequences like these::
|
| 33 |
+
|
| 34 |
+
4:(...
|
| 35 |
+
.1.
|
| 36 |
+
111)->1
|
| 37 |
+
|
| 38 |
+
(whitespaces including linebreaks are ignored). The option 4
|
| 39 |
+
describes a series of symmetry operations (in this case a
|
| 40 |
+
4-rotation), the pattern is described by:
|
| 41 |
+
|
| 42 |
+
- . or X - Ignore
|
| 43 |
+
- 1 - Pixel is on
|
| 44 |
+
- 0 - Pixel is off
|
| 45 |
+
|
| 46 |
+
The result of the operation is described after "->" string.
|
| 47 |
+
|
| 48 |
+
The default is to return the current pixel value, which is
|
| 49 |
+
returned if no other match is found.
|
| 50 |
+
|
| 51 |
+
Operations:
|
| 52 |
+
|
| 53 |
+
- 4 - 4 way rotation
|
| 54 |
+
- N - Negate
|
| 55 |
+
- 1 - Dummy op for no other operation (an op must always be given)
|
| 56 |
+
- M - Mirroring
|
| 57 |
+
|
| 58 |
+
Example::
|
| 59 |
+
|
| 60 |
+
lb = LutBuilder(patterns = ["4:(... .1. 111)->1"])
|
| 61 |
+
lut = lb.build_lut()
|
| 62 |
+
|
| 63 |
+
"""
|
| 64 |
+
|
| 65 |
+
def __init__(
|
| 66 |
+
self, patterns: list[str] | None = None, op_name: str | None = None
|
| 67 |
+
) -> None:
|
| 68 |
+
if patterns is not None:
|
| 69 |
+
self.patterns = patterns
|
| 70 |
+
else:
|
| 71 |
+
self.patterns = []
|
| 72 |
+
self.lut: bytearray | None = None
|
| 73 |
+
if op_name is not None:
|
| 74 |
+
known_patterns = {
|
| 75 |
+
"corner": ["1:(... ... ...)->0", "4:(00. 01. ...)->1"],
|
| 76 |
+
"dilation4": ["4:(... .0. .1.)->1"],
|
| 77 |
+
"dilation8": ["4:(... .0. .1.)->1", "4:(... .0. ..1)->1"],
|
| 78 |
+
"erosion4": ["4:(... .1. .0.)->0"],
|
| 79 |
+
"erosion8": ["4:(... .1. .0.)->0", "4:(... .1. ..0)->0"],
|
| 80 |
+
"edge": [
|
| 81 |
+
"1:(... ... ...)->0",
|
| 82 |
+
"4:(.0. .1. ...)->1",
|
| 83 |
+
"4:(01. .1. ...)->1",
|
| 84 |
+
],
|
| 85 |
+
}
|
| 86 |
+
if op_name not in known_patterns:
|
| 87 |
+
msg = f"Unknown pattern {op_name}!"
|
| 88 |
+
raise Exception(msg)
|
| 89 |
+
|
| 90 |
+
self.patterns = known_patterns[op_name]
|
| 91 |
+
|
| 92 |
+
def add_patterns(self, patterns: list[str]) -> None:
|
| 93 |
+
self.patterns += patterns
|
| 94 |
+
|
| 95 |
+
def build_default_lut(self) -> None:
|
| 96 |
+
symbols = [0, 1]
|
| 97 |
+
m = 1 << 4 # pos of current pixel
|
| 98 |
+
self.lut = bytearray(symbols[(i & m) > 0] for i in range(LUT_SIZE))
|
| 99 |
+
|
| 100 |
+
def get_lut(self) -> bytearray | None:
|
| 101 |
+
return self.lut
|
| 102 |
+
|
| 103 |
+
def _string_permute(self, pattern: str, permutation: list[int]) -> str:
|
| 104 |
+
"""string_permute takes a pattern and a permutation and returns the
|
| 105 |
+
string permuted according to the permutation list.
|
| 106 |
+
"""
|
| 107 |
+
assert len(permutation) == 9
|
| 108 |
+
return "".join(pattern[p] for p in permutation)
|
| 109 |
+
|
| 110 |
+
def _pattern_permute(
|
| 111 |
+
self, basic_pattern: str, options: str, basic_result: int
|
| 112 |
+
) -> list[tuple[str, int]]:
|
| 113 |
+
"""pattern_permute takes a basic pattern and its result and clones
|
| 114 |
+
the pattern according to the modifications described in the $options
|
| 115 |
+
parameter. It returns a list of all cloned patterns."""
|
| 116 |
+
patterns = [(basic_pattern, basic_result)]
|
| 117 |
+
|
| 118 |
+
# rotations
|
| 119 |
+
if "4" in options:
|
| 120 |
+
res = patterns[-1][1]
|
| 121 |
+
for i in range(4):
|
| 122 |
+
patterns.append(
|
| 123 |
+
(self._string_permute(patterns[-1][0], ROTATION_MATRIX), res)
|
| 124 |
+
)
|
| 125 |
+
# mirror
|
| 126 |
+
if "M" in options:
|
| 127 |
+
n = len(patterns)
|
| 128 |
+
for pattern, res in patterns[:n]:
|
| 129 |
+
patterns.append((self._string_permute(pattern, MIRROR_MATRIX), res))
|
| 130 |
+
|
| 131 |
+
# negate
|
| 132 |
+
if "N" in options:
|
| 133 |
+
n = len(patterns)
|
| 134 |
+
for pattern, res in patterns[:n]:
|
| 135 |
+
# Swap 0 and 1
|
| 136 |
+
pattern = pattern.replace("0", "Z").replace("1", "0").replace("Z", "1")
|
| 137 |
+
res = 1 - int(res)
|
| 138 |
+
patterns.append((pattern, res))
|
| 139 |
+
|
| 140 |
+
return patterns
|
| 141 |
+
|
| 142 |
+
def build_lut(self) -> bytearray:
|
| 143 |
+
"""Compile all patterns into a morphology lut.
|
| 144 |
+
|
| 145 |
+
TBD :Build based on (file) morphlut:modify_lut
|
| 146 |
+
"""
|
| 147 |
+
self.build_default_lut()
|
| 148 |
+
assert self.lut is not None
|
| 149 |
+
patterns = []
|
| 150 |
+
|
| 151 |
+
# Parse and create symmetries of the patterns strings
|
| 152 |
+
for p in self.patterns:
|
| 153 |
+
m = re.search(r"(\w*):?\s*\((.+?)\)\s*->\s*(\d)", p.replace("\n", ""))
|
| 154 |
+
if not m:
|
| 155 |
+
msg = 'Syntax error in pattern "' + p + '"'
|
| 156 |
+
raise Exception(msg)
|
| 157 |
+
options = m.group(1)
|
| 158 |
+
pattern = m.group(2)
|
| 159 |
+
result = int(m.group(3))
|
| 160 |
+
|
| 161 |
+
# Get rid of spaces
|
| 162 |
+
pattern = pattern.replace(" ", "").replace("\n", "")
|
| 163 |
+
|
| 164 |
+
patterns += self._pattern_permute(pattern, options, result)
|
| 165 |
+
|
| 166 |
+
# compile the patterns into regular expressions for speed
|
| 167 |
+
compiled_patterns = []
|
| 168 |
+
for pattern in patterns:
|
| 169 |
+
p = pattern[0].replace(".", "X").replace("X", "[01]")
|
| 170 |
+
compiled_patterns.append((re.compile(p), pattern[1]))
|
| 171 |
+
|
| 172 |
+
# Step through table and find patterns that match.
|
| 173 |
+
# Note that all the patterns are searched. The last one
|
| 174 |
+
# caught overrides
|
| 175 |
+
for i in range(LUT_SIZE):
|
| 176 |
+
# Build the bit pattern
|
| 177 |
+
bitpattern = bin(i)[2:]
|
| 178 |
+
bitpattern = ("0" * (9 - len(bitpattern)) + bitpattern)[::-1]
|
| 179 |
+
|
| 180 |
+
for pattern, r in compiled_patterns:
|
| 181 |
+
if pattern.match(bitpattern):
|
| 182 |
+
self.lut[i] = [0, 1][r]
|
| 183 |
+
|
| 184 |
+
return self.lut
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
class MorphOp:
|
| 188 |
+
"""A class for binary morphological operators"""
|
| 189 |
+
|
| 190 |
+
def __init__(
|
| 191 |
+
self,
|
| 192 |
+
lut: bytearray | None = None,
|
| 193 |
+
op_name: str | None = None,
|
| 194 |
+
patterns: list[str] | None = None,
|
| 195 |
+
) -> None:
|
| 196 |
+
"""Create a binary morphological operator"""
|
| 197 |
+
self.lut = lut
|
| 198 |
+
if op_name is not None:
|
| 199 |
+
self.lut = LutBuilder(op_name=op_name).build_lut()
|
| 200 |
+
elif patterns is not None:
|
| 201 |
+
self.lut = LutBuilder(patterns=patterns).build_lut()
|
| 202 |
+
|
| 203 |
+
def apply(self, image: Image.Image) -> tuple[int, Image.Image]:
|
| 204 |
+
"""Run a single morphological operation on an image
|
| 205 |
+
|
| 206 |
+
Returns a tuple of the number of changed pixels and the
|
| 207 |
+
morphed image"""
|
| 208 |
+
if self.lut is None:
|
| 209 |
+
msg = "No operator loaded"
|
| 210 |
+
raise Exception(msg)
|
| 211 |
+
|
| 212 |
+
if image.mode != "L":
|
| 213 |
+
msg = "Image mode must be L"
|
| 214 |
+
raise ValueError(msg)
|
| 215 |
+
outimage = Image.new(image.mode, image.size, None)
|
| 216 |
+
count = _imagingmorph.apply(bytes(self.lut), image.im.id, outimage.im.id)
|
| 217 |
+
return count, outimage
|
| 218 |
+
|
| 219 |
+
def match(self, image: Image.Image) -> list[tuple[int, int]]:
|
| 220 |
+
"""Get a list of coordinates matching the morphological operation on
|
| 221 |
+
an image.
|
| 222 |
+
|
| 223 |
+
Returns a list of tuples of (x,y) coordinates
|
| 224 |
+
of all matching pixels. See :ref:`coordinate-system`."""
|
| 225 |
+
if self.lut is None:
|
| 226 |
+
msg = "No operator loaded"
|
| 227 |
+
raise Exception(msg)
|
| 228 |
+
|
| 229 |
+
if image.mode != "L":
|
| 230 |
+
msg = "Image mode must be L"
|
| 231 |
+
raise ValueError(msg)
|
| 232 |
+
return _imagingmorph.match(bytes(self.lut), image.im.id)
|
| 233 |
+
|
| 234 |
+
def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]:
|
| 235 |
+
"""Get a list of all turned on pixels in a binary image
|
| 236 |
+
|
| 237 |
+
Returns a list of tuples of (x,y) coordinates
|
| 238 |
+
of all matching pixels. See :ref:`coordinate-system`."""
|
| 239 |
+
|
| 240 |
+
if image.mode != "L":
|
| 241 |
+
msg = "Image mode must be L"
|
| 242 |
+
raise ValueError(msg)
|
| 243 |
+
return _imagingmorph.get_on_pixels(image.im.id)
|
| 244 |
+
|
| 245 |
+
def load_lut(self, filename: str) -> None:
|
| 246 |
+
"""Load an operator from an mrl file"""
|
| 247 |
+
with open(filename, "rb") as f:
|
| 248 |
+
self.lut = bytearray(f.read())
|
| 249 |
+
|
| 250 |
+
if len(self.lut) != LUT_SIZE:
|
| 251 |
+
self.lut = None
|
| 252 |
+
msg = "Wrong size operator file!"
|
| 253 |
+
raise Exception(msg)
|
| 254 |
+
|
| 255 |
+
def save_lut(self, filename: str) -> None:
|
| 256 |
+
"""Save an operator to an mrl file"""
|
| 257 |
+
if self.lut is None:
|
| 258 |
+
msg = "No operator loaded"
|
| 259 |
+
raise Exception(msg)
|
| 260 |
+
with open(filename, "wb") as f:
|
| 261 |
+
f.write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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# standard image operations
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 2001-10-20 fl Created
|
| 9 |
+
# 2001-10-23 fl Added autocontrast operator
|
| 10 |
+
# 2001-12-18 fl Added Kevin's fit operator
|
| 11 |
+
# 2004-03-14 fl Fixed potential division by zero in equalize
|
| 12 |
+
# 2005-05-05 fl Fixed equalize for low number of values
|
| 13 |
+
#
|
| 14 |
+
# Copyright (c) 2001-2004 by Secret Labs AB
|
| 15 |
+
# Copyright (c) 2001-2004 by Fredrik Lundh
|
| 16 |
+
#
|
| 17 |
+
# See the README file for information on usage and redistribution.
|
| 18 |
+
#
|
| 19 |
+
from __future__ import annotations
|
| 20 |
+
|
| 21 |
+
import functools
|
| 22 |
+
import operator
|
| 23 |
+
import re
|
| 24 |
+
from typing import Protocol, Sequence, cast
|
| 25 |
+
|
| 26 |
+
from . import ExifTags, Image, ImagePalette
|
| 27 |
+
|
| 28 |
+
#
|
| 29 |
+
# helpers
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def _border(border: int | tuple[int, ...]) -> tuple[int, int, int, int]:
|
| 33 |
+
if isinstance(border, tuple):
|
| 34 |
+
if len(border) == 2:
|
| 35 |
+
left, top = right, bottom = border
|
| 36 |
+
elif len(border) == 4:
|
| 37 |
+
left, top, right, bottom = border
|
| 38 |
+
else:
|
| 39 |
+
left = top = right = bottom = border
|
| 40 |
+
return left, top, right, bottom
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
def _color(color: str | int | tuple[int, ...], mode: str) -> int | tuple[int, ...]:
|
| 44 |
+
if isinstance(color, str):
|
| 45 |
+
from . import ImageColor
|
| 46 |
+
|
| 47 |
+
color = ImageColor.getcolor(color, mode)
|
| 48 |
+
return color
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def _lut(image: Image.Image, lut: list[int]) -> Image.Image:
|
| 52 |
+
if image.mode == "P":
|
| 53 |
+
# FIXME: apply to lookup table, not image data
|
| 54 |
+
msg = "mode P support coming soon"
|
| 55 |
+
raise NotImplementedError(msg)
|
| 56 |
+
elif image.mode in ("L", "RGB"):
|
| 57 |
+
if image.mode == "RGB" and len(lut) == 256:
|
| 58 |
+
lut = lut + lut + lut
|
| 59 |
+
return image.point(lut)
|
| 60 |
+
else:
|
| 61 |
+
msg = f"not supported for mode {image.mode}"
|
| 62 |
+
raise OSError(msg)
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
#
|
| 66 |
+
# actions
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
def autocontrast(
|
| 70 |
+
image: Image.Image,
|
| 71 |
+
cutoff: float | tuple[float, float] = 0,
|
| 72 |
+
ignore: int | Sequence[int] | None = None,
|
| 73 |
+
mask: Image.Image | None = None,
|
| 74 |
+
preserve_tone: bool = False,
|
| 75 |
+
) -> Image.Image:
|
| 76 |
+
"""
|
| 77 |
+
Maximize (normalize) image contrast. This function calculates a
|
| 78 |
+
histogram of the input image (or mask region), removes ``cutoff`` percent of the
|
| 79 |
+
lightest and darkest pixels from the histogram, and remaps the image
|
| 80 |
+
so that the darkest pixel becomes black (0), and the lightest
|
| 81 |
+
becomes white (255).
|
| 82 |
+
|
| 83 |
+
:param image: The image to process.
|
| 84 |
+
:param cutoff: The percent to cut off from the histogram on the low and
|
| 85 |
+
high ends. Either a tuple of (low, high), or a single
|
| 86 |
+
number for both.
|
| 87 |
+
:param ignore: The background pixel value (use None for no background).
|
| 88 |
+
:param mask: Histogram used in contrast operation is computed using pixels
|
| 89 |
+
within the mask. If no mask is given the entire image is used
|
| 90 |
+
for histogram computation.
|
| 91 |
+
:param preserve_tone: Preserve image tone in Photoshop-like style autocontrast.
|
| 92 |
+
|
| 93 |
+
.. versionadded:: 8.2.0
|
| 94 |
+
|
| 95 |
+
:return: An image.
|
| 96 |
+
"""
|
| 97 |
+
if preserve_tone:
|
| 98 |
+
histogram = image.convert("L").histogram(mask)
|
| 99 |
+
else:
|
| 100 |
+
histogram = image.histogram(mask)
|
| 101 |
+
|
| 102 |
+
lut = []
|
| 103 |
+
for layer in range(0, len(histogram), 256):
|
| 104 |
+
h = histogram[layer : layer + 256]
|
| 105 |
+
if ignore is not None:
|
| 106 |
+
# get rid of outliers
|
| 107 |
+
if isinstance(ignore, int):
|
| 108 |
+
h[ignore] = 0
|
| 109 |
+
else:
|
| 110 |
+
for ix in ignore:
|
| 111 |
+
h[ix] = 0
|
| 112 |
+
if cutoff:
|
| 113 |
+
# cut off pixels from both ends of the histogram
|
| 114 |
+
if not isinstance(cutoff, tuple):
|
| 115 |
+
cutoff = (cutoff, cutoff)
|
| 116 |
+
# get number of pixels
|
| 117 |
+
n = 0
|
| 118 |
+
for ix in range(256):
|
| 119 |
+
n = n + h[ix]
|
| 120 |
+
# remove cutoff% pixels from the low end
|
| 121 |
+
cut = int(n * cutoff[0] // 100)
|
| 122 |
+
for lo in range(256):
|
| 123 |
+
if cut > h[lo]:
|
| 124 |
+
cut = cut - h[lo]
|
| 125 |
+
h[lo] = 0
|
| 126 |
+
else:
|
| 127 |
+
h[lo] -= cut
|
| 128 |
+
cut = 0
|
| 129 |
+
if cut <= 0:
|
| 130 |
+
break
|
| 131 |
+
# remove cutoff% samples from the high end
|
| 132 |
+
cut = int(n * cutoff[1] // 100)
|
| 133 |
+
for hi in range(255, -1, -1):
|
| 134 |
+
if cut > h[hi]:
|
| 135 |
+
cut = cut - h[hi]
|
| 136 |
+
h[hi] = 0
|
| 137 |
+
else:
|
| 138 |
+
h[hi] -= cut
|
| 139 |
+
cut = 0
|
| 140 |
+
if cut <= 0:
|
| 141 |
+
break
|
| 142 |
+
# find lowest/highest samples after preprocessing
|
| 143 |
+
for lo in range(256):
|
| 144 |
+
if h[lo]:
|
| 145 |
+
break
|
| 146 |
+
for hi in range(255, -1, -1):
|
| 147 |
+
if h[hi]:
|
| 148 |
+
break
|
| 149 |
+
if hi <= lo:
|
| 150 |
+
# don't bother
|
| 151 |
+
lut.extend(list(range(256)))
|
| 152 |
+
else:
|
| 153 |
+
scale = 255.0 / (hi - lo)
|
| 154 |
+
offset = -lo * scale
|
| 155 |
+
for ix in range(256):
|
| 156 |
+
ix = int(ix * scale + offset)
|
| 157 |
+
if ix < 0:
|
| 158 |
+
ix = 0
|
| 159 |
+
elif ix > 255:
|
| 160 |
+
ix = 255
|
| 161 |
+
lut.append(ix)
|
| 162 |
+
return _lut(image, lut)
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def colorize(
|
| 166 |
+
image: Image.Image,
|
| 167 |
+
black: str | tuple[int, ...],
|
| 168 |
+
white: str | tuple[int, ...],
|
| 169 |
+
mid: str | int | tuple[int, ...] | None = None,
|
| 170 |
+
blackpoint: int = 0,
|
| 171 |
+
whitepoint: int = 255,
|
| 172 |
+
midpoint: int = 127,
|
| 173 |
+
) -> Image.Image:
|
| 174 |
+
"""
|
| 175 |
+
Colorize grayscale image.
|
| 176 |
+
This function calculates a color wedge which maps all black pixels in
|
| 177 |
+
the source image to the first color and all white pixels to the
|
| 178 |
+
second color. If ``mid`` is specified, it uses three-color mapping.
|
| 179 |
+
The ``black`` and ``white`` arguments should be RGB tuples or color names;
|
| 180 |
+
optionally you can use three-color mapping by also specifying ``mid``.
|
| 181 |
+
Mapping positions for any of the colors can be specified
|
| 182 |
+
(e.g. ``blackpoint``), where these parameters are the integer
|
| 183 |
+
value corresponding to where the corresponding color should be mapped.
|
| 184 |
+
These parameters must have logical order, such that
|
| 185 |
+
``blackpoint <= midpoint <= whitepoint`` (if ``mid`` is specified).
|
| 186 |
+
|
| 187 |
+
:param image: The image to colorize.
|
| 188 |
+
:param black: The color to use for black input pixels.
|
| 189 |
+
:param white: The color to use for white input pixels.
|
| 190 |
+
:param mid: The color to use for midtone input pixels.
|
| 191 |
+
:param blackpoint: an int value [0, 255] for the black mapping.
|
| 192 |
+
:param whitepoint: an int value [0, 255] for the white mapping.
|
| 193 |
+
:param midpoint: an int value [0, 255] for the midtone mapping.
|
| 194 |
+
:return: An image.
|
| 195 |
+
"""
|
| 196 |
+
|
| 197 |
+
# Initial asserts
|
| 198 |
+
assert image.mode == "L"
|
| 199 |
+
if mid is None:
|
| 200 |
+
assert 0 <= blackpoint <= whitepoint <= 255
|
| 201 |
+
else:
|
| 202 |
+
assert 0 <= blackpoint <= midpoint <= whitepoint <= 255
|
| 203 |
+
|
| 204 |
+
# Define colors from arguments
|
| 205 |
+
rgb_black = cast(Sequence[int], _color(black, "RGB"))
|
| 206 |
+
rgb_white = cast(Sequence[int], _color(white, "RGB"))
|
| 207 |
+
rgb_mid = cast(Sequence[int], _color(mid, "RGB")) if mid is not None else None
|
| 208 |
+
|
| 209 |
+
# Empty lists for the mapping
|
| 210 |
+
red = []
|
| 211 |
+
green = []
|
| 212 |
+
blue = []
|
| 213 |
+
|
| 214 |
+
# Create the low-end values
|
| 215 |
+
for i in range(0, blackpoint):
|
| 216 |
+
red.append(rgb_black[0])
|
| 217 |
+
green.append(rgb_black[1])
|
| 218 |
+
blue.append(rgb_black[2])
|
| 219 |
+
|
| 220 |
+
# Create the mapping (2-color)
|
| 221 |
+
if rgb_mid is None:
|
| 222 |
+
range_map = range(0, whitepoint - blackpoint)
|
| 223 |
+
|
| 224 |
+
for i in range_map:
|
| 225 |
+
red.append(
|
| 226 |
+
rgb_black[0] + i * (rgb_white[0] - rgb_black[0]) // len(range_map)
|
| 227 |
+
)
|
| 228 |
+
green.append(
|
| 229 |
+
rgb_black[1] + i * (rgb_white[1] - rgb_black[1]) // len(range_map)
|
| 230 |
+
)
|
| 231 |
+
blue.append(
|
| 232 |
+
rgb_black[2] + i * (rgb_white[2] - rgb_black[2]) // len(range_map)
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
# Create the mapping (3-color)
|
| 236 |
+
else:
|
| 237 |
+
range_map1 = range(0, midpoint - blackpoint)
|
| 238 |
+
range_map2 = range(0, whitepoint - midpoint)
|
| 239 |
+
|
| 240 |
+
for i in range_map1:
|
| 241 |
+
red.append(
|
| 242 |
+
rgb_black[0] + i * (rgb_mid[0] - rgb_black[0]) // len(range_map1)
|
| 243 |
+
)
|
| 244 |
+
green.append(
|
| 245 |
+
rgb_black[1] + i * (rgb_mid[1] - rgb_black[1]) // len(range_map1)
|
| 246 |
+
)
|
| 247 |
+
blue.append(
|
| 248 |
+
rgb_black[2] + i * (rgb_mid[2] - rgb_black[2]) // len(range_map1)
|
| 249 |
+
)
|
| 250 |
+
for i in range_map2:
|
| 251 |
+
red.append(rgb_mid[0] + i * (rgb_white[0] - rgb_mid[0]) // len(range_map2))
|
| 252 |
+
green.append(
|
| 253 |
+
rgb_mid[1] + i * (rgb_white[1] - rgb_mid[1]) // len(range_map2)
|
| 254 |
+
)
|
| 255 |
+
blue.append(rgb_mid[2] + i * (rgb_white[2] - rgb_mid[2]) // len(range_map2))
|
| 256 |
+
|
| 257 |
+
# Create the high-end values
|
| 258 |
+
for i in range(0, 256 - whitepoint):
|
| 259 |
+
red.append(rgb_white[0])
|
| 260 |
+
green.append(rgb_white[1])
|
| 261 |
+
blue.append(rgb_white[2])
|
| 262 |
+
|
| 263 |
+
# Return converted image
|
| 264 |
+
image = image.convert("RGB")
|
| 265 |
+
return _lut(image, red + green + blue)
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def contain(
|
| 269 |
+
image: Image.Image, size: tuple[int, int], method: int = Image.Resampling.BICUBIC
|
| 270 |
+
) -> Image.Image:
|
| 271 |
+
"""
|
| 272 |
+
Returns a resized version of the image, set to the maximum width and height
|
| 273 |
+
within the requested size, while maintaining the original aspect ratio.
|
| 274 |
+
|
| 275 |
+
:param image: The image to resize.
|
| 276 |
+
:param size: The requested output size in pixels, given as a
|
| 277 |
+
(width, height) tuple.
|
| 278 |
+
:param method: Resampling method to use. Default is
|
| 279 |
+
:py:attr:`~PIL.Image.Resampling.BICUBIC`.
|
| 280 |
+
See :ref:`concept-filters`.
|
| 281 |
+
:return: An image.
|
| 282 |
+
"""
|
| 283 |
+
|
| 284 |
+
im_ratio = image.width / image.height
|
| 285 |
+
dest_ratio = size[0] / size[1]
|
| 286 |
+
|
| 287 |
+
if im_ratio != dest_ratio:
|
| 288 |
+
if im_ratio > dest_ratio:
|
| 289 |
+
new_height = round(image.height / image.width * size[0])
|
| 290 |
+
if new_height != size[1]:
|
| 291 |
+
size = (size[0], new_height)
|
| 292 |
+
else:
|
| 293 |
+
new_width = round(image.width / image.height * size[1])
|
| 294 |
+
if new_width != size[0]:
|
| 295 |
+
size = (new_width, size[1])
|
| 296 |
+
return image.resize(size, resample=method)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
def cover(
|
| 300 |
+
image: Image.Image, size: tuple[int, int], method: int = Image.Resampling.BICUBIC
|
| 301 |
+
) -> Image.Image:
|
| 302 |
+
"""
|
| 303 |
+
Returns a resized version of the image, so that the requested size is
|
| 304 |
+
covered, while maintaining the original aspect ratio.
|
| 305 |
+
|
| 306 |
+
:param image: The image to resize.
|
| 307 |
+
:param size: The requested output size in pixels, given as a
|
| 308 |
+
(width, height) tuple.
|
| 309 |
+
:param method: Resampling method to use. Default is
|
| 310 |
+
:py:attr:`~PIL.Image.Resampling.BICUBIC`.
|
| 311 |
+
See :ref:`concept-filters`.
|
| 312 |
+
:return: An image.
|
| 313 |
+
"""
|
| 314 |
+
|
| 315 |
+
im_ratio = image.width / image.height
|
| 316 |
+
dest_ratio = size[0] / size[1]
|
| 317 |
+
|
| 318 |
+
if im_ratio != dest_ratio:
|
| 319 |
+
if im_ratio < dest_ratio:
|
| 320 |
+
new_height = round(image.height / image.width * size[0])
|
| 321 |
+
if new_height != size[1]:
|
| 322 |
+
size = (size[0], new_height)
|
| 323 |
+
else:
|
| 324 |
+
new_width = round(image.width / image.height * size[1])
|
| 325 |
+
if new_width != size[0]:
|
| 326 |
+
size = (new_width, size[1])
|
| 327 |
+
return image.resize(size, resample=method)
|
| 328 |
+
|
| 329 |
+
|
| 330 |
+
def pad(
|
| 331 |
+
image: Image.Image,
|
| 332 |
+
size: tuple[int, int],
|
| 333 |
+
method: int = Image.Resampling.BICUBIC,
|
| 334 |
+
color: str | int | tuple[int, ...] | None = None,
|
| 335 |
+
centering: tuple[float, float] = (0.5, 0.5),
|
| 336 |
+
) -> Image.Image:
|
| 337 |
+
"""
|
| 338 |
+
Returns a resized and padded version of the image, expanded to fill the
|
| 339 |
+
requested aspect ratio and size.
|
| 340 |
+
|
| 341 |
+
:param image: The image to resize and crop.
|
| 342 |
+
:param size: The requested output size in pixels, given as a
|
| 343 |
+
(width, height) tuple.
|
| 344 |
+
:param method: Resampling method to use. Default is
|
| 345 |
+
:py:attr:`~PIL.Image.Resampling.BICUBIC`.
|
| 346 |
+
See :ref:`concept-filters`.
|
| 347 |
+
:param color: The background color of the padded image.
|
| 348 |
+
:param centering: Control the position of the original image within the
|
| 349 |
+
padded version.
|
| 350 |
+
|
| 351 |
+
(0.5, 0.5) will keep the image centered
|
| 352 |
+
(0, 0) will keep the image aligned to the top left
|
| 353 |
+
(1, 1) will keep the image aligned to the bottom
|
| 354 |
+
right
|
| 355 |
+
:return: An image.
|
| 356 |
+
"""
|
| 357 |
+
|
| 358 |
+
resized = contain(image, size, method)
|
| 359 |
+
if resized.size == size:
|
| 360 |
+
out = resized
|
| 361 |
+
else:
|
| 362 |
+
out = Image.new(image.mode, size, color)
|
| 363 |
+
if resized.palette:
|
| 364 |
+
out.putpalette(resized.getpalette())
|
| 365 |
+
if resized.width != size[0]:
|
| 366 |
+
x = round((size[0] - resized.width) * max(0, min(centering[0], 1)))
|
| 367 |
+
out.paste(resized, (x, 0))
|
| 368 |
+
else:
|
| 369 |
+
y = round((size[1] - resized.height) * max(0, min(centering[1], 1)))
|
| 370 |
+
out.paste(resized, (0, y))
|
| 371 |
+
return out
|
| 372 |
+
|
| 373 |
+
|
| 374 |
+
def crop(image: Image.Image, border: int = 0) -> Image.Image:
|
| 375 |
+
"""
|
| 376 |
+
Remove border from image. The same amount of pixels are removed
|
| 377 |
+
from all four sides. This function works on all image modes.
|
| 378 |
+
|
| 379 |
+
.. seealso:: :py:meth:`~PIL.Image.Image.crop`
|
| 380 |
+
|
| 381 |
+
:param image: The image to crop.
|
| 382 |
+
:param border: The number of pixels to remove.
|
| 383 |
+
:return: An image.
|
| 384 |
+
"""
|
| 385 |
+
left, top, right, bottom = _border(border)
|
| 386 |
+
return image.crop((left, top, image.size[0] - right, image.size[1] - bottom))
|
| 387 |
+
|
| 388 |
+
|
| 389 |
+
def scale(
|
| 390 |
+
image: Image.Image, factor: float, resample: int = Image.Resampling.BICUBIC
|
| 391 |
+
) -> Image.Image:
|
| 392 |
+
"""
|
| 393 |
+
Returns a rescaled image by a specific factor given in parameter.
|
| 394 |
+
A factor greater than 1 expands the image, between 0 and 1 contracts the
|
| 395 |
+
image.
|
| 396 |
+
|
| 397 |
+
:param image: The image to rescale.
|
| 398 |
+
:param factor: The expansion factor, as a float.
|
| 399 |
+
:param resample: Resampling method to use. Default is
|
| 400 |
+
:py:attr:`~PIL.Image.Resampling.BICUBIC`.
|
| 401 |
+
See :ref:`concept-filters`.
|
| 402 |
+
:returns: An :py:class:`~PIL.Image.Image` object.
|
| 403 |
+
"""
|
| 404 |
+
if factor == 1:
|
| 405 |
+
return image.copy()
|
| 406 |
+
elif factor <= 0:
|
| 407 |
+
msg = "the factor must be greater than 0"
|
| 408 |
+
raise ValueError(msg)
|
| 409 |
+
else:
|
| 410 |
+
size = (round(factor * image.width), round(factor * image.height))
|
| 411 |
+
return image.resize(size, resample)
|
| 412 |
+
|
| 413 |
+
|
| 414 |
+
class SupportsGetMesh(Protocol):
|
| 415 |
+
"""
|
| 416 |
+
An object that supports the ``getmesh`` method, taking an image as an
|
| 417 |
+
argument, and returning a list of tuples. Each tuple contains two tuples,
|
| 418 |
+
the source box as a tuple of 4 integers, and a tuple of 8 integers for the
|
| 419 |
+
final quadrilateral, in order of top left, bottom left, bottom right, top
|
| 420 |
+
right.
|
| 421 |
+
"""
|
| 422 |
+
|
| 423 |
+
def getmesh(
|
| 424 |
+
self, image: Image.Image
|
| 425 |
+
) -> list[
|
| 426 |
+
tuple[tuple[int, int, int, int], tuple[int, int, int, int, int, int, int, int]]
|
| 427 |
+
]: ...
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def deform(
|
| 431 |
+
image: Image.Image,
|
| 432 |
+
deformer: SupportsGetMesh,
|
| 433 |
+
resample: int = Image.Resampling.BILINEAR,
|
| 434 |
+
) -> Image.Image:
|
| 435 |
+
"""
|
| 436 |
+
Deform the image.
|
| 437 |
+
|
| 438 |
+
:param image: The image to deform.
|
| 439 |
+
:param deformer: A deformer object. Any object that implements a
|
| 440 |
+
``getmesh`` method can be used.
|
| 441 |
+
:param resample: An optional resampling filter. Same values possible as
|
| 442 |
+
in the PIL.Image.transform function.
|
| 443 |
+
:return: An image.
|
| 444 |
+
"""
|
| 445 |
+
return image.transform(
|
| 446 |
+
image.size, Image.Transform.MESH, deformer.getmesh(image), resample
|
| 447 |
+
)
|
| 448 |
+
|
| 449 |
+
|
| 450 |
+
def equalize(image: Image.Image, mask: Image.Image | None = None) -> Image.Image:
|
| 451 |
+
"""
|
| 452 |
+
Equalize the image histogram. This function applies a non-linear
|
| 453 |
+
mapping to the input image, in order to create a uniform
|
| 454 |
+
distribution of grayscale values in the output image.
|
| 455 |
+
|
| 456 |
+
:param image: The image to equalize.
|
| 457 |
+
:param mask: An optional mask. If given, only the pixels selected by
|
| 458 |
+
the mask are included in the analysis.
|
| 459 |
+
:return: An image.
|
| 460 |
+
"""
|
| 461 |
+
if image.mode == "P":
|
| 462 |
+
image = image.convert("RGB")
|
| 463 |
+
h = image.histogram(mask)
|
| 464 |
+
lut = []
|
| 465 |
+
for b in range(0, len(h), 256):
|
| 466 |
+
histo = [_f for _f in h[b : b + 256] if _f]
|
| 467 |
+
if len(histo) <= 1:
|
| 468 |
+
lut.extend(list(range(256)))
|
| 469 |
+
else:
|
| 470 |
+
step = (functools.reduce(operator.add, histo) - histo[-1]) // 255
|
| 471 |
+
if not step:
|
| 472 |
+
lut.extend(list(range(256)))
|
| 473 |
+
else:
|
| 474 |
+
n = step // 2
|
| 475 |
+
for i in range(256):
|
| 476 |
+
lut.append(n // step)
|
| 477 |
+
n = n + h[i + b]
|
| 478 |
+
return _lut(image, lut)
|
| 479 |
+
|
| 480 |
+
|
| 481 |
+
def expand(
|
| 482 |
+
image: Image.Image,
|
| 483 |
+
border: int | tuple[int, ...] = 0,
|
| 484 |
+
fill: str | int | tuple[int, ...] = 0,
|
| 485 |
+
) -> Image.Image:
|
| 486 |
+
"""
|
| 487 |
+
Add border to the image
|
| 488 |
+
|
| 489 |
+
:param image: The image to expand.
|
| 490 |
+
:param border: Border width, in pixels.
|
| 491 |
+
:param fill: Pixel fill value (a color value). Default is 0 (black).
|
| 492 |
+
:return: An image.
|
| 493 |
+
"""
|
| 494 |
+
left, top, right, bottom = _border(border)
|
| 495 |
+
width = left + image.size[0] + right
|
| 496 |
+
height = top + image.size[1] + bottom
|
| 497 |
+
color = _color(fill, image.mode)
|
| 498 |
+
if image.palette:
|
| 499 |
+
palette = ImagePalette.ImagePalette(palette=image.getpalette())
|
| 500 |
+
if isinstance(color, tuple) and (len(color) == 3 or len(color) == 4):
|
| 501 |
+
color = palette.getcolor(color)
|
| 502 |
+
else:
|
| 503 |
+
palette = None
|
| 504 |
+
out = Image.new(image.mode, (width, height), color)
|
| 505 |
+
if palette:
|
| 506 |
+
out.putpalette(palette.palette)
|
| 507 |
+
out.paste(image, (left, top))
|
| 508 |
+
return out
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def fit(
|
| 512 |
+
image: Image.Image,
|
| 513 |
+
size: tuple[int, int],
|
| 514 |
+
method: int = Image.Resampling.BICUBIC,
|
| 515 |
+
bleed: float = 0.0,
|
| 516 |
+
centering: tuple[float, float] = (0.5, 0.5),
|
| 517 |
+
) -> Image.Image:
|
| 518 |
+
"""
|
| 519 |
+
Returns a resized and cropped version of the image, cropped to the
|
| 520 |
+
requested aspect ratio and size.
|
| 521 |
+
|
| 522 |
+
This function was contributed by Kevin Cazabon.
|
| 523 |
+
|
| 524 |
+
:param image: The image to resize and crop.
|
| 525 |
+
:param size: The requested output size in pixels, given as a
|
| 526 |
+
(width, height) tuple.
|
| 527 |
+
:param method: Resampling method to use. Default is
|
| 528 |
+
:py:attr:`~PIL.Image.Resampling.BICUBIC`.
|
| 529 |
+
See :ref:`concept-filters`.
|
| 530 |
+
:param bleed: Remove a border around the outside of the image from all
|
| 531 |
+
four edges. The value is a decimal percentage (use 0.01 for
|
| 532 |
+
one percent). The default value is 0 (no border).
|
| 533 |
+
Cannot be greater than or equal to 0.5.
|
| 534 |
+
:param centering: Control the cropping position. Use (0.5, 0.5) for
|
| 535 |
+
center cropping (e.g. if cropping the width, take 50% off
|
| 536 |
+
of the left side, and therefore 50% off the right side).
|
| 537 |
+
(0.0, 0.0) will crop from the top left corner (i.e. if
|
| 538 |
+
cropping the width, take all of the crop off of the right
|
| 539 |
+
side, and if cropping the height, take all of it off the
|
| 540 |
+
bottom). (1.0, 0.0) will crop from the bottom left
|
| 541 |
+
corner, etc. (i.e. if cropping the width, take all of the
|
| 542 |
+
crop off the left side, and if cropping the height take
|
| 543 |
+
none from the top, and therefore all off the bottom).
|
| 544 |
+
:return: An image.
|
| 545 |
+
"""
|
| 546 |
+
|
| 547 |
+
# by Kevin Cazabon, Feb 17/2000
|
| 548 |
+
# kevin@cazabon.com
|
| 549 |
+
# https://www.cazabon.com
|
| 550 |
+
|
| 551 |
+
centering_x, centering_y = centering
|
| 552 |
+
|
| 553 |
+
if not 0.0 <= centering_x <= 1.0:
|
| 554 |
+
centering_x = 0.5
|
| 555 |
+
if not 0.0 <= centering_y <= 1.0:
|
| 556 |
+
centering_y = 0.5
|
| 557 |
+
|
| 558 |
+
if not 0.0 <= bleed < 0.5:
|
| 559 |
+
bleed = 0.0
|
| 560 |
+
|
| 561 |
+
# calculate the area to use for resizing and cropping, subtracting
|
| 562 |
+
# the 'bleed' around the edges
|
| 563 |
+
|
| 564 |
+
# number of pixels to trim off on Top and Bottom, Left and Right
|
| 565 |
+
bleed_pixels = (bleed * image.size[0], bleed * image.size[1])
|
| 566 |
+
|
| 567 |
+
live_size = (
|
| 568 |
+
image.size[0] - bleed_pixels[0] * 2,
|
| 569 |
+
image.size[1] - bleed_pixels[1] * 2,
|
| 570 |
+
)
|
| 571 |
+
|
| 572 |
+
# calculate the aspect ratio of the live_size
|
| 573 |
+
live_size_ratio = live_size[0] / live_size[1]
|
| 574 |
+
|
| 575 |
+
# calculate the aspect ratio of the output image
|
| 576 |
+
output_ratio = size[0] / size[1]
|
| 577 |
+
|
| 578 |
+
# figure out if the sides or top/bottom will be cropped off
|
| 579 |
+
if live_size_ratio == output_ratio:
|
| 580 |
+
# live_size is already the needed ratio
|
| 581 |
+
crop_width = live_size[0]
|
| 582 |
+
crop_height = live_size[1]
|
| 583 |
+
elif live_size_ratio >= output_ratio:
|
| 584 |
+
# live_size is wider than what's needed, crop the sides
|
| 585 |
+
crop_width = output_ratio * live_size[1]
|
| 586 |
+
crop_height = live_size[1]
|
| 587 |
+
else:
|
| 588 |
+
# live_size is taller than what's needed, crop the top and bottom
|
| 589 |
+
crop_width = live_size[0]
|
| 590 |
+
crop_height = live_size[0] / output_ratio
|
| 591 |
+
|
| 592 |
+
# make the crop
|
| 593 |
+
crop_left = bleed_pixels[0] + (live_size[0] - crop_width) * centering_x
|
| 594 |
+
crop_top = bleed_pixels[1] + (live_size[1] - crop_height) * centering_y
|
| 595 |
+
|
| 596 |
+
crop = (crop_left, crop_top, crop_left + crop_width, crop_top + crop_height)
|
| 597 |
+
|
| 598 |
+
# resize the image and return it
|
| 599 |
+
return image.resize(size, method, box=crop)
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
def flip(image: Image.Image) -> Image.Image:
|
| 603 |
+
"""
|
| 604 |
+
Flip the image vertically (top to bottom).
|
| 605 |
+
|
| 606 |
+
:param image: The image to flip.
|
| 607 |
+
:return: An image.
|
| 608 |
+
"""
|
| 609 |
+
return image.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
|
| 610 |
+
|
| 611 |
+
|
| 612 |
+
def grayscale(image: Image.Image) -> Image.Image:
|
| 613 |
+
"""
|
| 614 |
+
Convert the image to grayscale.
|
| 615 |
+
|
| 616 |
+
:param image: The image to convert.
|
| 617 |
+
:return: An image.
|
| 618 |
+
"""
|
| 619 |
+
return image.convert("L")
|
| 620 |
+
|
| 621 |
+
|
| 622 |
+
def invert(image: Image.Image) -> Image.Image:
|
| 623 |
+
"""
|
| 624 |
+
Invert (negate) the image.
|
| 625 |
+
|
| 626 |
+
:param image: The image to invert.
|
| 627 |
+
:return: An image.
|
| 628 |
+
"""
|
| 629 |
+
lut = list(range(255, -1, -1))
|
| 630 |
+
return image.point(lut) if image.mode == "1" else _lut(image, lut)
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
def mirror(image: Image.Image) -> Image.Image:
|
| 634 |
+
"""
|
| 635 |
+
Flip image horizontally (left to right).
|
| 636 |
+
|
| 637 |
+
:param image: The image to mirror.
|
| 638 |
+
:return: An image.
|
| 639 |
+
"""
|
| 640 |
+
return image.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
|
| 641 |
+
|
| 642 |
+
|
| 643 |
+
def posterize(image: Image.Image, bits: int) -> Image.Image:
|
| 644 |
+
"""
|
| 645 |
+
Reduce the number of bits for each color channel.
|
| 646 |
+
|
| 647 |
+
:param image: The image to posterize.
|
| 648 |
+
:param bits: The number of bits to keep for each channel (1-8).
|
| 649 |
+
:return: An image.
|
| 650 |
+
"""
|
| 651 |
+
mask = ~(2 ** (8 - bits) - 1)
|
| 652 |
+
lut = [i & mask for i in range(256)]
|
| 653 |
+
return _lut(image, lut)
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
def solarize(image: Image.Image, threshold: int = 128) -> Image.Image:
|
| 657 |
+
"""
|
| 658 |
+
Invert all pixel values above a threshold.
|
| 659 |
+
|
| 660 |
+
:param image: The image to solarize.
|
| 661 |
+
:param threshold: All pixels above this grayscale level are inverted.
|
| 662 |
+
:return: An image.
|
| 663 |
+
"""
|
| 664 |
+
lut = []
|
| 665 |
+
for i in range(256):
|
| 666 |
+
if i < threshold:
|
| 667 |
+
lut.append(i)
|
| 668 |
+
else:
|
| 669 |
+
lut.append(255 - i)
|
| 670 |
+
return _lut(image, lut)
|
| 671 |
+
|
| 672 |
+
|
| 673 |
+
def exif_transpose(image: Image.Image, *, in_place: bool = False) -> Image.Image | None:
|
| 674 |
+
"""
|
| 675 |
+
If an image has an EXIF Orientation tag, other than 1, transpose the image
|
| 676 |
+
accordingly, and remove the orientation data.
|
| 677 |
+
|
| 678 |
+
:param image: The image to transpose.
|
| 679 |
+
:param in_place: Boolean. Keyword-only argument.
|
| 680 |
+
If ``True``, the original image is modified in-place, and ``None`` is returned.
|
| 681 |
+
If ``False`` (default), a new :py:class:`~PIL.Image.Image` object is returned
|
| 682 |
+
with the transposition applied. If there is no transposition, a copy of the
|
| 683 |
+
image will be returned.
|
| 684 |
+
"""
|
| 685 |
+
image.load()
|
| 686 |
+
image_exif = image.getexif()
|
| 687 |
+
orientation = image_exif.get(ExifTags.Base.Orientation, 1)
|
| 688 |
+
method = {
|
| 689 |
+
2: Image.Transpose.FLIP_LEFT_RIGHT,
|
| 690 |
+
3: Image.Transpose.ROTATE_180,
|
| 691 |
+
4: Image.Transpose.FLIP_TOP_BOTTOM,
|
| 692 |
+
5: Image.Transpose.TRANSPOSE,
|
| 693 |
+
6: Image.Transpose.ROTATE_270,
|
| 694 |
+
7: Image.Transpose.TRANSVERSE,
|
| 695 |
+
8: Image.Transpose.ROTATE_90,
|
| 696 |
+
}.get(orientation)
|
| 697 |
+
if method is not None:
|
| 698 |
+
transposed_image = image.transpose(method)
|
| 699 |
+
if in_place:
|
| 700 |
+
image.im = transposed_image.im
|
| 701 |
+
image.pyaccess = None
|
| 702 |
+
image._size = transposed_image._size
|
| 703 |
+
exif_image = image if in_place else transposed_image
|
| 704 |
+
|
| 705 |
+
exif = exif_image.getexif()
|
| 706 |
+
if ExifTags.Base.Orientation in exif:
|
| 707 |
+
del exif[ExifTags.Base.Orientation]
|
| 708 |
+
if "exif" in exif_image.info:
|
| 709 |
+
exif_image.info["exif"] = exif.tobytes()
|
| 710 |
+
elif "Raw profile type exif" in exif_image.info:
|
| 711 |
+
exif_image.info["Raw profile type exif"] = exif.tobytes().hex()
|
| 712 |
+
for key in ("XML:com.adobe.xmp", "xmp"):
|
| 713 |
+
if key in exif_image.info:
|
| 714 |
+
for pattern in (
|
| 715 |
+
r'tiff:Orientation="([0-9])"',
|
| 716 |
+
r"<tiff:Orientation>([0-9])</tiff:Orientation>",
|
| 717 |
+
):
|
| 718 |
+
value = exif_image.info[key]
|
| 719 |
+
exif_image.info[key] = (
|
| 720 |
+
re.sub(pattern, "", value)
|
| 721 |
+
if isinstance(value, str)
|
| 722 |
+
else re.sub(pattern.encode(), b"", value)
|
| 723 |
+
)
|
| 724 |
+
if not in_place:
|
| 725 |
+
return transposed_image
|
| 726 |
+
elif not in_place:
|
| 727 |
+
return image.copy()
|
| 728 |
+
return None
|
.venv/lib/python3.11/site-packages/PIL/ImagePalette.py
ADDED
|
@@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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# image palette object
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1996-03-11 fl Rewritten.
|
| 9 |
+
# 1997-01-03 fl Up and running.
|
| 10 |
+
# 1997-08-23 fl Added load hack
|
| 11 |
+
# 2001-04-16 fl Fixed randint shadow bug in random()
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) 1997-2001 by Secret Labs AB
|
| 14 |
+
# Copyright (c) 1996-1997 by Fredrik Lundh
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import array
|
| 21 |
+
from typing import IO, TYPE_CHECKING, Sequence
|
| 22 |
+
|
| 23 |
+
from . import GimpGradientFile, GimpPaletteFile, ImageColor, PaletteFile
|
| 24 |
+
|
| 25 |
+
if TYPE_CHECKING:
|
| 26 |
+
from . import Image
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
class ImagePalette:
|
| 30 |
+
"""
|
| 31 |
+
Color palette for palette mapped images
|
| 32 |
+
|
| 33 |
+
:param mode: The mode to use for the palette. See:
|
| 34 |
+
:ref:`concept-modes`. Defaults to "RGB"
|
| 35 |
+
:param palette: An optional palette. If given, it must be a bytearray,
|
| 36 |
+
an array or a list of ints between 0-255. The list must consist of
|
| 37 |
+
all channels for one color followed by the next color (e.g. RGBRGBRGB).
|
| 38 |
+
Defaults to an empty palette.
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
def __init__(
|
| 42 |
+
self,
|
| 43 |
+
mode: str = "RGB",
|
| 44 |
+
palette: Sequence[int] | bytes | bytearray | None = None,
|
| 45 |
+
) -> None:
|
| 46 |
+
self.mode = mode
|
| 47 |
+
self.rawmode: str | None = None # if set, palette contains raw data
|
| 48 |
+
self.palette = palette or bytearray()
|
| 49 |
+
self.dirty: int | None = None
|
| 50 |
+
|
| 51 |
+
@property
|
| 52 |
+
def palette(self) -> Sequence[int] | bytes | bytearray:
|
| 53 |
+
return self._palette
|
| 54 |
+
|
| 55 |
+
@palette.setter
|
| 56 |
+
def palette(self, palette: Sequence[int] | bytes | bytearray) -> None:
|
| 57 |
+
self._colors: dict[tuple[int, ...], int] | None = None
|
| 58 |
+
self._palette = palette
|
| 59 |
+
|
| 60 |
+
@property
|
| 61 |
+
def colors(self) -> dict[tuple[int, ...], int]:
|
| 62 |
+
if self._colors is None:
|
| 63 |
+
mode_len = len(self.mode)
|
| 64 |
+
self._colors = {}
|
| 65 |
+
for i in range(0, len(self.palette), mode_len):
|
| 66 |
+
color = tuple(self.palette[i : i + mode_len])
|
| 67 |
+
if color in self._colors:
|
| 68 |
+
continue
|
| 69 |
+
self._colors[color] = i // mode_len
|
| 70 |
+
return self._colors
|
| 71 |
+
|
| 72 |
+
@colors.setter
|
| 73 |
+
def colors(self, colors: dict[tuple[int, ...], int]) -> None:
|
| 74 |
+
self._colors = colors
|
| 75 |
+
|
| 76 |
+
def copy(self) -> ImagePalette:
|
| 77 |
+
new = ImagePalette()
|
| 78 |
+
|
| 79 |
+
new.mode = self.mode
|
| 80 |
+
new.rawmode = self.rawmode
|
| 81 |
+
if self.palette is not None:
|
| 82 |
+
new.palette = self.palette[:]
|
| 83 |
+
new.dirty = self.dirty
|
| 84 |
+
|
| 85 |
+
return new
|
| 86 |
+
|
| 87 |
+
def getdata(self) -> tuple[str, Sequence[int] | bytes | bytearray]:
|
| 88 |
+
"""
|
| 89 |
+
Get palette contents in format suitable for the low-level
|
| 90 |
+
``im.putpalette`` primitive.
|
| 91 |
+
|
| 92 |
+
.. warning:: This method is experimental.
|
| 93 |
+
"""
|
| 94 |
+
if self.rawmode:
|
| 95 |
+
return self.rawmode, self.palette
|
| 96 |
+
return self.mode, self.tobytes()
|
| 97 |
+
|
| 98 |
+
def tobytes(self) -> bytes:
|
| 99 |
+
"""Convert palette to bytes.
|
| 100 |
+
|
| 101 |
+
.. warning:: This method is experimental.
|
| 102 |
+
"""
|
| 103 |
+
if self.rawmode:
|
| 104 |
+
msg = "palette contains raw palette data"
|
| 105 |
+
raise ValueError(msg)
|
| 106 |
+
if isinstance(self.palette, bytes):
|
| 107 |
+
return self.palette
|
| 108 |
+
arr = array.array("B", self.palette)
|
| 109 |
+
return arr.tobytes()
|
| 110 |
+
|
| 111 |
+
# Declare tostring as an alias for tobytes
|
| 112 |
+
tostring = tobytes
|
| 113 |
+
|
| 114 |
+
def _new_color_index(
|
| 115 |
+
self, image: Image.Image | None = None, e: Exception | None = None
|
| 116 |
+
) -> int:
|
| 117 |
+
if not isinstance(self.palette, bytearray):
|
| 118 |
+
self._palette = bytearray(self.palette)
|
| 119 |
+
index = len(self.palette) // 3
|
| 120 |
+
special_colors: tuple[int | tuple[int, ...] | None, ...] = ()
|
| 121 |
+
if image:
|
| 122 |
+
special_colors = (
|
| 123 |
+
image.info.get("background"),
|
| 124 |
+
image.info.get("transparency"),
|
| 125 |
+
)
|
| 126 |
+
while index in special_colors:
|
| 127 |
+
index += 1
|
| 128 |
+
if index >= 256:
|
| 129 |
+
if image:
|
| 130 |
+
# Search for an unused index
|
| 131 |
+
for i, count in reversed(list(enumerate(image.histogram()))):
|
| 132 |
+
if count == 0 and i not in special_colors:
|
| 133 |
+
index = i
|
| 134 |
+
break
|
| 135 |
+
if index >= 256:
|
| 136 |
+
msg = "cannot allocate more than 256 colors"
|
| 137 |
+
raise ValueError(msg) from e
|
| 138 |
+
return index
|
| 139 |
+
|
| 140 |
+
def getcolor(
|
| 141 |
+
self,
|
| 142 |
+
color: tuple[int, ...],
|
| 143 |
+
image: Image.Image | None = None,
|
| 144 |
+
) -> int:
|
| 145 |
+
"""Given an rgb tuple, allocate palette entry.
|
| 146 |
+
|
| 147 |
+
.. warning:: This method is experimental.
|
| 148 |
+
"""
|
| 149 |
+
if self.rawmode:
|
| 150 |
+
msg = "palette contains raw palette data"
|
| 151 |
+
raise ValueError(msg)
|
| 152 |
+
if isinstance(color, tuple):
|
| 153 |
+
if self.mode == "RGB":
|
| 154 |
+
if len(color) == 4:
|
| 155 |
+
if color[3] != 255:
|
| 156 |
+
msg = "cannot add non-opaque RGBA color to RGB palette"
|
| 157 |
+
raise ValueError(msg)
|
| 158 |
+
color = color[:3]
|
| 159 |
+
elif self.mode == "RGBA":
|
| 160 |
+
if len(color) == 3:
|
| 161 |
+
color += (255,)
|
| 162 |
+
try:
|
| 163 |
+
return self.colors[color]
|
| 164 |
+
except KeyError as e:
|
| 165 |
+
# allocate new color slot
|
| 166 |
+
index = self._new_color_index(image, e)
|
| 167 |
+
assert isinstance(self._palette, bytearray)
|
| 168 |
+
self.colors[color] = index
|
| 169 |
+
if index * 3 < len(self.palette):
|
| 170 |
+
self._palette = (
|
| 171 |
+
self._palette[: index * 3]
|
| 172 |
+
+ bytes(color)
|
| 173 |
+
+ self._palette[index * 3 + 3 :]
|
| 174 |
+
)
|
| 175 |
+
else:
|
| 176 |
+
self._palette += bytes(color)
|
| 177 |
+
self.dirty = 1
|
| 178 |
+
return index
|
| 179 |
+
else:
|
| 180 |
+
msg = f"unknown color specifier: {repr(color)}" # type: ignore[unreachable]
|
| 181 |
+
raise ValueError(msg)
|
| 182 |
+
|
| 183 |
+
def save(self, fp: str | IO[str]) -> None:
|
| 184 |
+
"""Save palette to text file.
|
| 185 |
+
|
| 186 |
+
.. warning:: This method is experimental.
|
| 187 |
+
"""
|
| 188 |
+
if self.rawmode:
|
| 189 |
+
msg = "palette contains raw palette data"
|
| 190 |
+
raise ValueError(msg)
|
| 191 |
+
if isinstance(fp, str):
|
| 192 |
+
fp = open(fp, "w")
|
| 193 |
+
fp.write("# Palette\n")
|
| 194 |
+
fp.write(f"# Mode: {self.mode}\n")
|
| 195 |
+
for i in range(256):
|
| 196 |
+
fp.write(f"{i}")
|
| 197 |
+
for j in range(i * len(self.mode), (i + 1) * len(self.mode)):
|
| 198 |
+
try:
|
| 199 |
+
fp.write(f" {self.palette[j]}")
|
| 200 |
+
except IndexError:
|
| 201 |
+
fp.write(" 0")
|
| 202 |
+
fp.write("\n")
|
| 203 |
+
fp.close()
|
| 204 |
+
|
| 205 |
+
|
| 206 |
+
# --------------------------------------------------------------------
|
| 207 |
+
# Internal
|
| 208 |
+
|
| 209 |
+
|
| 210 |
+
def raw(rawmode, data: Sequence[int] | bytes | bytearray) -> ImagePalette:
|
| 211 |
+
palette = ImagePalette()
|
| 212 |
+
palette.rawmode = rawmode
|
| 213 |
+
palette.palette = data
|
| 214 |
+
palette.dirty = 1
|
| 215 |
+
return palette
|
| 216 |
+
|
| 217 |
+
|
| 218 |
+
# --------------------------------------------------------------------
|
| 219 |
+
# Factories
|
| 220 |
+
|
| 221 |
+
|
| 222 |
+
def make_linear_lut(black: int, white: float) -> list[int]:
|
| 223 |
+
if black == 0:
|
| 224 |
+
return [int(white * i // 255) for i in range(256)]
|
| 225 |
+
|
| 226 |
+
msg = "unavailable when black is non-zero"
|
| 227 |
+
raise NotImplementedError(msg) # FIXME
|
| 228 |
+
|
| 229 |
+
|
| 230 |
+
def make_gamma_lut(exp: float) -> list[int]:
|
| 231 |
+
return [int(((i / 255.0) ** exp) * 255.0 + 0.5) for i in range(256)]
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
def negative(mode: str = "RGB") -> ImagePalette:
|
| 235 |
+
palette = list(range(256 * len(mode)))
|
| 236 |
+
palette.reverse()
|
| 237 |
+
return ImagePalette(mode, [i // len(mode) for i in palette])
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
def random(mode: str = "RGB") -> ImagePalette:
|
| 241 |
+
from random import randint
|
| 242 |
+
|
| 243 |
+
palette = [randint(0, 255) for _ in range(256 * len(mode))]
|
| 244 |
+
return ImagePalette(mode, palette)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
def sepia(white: str = "#fff0c0") -> ImagePalette:
|
| 248 |
+
bands = [make_linear_lut(0, band) for band in ImageColor.getrgb(white)]
|
| 249 |
+
return ImagePalette("RGB", [bands[i % 3][i // 3] for i in range(256 * 3)])
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def wedge(mode: str = "RGB") -> ImagePalette:
|
| 253 |
+
palette = list(range(256 * len(mode)))
|
| 254 |
+
return ImagePalette(mode, [i // len(mode) for i in palette])
|
| 255 |
+
|
| 256 |
+
|
| 257 |
+
def load(filename: str) -> tuple[bytes, str]:
|
| 258 |
+
# FIXME: supports GIMP gradients only
|
| 259 |
+
|
| 260 |
+
with open(filename, "rb") as fp:
|
| 261 |
+
paletteHandlers: list[
|
| 262 |
+
type[
|
| 263 |
+
GimpPaletteFile.GimpPaletteFile
|
| 264 |
+
| GimpGradientFile.GimpGradientFile
|
| 265 |
+
| PaletteFile.PaletteFile
|
| 266 |
+
]
|
| 267 |
+
] = [
|
| 268 |
+
GimpPaletteFile.GimpPaletteFile,
|
| 269 |
+
GimpGradientFile.GimpGradientFile,
|
| 270 |
+
PaletteFile.PaletteFile,
|
| 271 |
+
]
|
| 272 |
+
for paletteHandler in paletteHandlers:
|
| 273 |
+
try:
|
| 274 |
+
fp.seek(0)
|
| 275 |
+
lut = paletteHandler(fp).getpalette()
|
| 276 |
+
if lut:
|
| 277 |
+
break
|
| 278 |
+
except (SyntaxError, ValueError):
|
| 279 |
+
pass
|
| 280 |
+
else:
|
| 281 |
+
msg = "cannot load palette"
|
| 282 |
+
raise OSError(msg)
|
| 283 |
+
|
| 284 |
+
return lut # data, rawmode
|
.venv/lib/python3.11/site-packages/PIL/ImageQt.py
ADDED
|
@@ -0,0 +1,205 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# a simple Qt image interface.
|
| 6 |
+
#
|
| 7 |
+
# history:
|
| 8 |
+
# 2006-06-03 fl: created
|
| 9 |
+
# 2006-06-04 fl: inherit from QImage instead of wrapping it
|
| 10 |
+
# 2006-06-05 fl: removed toimage helper; move string support to ImageQt
|
| 11 |
+
# 2013-11-13 fl: add support for Qt5 (aurelien.ballier@cyclonit.com)
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) 2006 by Secret Labs AB
|
| 14 |
+
# Copyright (c) 2006 by Fredrik Lundh
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import sys
|
| 21 |
+
from io import BytesIO
|
| 22 |
+
from typing import Callable
|
| 23 |
+
|
| 24 |
+
from . import Image
|
| 25 |
+
from ._util import is_path
|
| 26 |
+
|
| 27 |
+
qt_version: str | None
|
| 28 |
+
qt_versions = [
|
| 29 |
+
["6", "PyQt6"],
|
| 30 |
+
["side6", "PySide6"],
|
| 31 |
+
]
|
| 32 |
+
|
| 33 |
+
# If a version has already been imported, attempt it first
|
| 34 |
+
qt_versions.sort(key=lambda version: version[1] in sys.modules, reverse=True)
|
| 35 |
+
for version, qt_module in qt_versions:
|
| 36 |
+
try:
|
| 37 |
+
QBuffer: type
|
| 38 |
+
QIODevice: type
|
| 39 |
+
QImage: type
|
| 40 |
+
QPixmap: type
|
| 41 |
+
qRgba: Callable[[int, int, int, int], int]
|
| 42 |
+
if qt_module == "PyQt6":
|
| 43 |
+
from PyQt6.QtCore import QBuffer, QIODevice
|
| 44 |
+
from PyQt6.QtGui import QImage, QPixmap, qRgba
|
| 45 |
+
elif qt_module == "PySide6":
|
| 46 |
+
from PySide6.QtCore import QBuffer, QIODevice
|
| 47 |
+
from PySide6.QtGui import QImage, QPixmap, qRgba
|
| 48 |
+
except (ImportError, RuntimeError):
|
| 49 |
+
continue
|
| 50 |
+
qt_is_installed = True
|
| 51 |
+
qt_version = version
|
| 52 |
+
break
|
| 53 |
+
else:
|
| 54 |
+
qt_is_installed = False
|
| 55 |
+
qt_version = None
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def rgb(r, g, b, a=255):
|
| 59 |
+
"""(Internal) Turns an RGB color into a Qt compatible color integer."""
|
| 60 |
+
# use qRgb to pack the colors, and then turn the resulting long
|
| 61 |
+
# into a negative integer with the same bitpattern.
|
| 62 |
+
return qRgba(r, g, b, a) & 0xFFFFFFFF
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def fromqimage(im):
|
| 66 |
+
"""
|
| 67 |
+
:param im: QImage or PIL ImageQt object
|
| 68 |
+
"""
|
| 69 |
+
buffer = QBuffer()
|
| 70 |
+
if qt_version == "6":
|
| 71 |
+
try:
|
| 72 |
+
qt_openmode = QIODevice.OpenModeFlag
|
| 73 |
+
except AttributeError:
|
| 74 |
+
qt_openmode = QIODevice.OpenMode
|
| 75 |
+
else:
|
| 76 |
+
qt_openmode = QIODevice
|
| 77 |
+
buffer.open(qt_openmode.ReadWrite)
|
| 78 |
+
# preserve alpha channel with png
|
| 79 |
+
# otherwise ppm is more friendly with Image.open
|
| 80 |
+
if im.hasAlphaChannel():
|
| 81 |
+
im.save(buffer, "png")
|
| 82 |
+
else:
|
| 83 |
+
im.save(buffer, "ppm")
|
| 84 |
+
|
| 85 |
+
b = BytesIO()
|
| 86 |
+
b.write(buffer.data())
|
| 87 |
+
buffer.close()
|
| 88 |
+
b.seek(0)
|
| 89 |
+
|
| 90 |
+
return Image.open(b)
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
def fromqpixmap(im):
|
| 94 |
+
return fromqimage(im)
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
def align8to32(bytes, width, mode):
|
| 98 |
+
"""
|
| 99 |
+
converts each scanline of data from 8 bit to 32 bit aligned
|
| 100 |
+
"""
|
| 101 |
+
|
| 102 |
+
bits_per_pixel = {"1": 1, "L": 8, "P": 8, "I;16": 16}[mode]
|
| 103 |
+
|
| 104 |
+
# calculate bytes per line and the extra padding if needed
|
| 105 |
+
bits_per_line = bits_per_pixel * width
|
| 106 |
+
full_bytes_per_line, remaining_bits_per_line = divmod(bits_per_line, 8)
|
| 107 |
+
bytes_per_line = full_bytes_per_line + (1 if remaining_bits_per_line else 0)
|
| 108 |
+
|
| 109 |
+
extra_padding = -bytes_per_line % 4
|
| 110 |
+
|
| 111 |
+
# already 32 bit aligned by luck
|
| 112 |
+
if not extra_padding:
|
| 113 |
+
return bytes
|
| 114 |
+
|
| 115 |
+
new_data = [
|
| 116 |
+
bytes[i * bytes_per_line : (i + 1) * bytes_per_line] + b"\x00" * extra_padding
|
| 117 |
+
for i in range(len(bytes) // bytes_per_line)
|
| 118 |
+
]
|
| 119 |
+
|
| 120 |
+
return b"".join(new_data)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
def _toqclass_helper(im):
|
| 124 |
+
data = None
|
| 125 |
+
colortable = None
|
| 126 |
+
exclusive_fp = False
|
| 127 |
+
|
| 128 |
+
# handle filename, if given instead of image name
|
| 129 |
+
if hasattr(im, "toUtf8"):
|
| 130 |
+
# FIXME - is this really the best way to do this?
|
| 131 |
+
im = str(im.toUtf8(), "utf-8")
|
| 132 |
+
if is_path(im):
|
| 133 |
+
im = Image.open(im)
|
| 134 |
+
exclusive_fp = True
|
| 135 |
+
|
| 136 |
+
qt_format = QImage.Format if qt_version == "6" else QImage
|
| 137 |
+
if im.mode == "1":
|
| 138 |
+
format = qt_format.Format_Mono
|
| 139 |
+
elif im.mode == "L":
|
| 140 |
+
format = qt_format.Format_Indexed8
|
| 141 |
+
colortable = [rgb(i, i, i) for i in range(256)]
|
| 142 |
+
elif im.mode == "P":
|
| 143 |
+
format = qt_format.Format_Indexed8
|
| 144 |
+
palette = im.getpalette()
|
| 145 |
+
colortable = [rgb(*palette[i : i + 3]) for i in range(0, len(palette), 3)]
|
| 146 |
+
elif im.mode == "RGB":
|
| 147 |
+
# Populate the 4th channel with 255
|
| 148 |
+
im = im.convert("RGBA")
|
| 149 |
+
|
| 150 |
+
data = im.tobytes("raw", "BGRA")
|
| 151 |
+
format = qt_format.Format_RGB32
|
| 152 |
+
elif im.mode == "RGBA":
|
| 153 |
+
data = im.tobytes("raw", "BGRA")
|
| 154 |
+
format = qt_format.Format_ARGB32
|
| 155 |
+
elif im.mode == "I;16":
|
| 156 |
+
im = im.point(lambda i: i * 256)
|
| 157 |
+
|
| 158 |
+
format = qt_format.Format_Grayscale16
|
| 159 |
+
else:
|
| 160 |
+
if exclusive_fp:
|
| 161 |
+
im.close()
|
| 162 |
+
msg = f"unsupported image mode {repr(im.mode)}"
|
| 163 |
+
raise ValueError(msg)
|
| 164 |
+
|
| 165 |
+
size = im.size
|
| 166 |
+
__data = data or align8to32(im.tobytes(), size[0], im.mode)
|
| 167 |
+
if exclusive_fp:
|
| 168 |
+
im.close()
|
| 169 |
+
return {"data": __data, "size": size, "format": format, "colortable": colortable}
|
| 170 |
+
|
| 171 |
+
|
| 172 |
+
if qt_is_installed:
|
| 173 |
+
|
| 174 |
+
class ImageQt(QImage):
|
| 175 |
+
def __init__(self, im):
|
| 176 |
+
"""
|
| 177 |
+
An PIL image wrapper for Qt. This is a subclass of PyQt's QImage
|
| 178 |
+
class.
|
| 179 |
+
|
| 180 |
+
:param im: A PIL Image object, or a file name (given either as
|
| 181 |
+
Python string or a PyQt string object).
|
| 182 |
+
"""
|
| 183 |
+
im_data = _toqclass_helper(im)
|
| 184 |
+
# must keep a reference, or Qt will crash!
|
| 185 |
+
# All QImage constructors that take data operate on an existing
|
| 186 |
+
# buffer, so this buffer has to hang on for the life of the image.
|
| 187 |
+
# Fixes https://github.com/python-pillow/Pillow/issues/1370
|
| 188 |
+
self.__data = im_data["data"]
|
| 189 |
+
super().__init__(
|
| 190 |
+
self.__data,
|
| 191 |
+
im_data["size"][0],
|
| 192 |
+
im_data["size"][1],
|
| 193 |
+
im_data["format"],
|
| 194 |
+
)
|
| 195 |
+
if im_data["colortable"]:
|
| 196 |
+
self.setColorTable(im_data["colortable"])
|
| 197 |
+
|
| 198 |
+
|
| 199 |
+
def toqimage(im) -> ImageQt:
|
| 200 |
+
return ImageQt(im)
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
def toqpixmap(im):
|
| 204 |
+
qimage = toqimage(im)
|
| 205 |
+
return QPixmap.fromImage(qimage)
|
.venv/lib/python3.11/site-packages/PIL/ImageSequence.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# sequence support classes
|
| 6 |
+
#
|
| 7 |
+
# history:
|
| 8 |
+
# 1997-02-20 fl Created
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) 1997 by Secret Labs AB.
|
| 11 |
+
# Copyright (c) 1997 by Fredrik Lundh.
|
| 12 |
+
#
|
| 13 |
+
# See the README file for information on usage and redistribution.
|
| 14 |
+
#
|
| 15 |
+
|
| 16 |
+
##
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
from typing import Callable
|
| 20 |
+
|
| 21 |
+
from . import Image
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
class Iterator:
|
| 25 |
+
"""
|
| 26 |
+
This class implements an iterator object that can be used to loop
|
| 27 |
+
over an image sequence.
|
| 28 |
+
|
| 29 |
+
You can use the ``[]`` operator to access elements by index. This operator
|
| 30 |
+
will raise an :py:exc:`IndexError` if you try to access a nonexistent
|
| 31 |
+
frame.
|
| 32 |
+
|
| 33 |
+
:param im: An image object.
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
def __init__(self, im: Image.Image):
|
| 37 |
+
if not hasattr(im, "seek"):
|
| 38 |
+
msg = "im must have seek method"
|
| 39 |
+
raise AttributeError(msg)
|
| 40 |
+
self.im = im
|
| 41 |
+
self.position = getattr(self.im, "_min_frame", 0)
|
| 42 |
+
|
| 43 |
+
def __getitem__(self, ix: int) -> Image.Image:
|
| 44 |
+
try:
|
| 45 |
+
self.im.seek(ix)
|
| 46 |
+
return self.im
|
| 47 |
+
except EOFError as e:
|
| 48 |
+
msg = "end of sequence"
|
| 49 |
+
raise IndexError(msg) from e
|
| 50 |
+
|
| 51 |
+
def __iter__(self) -> Iterator:
|
| 52 |
+
return self
|
| 53 |
+
|
| 54 |
+
def __next__(self) -> Image.Image:
|
| 55 |
+
try:
|
| 56 |
+
self.im.seek(self.position)
|
| 57 |
+
self.position += 1
|
| 58 |
+
return self.im
|
| 59 |
+
except EOFError as e:
|
| 60 |
+
msg = "end of sequence"
|
| 61 |
+
raise StopIteration(msg) from e
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def all_frames(
|
| 65 |
+
im: Image.Image | list[Image.Image],
|
| 66 |
+
func: Callable[[Image.Image], Image.Image] | None = None,
|
| 67 |
+
) -> list[Image.Image]:
|
| 68 |
+
"""
|
| 69 |
+
Applies a given function to all frames in an image or a list of images.
|
| 70 |
+
The frames are returned as a list of separate images.
|
| 71 |
+
|
| 72 |
+
:param im: An image, or a list of images.
|
| 73 |
+
:param func: The function to apply to all of the image frames.
|
| 74 |
+
:returns: A list of images.
|
| 75 |
+
"""
|
| 76 |
+
if not isinstance(im, list):
|
| 77 |
+
im = [im]
|
| 78 |
+
|
| 79 |
+
ims = []
|
| 80 |
+
for imSequence in im:
|
| 81 |
+
current = imSequence.tell()
|
| 82 |
+
|
| 83 |
+
ims += [im_frame.copy() for im_frame in Iterator(imSequence)]
|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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# IPTC/NAA file handling
|
| 6 |
+
#
|
| 7 |
+
# history:
|
| 8 |
+
# 1995-10-01 fl Created
|
| 9 |
+
# 1998-03-09 fl Cleaned up and added to PIL
|
| 10 |
+
# 2002-06-18 fl Added getiptcinfo helper
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) Secret Labs AB 1997-2002.
|
| 13 |
+
# Copyright (c) Fredrik Lundh 1995.
|
| 14 |
+
#
|
| 15 |
+
# See the README file for information on usage and redistribution.
|
| 16 |
+
#
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
from io import BytesIO
|
| 20 |
+
from typing import Sequence
|
| 21 |
+
|
| 22 |
+
from . import Image, ImageFile
|
| 23 |
+
from ._binary import i16be as i16
|
| 24 |
+
from ._binary import i32be as i32
|
| 25 |
+
from ._deprecate import deprecate
|
| 26 |
+
|
| 27 |
+
COMPRESSION = {1: "raw", 5: "jpeg"}
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
def __getattr__(name: str) -> bytes:
|
| 31 |
+
if name == "PAD":
|
| 32 |
+
deprecate("IptcImagePlugin.PAD", 12)
|
| 33 |
+
return b"\0\0\0\0"
|
| 34 |
+
msg = f"module '{__name__}' has no attribute '{name}'"
|
| 35 |
+
raise AttributeError(msg)
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
#
|
| 39 |
+
# Helpers
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def _i(c: bytes) -> int:
|
| 43 |
+
return i32((b"\0\0\0\0" + c)[-4:])
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def _i8(c: int | bytes) -> int:
|
| 47 |
+
return c if isinstance(c, int) else c[0]
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def i(c: bytes) -> int:
|
| 51 |
+
""".. deprecated:: 10.2.0"""
|
| 52 |
+
deprecate("IptcImagePlugin.i", 12)
|
| 53 |
+
return _i(c)
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def dump(c: Sequence[int | bytes]) -> None:
|
| 57 |
+
""".. deprecated:: 10.2.0"""
|
| 58 |
+
deprecate("IptcImagePlugin.dump", 12)
|
| 59 |
+
for i in c:
|
| 60 |
+
print(f"{_i8(i):02x}", end=" ")
|
| 61 |
+
print()
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
##
|
| 65 |
+
# Image plugin for IPTC/NAA datastreams. To read IPTC/NAA fields
|
| 66 |
+
# from TIFF and JPEG files, use the <b>getiptcinfo</b> function.
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
class IptcImageFile(ImageFile.ImageFile):
|
| 70 |
+
format = "IPTC"
|
| 71 |
+
format_description = "IPTC/NAA"
|
| 72 |
+
|
| 73 |
+
def getint(self, key: tuple[int, int]) -> int:
|
| 74 |
+
return _i(self.info[key])
|
| 75 |
+
|
| 76 |
+
def field(self) -> tuple[tuple[int, int] | None, int]:
|
| 77 |
+
#
|
| 78 |
+
# get a IPTC field header
|
| 79 |
+
s = self.fp.read(5)
|
| 80 |
+
if not s.strip(b"\x00"):
|
| 81 |
+
return None, 0
|
| 82 |
+
|
| 83 |
+
tag = s[1], s[2]
|
| 84 |
+
|
| 85 |
+
# syntax
|
| 86 |
+
if s[0] != 0x1C or tag[0] not in [1, 2, 3, 4, 5, 6, 7, 8, 9, 240]:
|
| 87 |
+
msg = "invalid IPTC/NAA file"
|
| 88 |
+
raise SyntaxError(msg)
|
| 89 |
+
|
| 90 |
+
# field size
|
| 91 |
+
size = s[3]
|
| 92 |
+
if size > 132:
|
| 93 |
+
msg = "illegal field length in IPTC/NAA file"
|
| 94 |
+
raise OSError(msg)
|
| 95 |
+
elif size == 128:
|
| 96 |
+
size = 0
|
| 97 |
+
elif size > 128:
|
| 98 |
+
size = _i(self.fp.read(size - 128))
|
| 99 |
+
else:
|
| 100 |
+
size = i16(s, 3)
|
| 101 |
+
|
| 102 |
+
return tag, size
|
| 103 |
+
|
| 104 |
+
def _open(self) -> None:
|
| 105 |
+
# load descriptive fields
|
| 106 |
+
while True:
|
| 107 |
+
offset = self.fp.tell()
|
| 108 |
+
tag, size = self.field()
|
| 109 |
+
if not tag or tag == (8, 10):
|
| 110 |
+
break
|
| 111 |
+
if size:
|
| 112 |
+
tagdata = self.fp.read(size)
|
| 113 |
+
else:
|
| 114 |
+
tagdata = None
|
| 115 |
+
if tag in self.info:
|
| 116 |
+
if isinstance(self.info[tag], list):
|
| 117 |
+
self.info[tag].append(tagdata)
|
| 118 |
+
else:
|
| 119 |
+
self.info[tag] = [self.info[tag], tagdata]
|
| 120 |
+
else:
|
| 121 |
+
self.info[tag] = tagdata
|
| 122 |
+
|
| 123 |
+
# mode
|
| 124 |
+
layers = self.info[(3, 60)][0]
|
| 125 |
+
component = self.info[(3, 60)][1]
|
| 126 |
+
if (3, 65) in self.info:
|
| 127 |
+
id = self.info[(3, 65)][0] - 1
|
| 128 |
+
else:
|
| 129 |
+
id = 0
|
| 130 |
+
if layers == 1 and not component:
|
| 131 |
+
self._mode = "L"
|
| 132 |
+
elif layers == 3 and component:
|
| 133 |
+
self._mode = "RGB"[id]
|
| 134 |
+
elif layers == 4 and component:
|
| 135 |
+
self._mode = "CMYK"[id]
|
| 136 |
+
|
| 137 |
+
# size
|
| 138 |
+
self._size = self.getint((3, 20)), self.getint((3, 30))
|
| 139 |
+
|
| 140 |
+
# compression
|
| 141 |
+
try:
|
| 142 |
+
compression = COMPRESSION[self.getint((3, 120))]
|
| 143 |
+
except KeyError as e:
|
| 144 |
+
msg = "Unknown IPTC image compression"
|
| 145 |
+
raise OSError(msg) from e
|
| 146 |
+
|
| 147 |
+
# tile
|
| 148 |
+
if tag == (8, 10):
|
| 149 |
+
self.tile = [("iptc", (0, 0) + self.size, offset, compression)]
|
| 150 |
+
|
| 151 |
+
def load(self):
|
| 152 |
+
if len(self.tile) != 1 or self.tile[0][0] != "iptc":
|
| 153 |
+
return ImageFile.ImageFile.load(self)
|
| 154 |
+
|
| 155 |
+
offset, compression = self.tile[0][2:]
|
| 156 |
+
|
| 157 |
+
self.fp.seek(offset)
|
| 158 |
+
|
| 159 |
+
# Copy image data to temporary file
|
| 160 |
+
o = BytesIO()
|
| 161 |
+
if compression == "raw":
|
| 162 |
+
# To simplify access to the extracted file,
|
| 163 |
+
# prepend a PPM header
|
| 164 |
+
o.write(b"P5\n%d %d\n255\n" % self.size)
|
| 165 |
+
while True:
|
| 166 |
+
type, size = self.field()
|
| 167 |
+
if type != (8, 10):
|
| 168 |
+
break
|
| 169 |
+
while size > 0:
|
| 170 |
+
s = self.fp.read(min(size, 8192))
|
| 171 |
+
if not s:
|
| 172 |
+
break
|
| 173 |
+
o.write(s)
|
| 174 |
+
size -= len(s)
|
| 175 |
+
|
| 176 |
+
with Image.open(o) as _im:
|
| 177 |
+
_im.load()
|
| 178 |
+
self.im = _im.im
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
Image.register_open(IptcImageFile.format, IptcImageFile)
|
| 182 |
+
|
| 183 |
+
Image.register_extension(IptcImageFile.format, ".iim")
|
| 184 |
+
|
| 185 |
+
|
| 186 |
+
def getiptcinfo(im):
|
| 187 |
+
"""
|
| 188 |
+
Get IPTC information from TIFF, JPEG, or IPTC file.
|
| 189 |
+
|
| 190 |
+
:param im: An image containing IPTC data.
|
| 191 |
+
:returns: A dictionary containing IPTC information, or None if
|
| 192 |
+
no IPTC information block was found.
|
| 193 |
+
"""
|
| 194 |
+
from . import JpegImagePlugin, TiffImagePlugin
|
| 195 |
+
|
| 196 |
+
data = None
|
| 197 |
+
|
| 198 |
+
if isinstance(im, IptcImageFile):
|
| 199 |
+
# return info dictionary right away
|
| 200 |
+
return im.info
|
| 201 |
+
|
| 202 |
+
elif isinstance(im, JpegImagePlugin.JpegImageFile):
|
| 203 |
+
# extract the IPTC/NAA resource
|
| 204 |
+
photoshop = im.info.get("photoshop")
|
| 205 |
+
if photoshop:
|
| 206 |
+
data = photoshop.get(0x0404)
|
| 207 |
+
|
| 208 |
+
elif isinstance(im, TiffImagePlugin.TiffImageFile):
|
| 209 |
+
# get raw data from the IPTC/NAA tag (PhotoShop tags the data
|
| 210 |
+
# as 4-byte integers, so we cannot use the get method...)
|
| 211 |
+
try:
|
| 212 |
+
data = im.tag.tagdata[TiffImagePlugin.IPTC_NAA_CHUNK]
|
| 213 |
+
except (AttributeError, KeyError):
|
| 214 |
+
pass
|
| 215 |
+
|
| 216 |
+
if data is None:
|
| 217 |
+
return None # no properties
|
| 218 |
+
|
| 219 |
+
# create an IptcImagePlugin object without initializing it
|
| 220 |
+
class FakeImage:
|
| 221 |
+
pass
|
| 222 |
+
|
| 223 |
+
im = FakeImage()
|
| 224 |
+
im.__class__ = IptcImageFile
|
| 225 |
+
|
| 226 |
+
# parse the IPTC information chunk
|
| 227 |
+
im.info = {}
|
| 228 |
+
im.fp = BytesIO(data)
|
| 229 |
+
|
| 230 |
+
try:
|
| 231 |
+
im._open()
|
| 232 |
+
except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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# JPEG (JFIF) file handling
|
| 6 |
+
#
|
| 7 |
+
# See "Digital Compression and Coding of Continuous-Tone Still Images,
|
| 8 |
+
# Part 1, Requirements and Guidelines" (CCITT T.81 / ISO 10918-1)
|
| 9 |
+
#
|
| 10 |
+
# History:
|
| 11 |
+
# 1995-09-09 fl Created
|
| 12 |
+
# 1995-09-13 fl Added full parser
|
| 13 |
+
# 1996-03-25 fl Added hack to use the IJG command line utilities
|
| 14 |
+
# 1996-05-05 fl Workaround Photoshop 2.5 CMYK polarity bug
|
| 15 |
+
# 1996-05-28 fl Added draft support, JFIF version (0.1)
|
| 16 |
+
# 1996-12-30 fl Added encoder options, added progression property (0.2)
|
| 17 |
+
# 1997-08-27 fl Save mode 1 images as BW (0.3)
|
| 18 |
+
# 1998-07-12 fl Added YCbCr to draft and save methods (0.4)
|
| 19 |
+
# 1998-10-19 fl Don't hang on files using 16-bit DQT's (0.4.1)
|
| 20 |
+
# 2001-04-16 fl Extract DPI settings from JFIF files (0.4.2)
|
| 21 |
+
# 2002-07-01 fl Skip pad bytes before markers; identify Exif files (0.4.3)
|
| 22 |
+
# 2003-04-25 fl Added experimental EXIF decoder (0.5)
|
| 23 |
+
# 2003-06-06 fl Added experimental EXIF GPSinfo decoder
|
| 24 |
+
# 2003-09-13 fl Extract COM markers
|
| 25 |
+
# 2009-09-06 fl Added icc_profile support (from Florian Hoech)
|
| 26 |
+
# 2009-03-06 fl Changed CMYK handling; always use Adobe polarity (0.6)
|
| 27 |
+
# 2009-03-08 fl Added subsampling support (from Justin Huff).
|
| 28 |
+
#
|
| 29 |
+
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 30 |
+
# Copyright (c) 1995-1996 by Fredrik Lundh.
|
| 31 |
+
#
|
| 32 |
+
# See the README file for information on usage and redistribution.
|
| 33 |
+
#
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
import array
|
| 37 |
+
import io
|
| 38 |
+
import math
|
| 39 |
+
import os
|
| 40 |
+
import struct
|
| 41 |
+
import subprocess
|
| 42 |
+
import sys
|
| 43 |
+
import tempfile
|
| 44 |
+
import warnings
|
| 45 |
+
from typing import IO, Any
|
| 46 |
+
|
| 47 |
+
from . import Image, ImageFile
|
| 48 |
+
from ._binary import i16be as i16
|
| 49 |
+
from ._binary import i32be as i32
|
| 50 |
+
from ._binary import o8
|
| 51 |
+
from ._binary import o16be as o16
|
| 52 |
+
from .JpegPresets import presets
|
| 53 |
+
|
| 54 |
+
#
|
| 55 |
+
# Parser
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
def Skip(self: JpegImageFile, marker: int) -> None:
|
| 59 |
+
n = i16(self.fp.read(2)) - 2
|
| 60 |
+
ImageFile._safe_read(self.fp, n)
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def APP(self, marker):
|
| 64 |
+
#
|
| 65 |
+
# Application marker. Store these in the APP dictionary.
|
| 66 |
+
# Also look for well-known application markers.
|
| 67 |
+
|
| 68 |
+
n = i16(self.fp.read(2)) - 2
|
| 69 |
+
s = ImageFile._safe_read(self.fp, n)
|
| 70 |
+
|
| 71 |
+
app = "APP%d" % (marker & 15)
|
| 72 |
+
|
| 73 |
+
self.app[app] = s # compatibility
|
| 74 |
+
self.applist.append((app, s))
|
| 75 |
+
|
| 76 |
+
if marker == 0xFFE0 and s[:4] == b"JFIF":
|
| 77 |
+
# extract JFIF information
|
| 78 |
+
self.info["jfif"] = version = i16(s, 5) # version
|
| 79 |
+
self.info["jfif_version"] = divmod(version, 256)
|
| 80 |
+
# extract JFIF properties
|
| 81 |
+
try:
|
| 82 |
+
jfif_unit = s[7]
|
| 83 |
+
jfif_density = i16(s, 8), i16(s, 10)
|
| 84 |
+
except Exception:
|
| 85 |
+
pass
|
| 86 |
+
else:
|
| 87 |
+
if jfif_unit == 1:
|
| 88 |
+
self.info["dpi"] = jfif_density
|
| 89 |
+
self.info["jfif_unit"] = jfif_unit
|
| 90 |
+
self.info["jfif_density"] = jfif_density
|
| 91 |
+
elif marker == 0xFFE1 and s[:6] == b"Exif\0\0":
|
| 92 |
+
# extract EXIF information
|
| 93 |
+
if "exif" in self.info:
|
| 94 |
+
self.info["exif"] += s[6:]
|
| 95 |
+
else:
|
| 96 |
+
self.info["exif"] = s
|
| 97 |
+
self._exif_offset = self.fp.tell() - n + 6
|
| 98 |
+
elif marker == 0xFFE1 and s[:29] == b"http://ns.adobe.com/xap/1.0/\x00":
|
| 99 |
+
self.info["xmp"] = s.split(b"\x00", 1)[1]
|
| 100 |
+
elif marker == 0xFFE2 and s[:5] == b"FPXR\0":
|
| 101 |
+
# extract FlashPix information (incomplete)
|
| 102 |
+
self.info["flashpix"] = s # FIXME: value will change
|
| 103 |
+
elif marker == 0xFFE2 and s[:12] == b"ICC_PROFILE\0":
|
| 104 |
+
# Since an ICC profile can be larger than the maximum size of
|
| 105 |
+
# a JPEG marker (64K), we need provisions to split it into
|
| 106 |
+
# multiple markers. The format defined by the ICC specifies
|
| 107 |
+
# one or more APP2 markers containing the following data:
|
| 108 |
+
# Identifying string ASCII "ICC_PROFILE\0" (12 bytes)
|
| 109 |
+
# Marker sequence number 1, 2, etc (1 byte)
|
| 110 |
+
# Number of markers Total of APP2's used (1 byte)
|
| 111 |
+
# Profile data (remainder of APP2 data)
|
| 112 |
+
# Decoders should use the marker sequence numbers to
|
| 113 |
+
# reassemble the profile, rather than assuming that the APP2
|
| 114 |
+
# markers appear in the correct sequence.
|
| 115 |
+
self.icclist.append(s)
|
| 116 |
+
elif marker == 0xFFED and s[:14] == b"Photoshop 3.0\x00":
|
| 117 |
+
# parse the image resource block
|
| 118 |
+
offset = 14
|
| 119 |
+
photoshop = self.info.setdefault("photoshop", {})
|
| 120 |
+
while s[offset : offset + 4] == b"8BIM":
|
| 121 |
+
try:
|
| 122 |
+
offset += 4
|
| 123 |
+
# resource code
|
| 124 |
+
code = i16(s, offset)
|
| 125 |
+
offset += 2
|
| 126 |
+
# resource name (usually empty)
|
| 127 |
+
name_len = s[offset]
|
| 128 |
+
# name = s[offset+1:offset+1+name_len]
|
| 129 |
+
offset += 1 + name_len
|
| 130 |
+
offset += offset & 1 # align
|
| 131 |
+
# resource data block
|
| 132 |
+
size = i32(s, offset)
|
| 133 |
+
offset += 4
|
| 134 |
+
data = s[offset : offset + size]
|
| 135 |
+
if code == 0x03ED: # ResolutionInfo
|
| 136 |
+
data = {
|
| 137 |
+
"XResolution": i32(data, 0) / 65536,
|
| 138 |
+
"DisplayedUnitsX": i16(data, 4),
|
| 139 |
+
"YResolution": i32(data, 8) / 65536,
|
| 140 |
+
"DisplayedUnitsY": i16(data, 12),
|
| 141 |
+
}
|
| 142 |
+
photoshop[code] = data
|
| 143 |
+
offset += size
|
| 144 |
+
offset += offset & 1 # align
|
| 145 |
+
except struct.error:
|
| 146 |
+
break # insufficient data
|
| 147 |
+
|
| 148 |
+
elif marker == 0xFFEE and s[:5] == b"Adobe":
|
| 149 |
+
self.info["adobe"] = i16(s, 5)
|
| 150 |
+
# extract Adobe custom properties
|
| 151 |
+
try:
|
| 152 |
+
adobe_transform = s[11]
|
| 153 |
+
except IndexError:
|
| 154 |
+
pass
|
| 155 |
+
else:
|
| 156 |
+
self.info["adobe_transform"] = adobe_transform
|
| 157 |
+
elif marker == 0xFFE2 and s[:4] == b"MPF\0":
|
| 158 |
+
# extract MPO information
|
| 159 |
+
self.info["mp"] = s[4:]
|
| 160 |
+
# offset is current location minus buffer size
|
| 161 |
+
# plus constant header size
|
| 162 |
+
self.info["mpoffset"] = self.fp.tell() - n + 4
|
| 163 |
+
|
| 164 |
+
|
| 165 |
+
def COM(self: JpegImageFile, marker: int) -> None:
|
| 166 |
+
#
|
| 167 |
+
# Comment marker. Store these in the APP dictionary.
|
| 168 |
+
n = i16(self.fp.read(2)) - 2
|
| 169 |
+
s = ImageFile._safe_read(self.fp, n)
|
| 170 |
+
|
| 171 |
+
self.info["comment"] = s
|
| 172 |
+
self.app["COM"] = s # compatibility
|
| 173 |
+
self.applist.append(("COM", s))
|
| 174 |
+
|
| 175 |
+
|
| 176 |
+
def SOF(self: JpegImageFile, marker: int) -> None:
|
| 177 |
+
#
|
| 178 |
+
# Start of frame marker. Defines the size and mode of the
|
| 179 |
+
# image. JPEG is colour blind, so we use some simple
|
| 180 |
+
# heuristics to map the number of layers to an appropriate
|
| 181 |
+
# mode. Note that this could be made a bit brighter, by
|
| 182 |
+
# looking for JFIF and Adobe APP markers.
|
| 183 |
+
|
| 184 |
+
n = i16(self.fp.read(2)) - 2
|
| 185 |
+
s = ImageFile._safe_read(self.fp, n)
|
| 186 |
+
self._size = i16(s, 3), i16(s, 1)
|
| 187 |
+
|
| 188 |
+
self.bits = s[0]
|
| 189 |
+
if self.bits != 8:
|
| 190 |
+
msg = f"cannot handle {self.bits}-bit layers"
|
| 191 |
+
raise SyntaxError(msg)
|
| 192 |
+
|
| 193 |
+
self.layers = s[5]
|
| 194 |
+
if self.layers == 1:
|
| 195 |
+
self._mode = "L"
|
| 196 |
+
elif self.layers == 3:
|
| 197 |
+
self._mode = "RGB"
|
| 198 |
+
elif self.layers == 4:
|
| 199 |
+
self._mode = "CMYK"
|
| 200 |
+
else:
|
| 201 |
+
msg = f"cannot handle {self.layers}-layer images"
|
| 202 |
+
raise SyntaxError(msg)
|
| 203 |
+
|
| 204 |
+
if marker in [0xFFC2, 0xFFC6, 0xFFCA, 0xFFCE]:
|
| 205 |
+
self.info["progressive"] = self.info["progression"] = 1
|
| 206 |
+
|
| 207 |
+
if self.icclist:
|
| 208 |
+
# fixup icc profile
|
| 209 |
+
self.icclist.sort() # sort by sequence number
|
| 210 |
+
if self.icclist[0][13] == len(self.icclist):
|
| 211 |
+
profile = [p[14:] for p in self.icclist]
|
| 212 |
+
icc_profile = b"".join(profile)
|
| 213 |
+
else:
|
| 214 |
+
icc_profile = None # wrong number of fragments
|
| 215 |
+
self.info["icc_profile"] = icc_profile
|
| 216 |
+
self.icclist = []
|
| 217 |
+
|
| 218 |
+
for i in range(6, len(s), 3):
|
| 219 |
+
t = s[i : i + 3]
|
| 220 |
+
# 4-tuples: id, vsamp, hsamp, qtable
|
| 221 |
+
self.layer.append((t[0], t[1] // 16, t[1] & 15, t[2]))
|
| 222 |
+
|
| 223 |
+
|
| 224 |
+
def DQT(self: JpegImageFile, marker: int) -> None:
|
| 225 |
+
#
|
| 226 |
+
# Define quantization table. Note that there might be more
|
| 227 |
+
# than one table in each marker.
|
| 228 |
+
|
| 229 |
+
# FIXME: The quantization tables can be used to estimate the
|
| 230 |
+
# compression quality.
|
| 231 |
+
|
| 232 |
+
n = i16(self.fp.read(2)) - 2
|
| 233 |
+
s = ImageFile._safe_read(self.fp, n)
|
| 234 |
+
while len(s):
|
| 235 |
+
v = s[0]
|
| 236 |
+
precision = 1 if (v // 16 == 0) else 2 # in bytes
|
| 237 |
+
qt_length = 1 + precision * 64
|
| 238 |
+
if len(s) < qt_length:
|
| 239 |
+
msg = "bad quantization table marker"
|
| 240 |
+
raise SyntaxError(msg)
|
| 241 |
+
data = array.array("B" if precision == 1 else "H", s[1:qt_length])
|
| 242 |
+
if sys.byteorder == "little" and precision > 1:
|
| 243 |
+
data.byteswap() # the values are always big-endian
|
| 244 |
+
self.quantization[v & 15] = [data[i] for i in zigzag_index]
|
| 245 |
+
s = s[qt_length:]
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
#
|
| 249 |
+
# JPEG marker table
|
| 250 |
+
|
| 251 |
+
MARKER = {
|
| 252 |
+
0xFFC0: ("SOF0", "Baseline DCT", SOF),
|
| 253 |
+
0xFFC1: ("SOF1", "Extended Sequential DCT", SOF),
|
| 254 |
+
0xFFC2: ("SOF2", "Progressive DCT", SOF),
|
| 255 |
+
0xFFC3: ("SOF3", "Spatial lossless", SOF),
|
| 256 |
+
0xFFC4: ("DHT", "Define Huffman table", Skip),
|
| 257 |
+
0xFFC5: ("SOF5", "Differential sequential DCT", SOF),
|
| 258 |
+
0xFFC6: ("SOF6", "Differential progressive DCT", SOF),
|
| 259 |
+
0xFFC7: ("SOF7", "Differential spatial", SOF),
|
| 260 |
+
0xFFC8: ("JPG", "Extension", None),
|
| 261 |
+
0xFFC9: ("SOF9", "Extended sequential DCT (AC)", SOF),
|
| 262 |
+
0xFFCA: ("SOF10", "Progressive DCT (AC)", SOF),
|
| 263 |
+
0xFFCB: ("SOF11", "Spatial lossless DCT (AC)", SOF),
|
| 264 |
+
0xFFCC: ("DAC", "Define arithmetic coding conditioning", Skip),
|
| 265 |
+
0xFFCD: ("SOF13", "Differential sequential DCT (AC)", SOF),
|
| 266 |
+
0xFFCE: ("SOF14", "Differential progressive DCT (AC)", SOF),
|
| 267 |
+
0xFFCF: ("SOF15", "Differential spatial (AC)", SOF),
|
| 268 |
+
0xFFD0: ("RST0", "Restart 0", None),
|
| 269 |
+
0xFFD1: ("RST1", "Restart 1", None),
|
| 270 |
+
0xFFD2: ("RST2", "Restart 2", None),
|
| 271 |
+
0xFFD3: ("RST3", "Restart 3", None),
|
| 272 |
+
0xFFD4: ("RST4", "Restart 4", None),
|
| 273 |
+
0xFFD5: ("RST5", "Restart 5", None),
|
| 274 |
+
0xFFD6: ("RST6", "Restart 6", None),
|
| 275 |
+
0xFFD7: ("RST7", "Restart 7", None),
|
| 276 |
+
0xFFD8: ("SOI", "Start of image", None),
|
| 277 |
+
0xFFD9: ("EOI", "End of image", None),
|
| 278 |
+
0xFFDA: ("SOS", "Start of scan", Skip),
|
| 279 |
+
0xFFDB: ("DQT", "Define quantization table", DQT),
|
| 280 |
+
0xFFDC: ("DNL", "Define number of lines", Skip),
|
| 281 |
+
0xFFDD: ("DRI", "Define restart interval", Skip),
|
| 282 |
+
0xFFDE: ("DHP", "Define hierarchical progression", SOF),
|
| 283 |
+
0xFFDF: ("EXP", "Expand reference component", Skip),
|
| 284 |
+
0xFFE0: ("APP0", "Application segment 0", APP),
|
| 285 |
+
0xFFE1: ("APP1", "Application segment 1", APP),
|
| 286 |
+
0xFFE2: ("APP2", "Application segment 2", APP),
|
| 287 |
+
0xFFE3: ("APP3", "Application segment 3", APP),
|
| 288 |
+
0xFFE4: ("APP4", "Application segment 4", APP),
|
| 289 |
+
0xFFE5: ("APP5", "Application segment 5", APP),
|
| 290 |
+
0xFFE6: ("APP6", "Application segment 6", APP),
|
| 291 |
+
0xFFE7: ("APP7", "Application segment 7", APP),
|
| 292 |
+
0xFFE8: ("APP8", "Application segment 8", APP),
|
| 293 |
+
0xFFE9: ("APP9", "Application segment 9", APP),
|
| 294 |
+
0xFFEA: ("APP10", "Application segment 10", APP),
|
| 295 |
+
0xFFEB: ("APP11", "Application segment 11", APP),
|
| 296 |
+
0xFFEC: ("APP12", "Application segment 12", APP),
|
| 297 |
+
0xFFED: ("APP13", "Application segment 13", APP),
|
| 298 |
+
0xFFEE: ("APP14", "Application segment 14", APP),
|
| 299 |
+
0xFFEF: ("APP15", "Application segment 15", APP),
|
| 300 |
+
0xFFF0: ("JPG0", "Extension 0", None),
|
| 301 |
+
0xFFF1: ("JPG1", "Extension 1", None),
|
| 302 |
+
0xFFF2: ("JPG2", "Extension 2", None),
|
| 303 |
+
0xFFF3: ("JPG3", "Extension 3", None),
|
| 304 |
+
0xFFF4: ("JPG4", "Extension 4", None),
|
| 305 |
+
0xFFF5: ("JPG5", "Extension 5", None),
|
| 306 |
+
0xFFF6: ("JPG6", "Extension 6", None),
|
| 307 |
+
0xFFF7: ("JPG7", "Extension 7", None),
|
| 308 |
+
0xFFF8: ("JPG8", "Extension 8", None),
|
| 309 |
+
0xFFF9: ("JPG9", "Extension 9", None),
|
| 310 |
+
0xFFFA: ("JPG10", "Extension 10", None),
|
| 311 |
+
0xFFFB: ("JPG11", "Extension 11", None),
|
| 312 |
+
0xFFFC: ("JPG12", "Extension 12", None),
|
| 313 |
+
0xFFFD: ("JPG13", "Extension 13", None),
|
| 314 |
+
0xFFFE: ("COM", "Comment", COM),
|
| 315 |
+
}
|
| 316 |
+
|
| 317 |
+
|
| 318 |
+
def _accept(prefix: bytes) -> bool:
|
| 319 |
+
# Magic number was taken from https://en.wikipedia.org/wiki/JPEG
|
| 320 |
+
return prefix[:3] == b"\xFF\xD8\xFF"
|
| 321 |
+
|
| 322 |
+
|
| 323 |
+
##
|
| 324 |
+
# Image plugin for JPEG and JFIF images.
|
| 325 |
+
|
| 326 |
+
|
| 327 |
+
class JpegImageFile(ImageFile.ImageFile):
|
| 328 |
+
format = "JPEG"
|
| 329 |
+
format_description = "JPEG (ISO 10918)"
|
| 330 |
+
|
| 331 |
+
def _open(self):
|
| 332 |
+
s = self.fp.read(3)
|
| 333 |
+
|
| 334 |
+
if not _accept(s):
|
| 335 |
+
msg = "not a JPEG file"
|
| 336 |
+
raise SyntaxError(msg)
|
| 337 |
+
s = b"\xFF"
|
| 338 |
+
|
| 339 |
+
# Create attributes
|
| 340 |
+
self.bits = self.layers = 0
|
| 341 |
+
|
| 342 |
+
# JPEG specifics (internal)
|
| 343 |
+
self.layer = []
|
| 344 |
+
self.huffman_dc = {}
|
| 345 |
+
self.huffman_ac = {}
|
| 346 |
+
self.quantization = {}
|
| 347 |
+
self.app = {} # compatibility
|
| 348 |
+
self.applist = []
|
| 349 |
+
self.icclist = []
|
| 350 |
+
|
| 351 |
+
while True:
|
| 352 |
+
i = s[0]
|
| 353 |
+
if i == 0xFF:
|
| 354 |
+
s = s + self.fp.read(1)
|
| 355 |
+
i = i16(s)
|
| 356 |
+
else:
|
| 357 |
+
# Skip non-0xFF junk
|
| 358 |
+
s = self.fp.read(1)
|
| 359 |
+
continue
|
| 360 |
+
|
| 361 |
+
if i in MARKER:
|
| 362 |
+
name, description, handler = MARKER[i]
|
| 363 |
+
if handler is not None:
|
| 364 |
+
handler(self, i)
|
| 365 |
+
if i == 0xFFDA: # start of scan
|
| 366 |
+
rawmode = self.mode
|
| 367 |
+
if self.mode == "CMYK":
|
| 368 |
+
rawmode = "CMYK;I" # assume adobe conventions
|
| 369 |
+
self.tile = [("jpeg", (0, 0) + self.size, 0, (rawmode, ""))]
|
| 370 |
+
# self.__offset = self.fp.tell()
|
| 371 |
+
break
|
| 372 |
+
s = self.fp.read(1)
|
| 373 |
+
elif i in {0, 0xFFFF}:
|
| 374 |
+
# padded marker or junk; move on
|
| 375 |
+
s = b"\xff"
|
| 376 |
+
elif i == 0xFF00: # Skip extraneous data (escaped 0xFF)
|
| 377 |
+
s = self.fp.read(1)
|
| 378 |
+
else:
|
| 379 |
+
msg = "no marker found"
|
| 380 |
+
raise SyntaxError(msg)
|
| 381 |
+
|
| 382 |
+
self._read_dpi_from_exif()
|
| 383 |
+
|
| 384 |
+
def load_read(self, read_bytes: int) -> bytes:
|
| 385 |
+
"""
|
| 386 |
+
internal: read more image data
|
| 387 |
+
For premature EOF and LOAD_TRUNCATED_IMAGES adds EOI marker
|
| 388 |
+
so libjpeg can finish decoding
|
| 389 |
+
"""
|
| 390 |
+
s = self.fp.read(read_bytes)
|
| 391 |
+
|
| 392 |
+
if not s and ImageFile.LOAD_TRUNCATED_IMAGES and not hasattr(self, "_ended"):
|
| 393 |
+
# Premature EOF.
|
| 394 |
+
# Pretend file is finished adding EOI marker
|
| 395 |
+
self._ended = True
|
| 396 |
+
return b"\xFF\xD9"
|
| 397 |
+
|
| 398 |
+
return s
|
| 399 |
+
|
| 400 |
+
def draft(
|
| 401 |
+
self, mode: str | None, size: tuple[int, int] | None
|
| 402 |
+
) -> tuple[str, tuple[int, int, float, float]] | None:
|
| 403 |
+
if len(self.tile) != 1:
|
| 404 |
+
return None
|
| 405 |
+
|
| 406 |
+
# Protect from second call
|
| 407 |
+
if self.decoderconfig:
|
| 408 |
+
return None
|
| 409 |
+
|
| 410 |
+
d, e, o, a = self.tile[0]
|
| 411 |
+
scale = 1
|
| 412 |
+
original_size = self.size
|
| 413 |
+
|
| 414 |
+
if a[0] == "RGB" and mode in ["L", "YCbCr"]:
|
| 415 |
+
self._mode = mode
|
| 416 |
+
a = mode, ""
|
| 417 |
+
|
| 418 |
+
if size:
|
| 419 |
+
scale = min(self.size[0] // size[0], self.size[1] // size[1])
|
| 420 |
+
for s in [8, 4, 2, 1]:
|
| 421 |
+
if scale >= s:
|
| 422 |
+
break
|
| 423 |
+
e = (
|
| 424 |
+
e[0],
|
| 425 |
+
e[1],
|
| 426 |
+
(e[2] - e[0] + s - 1) // s + e[0],
|
| 427 |
+
(e[3] - e[1] + s - 1) // s + e[1],
|
| 428 |
+
)
|
| 429 |
+
self._size = ((self.size[0] + s - 1) // s, (self.size[1] + s - 1) // s)
|
| 430 |
+
scale = s
|
| 431 |
+
|
| 432 |
+
self.tile = [(d, e, o, a)]
|
| 433 |
+
self.decoderconfig = (scale, 0)
|
| 434 |
+
|
| 435 |
+
box = (0, 0, original_size[0] / scale, original_size[1] / scale)
|
| 436 |
+
return self.mode, box
|
| 437 |
+
|
| 438 |
+
def load_djpeg(self) -> None:
|
| 439 |
+
# ALTERNATIVE: handle JPEGs via the IJG command line utilities
|
| 440 |
+
|
| 441 |
+
f, path = tempfile.mkstemp()
|
| 442 |
+
os.close(f)
|
| 443 |
+
if os.path.exists(self.filename):
|
| 444 |
+
subprocess.check_call(["djpeg", "-outfile", path, self.filename])
|
| 445 |
+
else:
|
| 446 |
+
try:
|
| 447 |
+
os.unlink(path)
|
| 448 |
+
except OSError:
|
| 449 |
+
pass
|
| 450 |
+
|
| 451 |
+
msg = "Invalid Filename"
|
| 452 |
+
raise ValueError(msg)
|
| 453 |
+
|
| 454 |
+
try:
|
| 455 |
+
with Image.open(path) as _im:
|
| 456 |
+
_im.load()
|
| 457 |
+
self.im = _im.im
|
| 458 |
+
finally:
|
| 459 |
+
try:
|
| 460 |
+
os.unlink(path)
|
| 461 |
+
except OSError:
|
| 462 |
+
pass
|
| 463 |
+
|
| 464 |
+
self._mode = self.im.mode
|
| 465 |
+
self._size = self.im.size
|
| 466 |
+
|
| 467 |
+
self.tile = []
|
| 468 |
+
|
| 469 |
+
def _getexif(self) -> dict[str, Any] | None:
|
| 470 |
+
return _getexif(self)
|
| 471 |
+
|
| 472 |
+
def _read_dpi_from_exif(self) -> None:
|
| 473 |
+
# If DPI isn't in JPEG header, fetch from EXIF
|
| 474 |
+
if "dpi" in self.info or "exif" not in self.info:
|
| 475 |
+
return
|
| 476 |
+
try:
|
| 477 |
+
exif = self.getexif()
|
| 478 |
+
resolution_unit = exif[0x0128]
|
| 479 |
+
x_resolution = exif[0x011A]
|
| 480 |
+
try:
|
| 481 |
+
dpi = float(x_resolution[0]) / x_resolution[1]
|
| 482 |
+
except TypeError:
|
| 483 |
+
dpi = x_resolution
|
| 484 |
+
if math.isnan(dpi):
|
| 485 |
+
msg = "DPI is not a number"
|
| 486 |
+
raise ValueError(msg)
|
| 487 |
+
if resolution_unit == 3: # cm
|
| 488 |
+
# 1 dpcm = 2.54 dpi
|
| 489 |
+
dpi *= 2.54
|
| 490 |
+
self.info["dpi"] = dpi, dpi
|
| 491 |
+
except (
|
| 492 |
+
struct.error, # truncated EXIF
|
| 493 |
+
KeyError, # dpi not included
|
| 494 |
+
SyntaxError, # invalid/unreadable EXIF
|
| 495 |
+
TypeError, # dpi is an invalid float
|
| 496 |
+
ValueError, # dpi is an invalid float
|
| 497 |
+
ZeroDivisionError, # invalid dpi rational value
|
| 498 |
+
):
|
| 499 |
+
self.info["dpi"] = 72, 72
|
| 500 |
+
|
| 501 |
+
def _getmp(self):
|
| 502 |
+
return _getmp(self)
|
| 503 |
+
|
| 504 |
+
|
| 505 |
+
def _getexif(self) -> dict[str, Any] | None:
|
| 506 |
+
if "exif" not in self.info:
|
| 507 |
+
return None
|
| 508 |
+
return self.getexif()._get_merged_dict()
|
| 509 |
+
|
| 510 |
+
|
| 511 |
+
def _getmp(self):
|
| 512 |
+
# Extract MP information. This method was inspired by the "highly
|
| 513 |
+
# experimental" _getexif version that's been in use for years now,
|
| 514 |
+
# itself based on the ImageFileDirectory class in the TIFF plugin.
|
| 515 |
+
|
| 516 |
+
# The MP record essentially consists of a TIFF file embedded in a JPEG
|
| 517 |
+
# application marker.
|
| 518 |
+
try:
|
| 519 |
+
data = self.info["mp"]
|
| 520 |
+
except KeyError:
|
| 521 |
+
return None
|
| 522 |
+
file_contents = io.BytesIO(data)
|
| 523 |
+
head = file_contents.read(8)
|
| 524 |
+
endianness = ">" if head[:4] == b"\x4d\x4d\x00\x2a" else "<"
|
| 525 |
+
# process dictionary
|
| 526 |
+
from . import TiffImagePlugin
|
| 527 |
+
|
| 528 |
+
try:
|
| 529 |
+
info = TiffImagePlugin.ImageFileDirectory_v2(head)
|
| 530 |
+
file_contents.seek(info.next)
|
| 531 |
+
info.load(file_contents)
|
| 532 |
+
mp = dict(info)
|
| 533 |
+
except Exception as e:
|
| 534 |
+
msg = "malformed MP Index (unreadable directory)"
|
| 535 |
+
raise SyntaxError(msg) from e
|
| 536 |
+
# it's an error not to have a number of images
|
| 537 |
+
try:
|
| 538 |
+
quant = mp[0xB001]
|
| 539 |
+
except KeyError as e:
|
| 540 |
+
msg = "malformed MP Index (no number of images)"
|
| 541 |
+
raise SyntaxError(msg) from e
|
| 542 |
+
# get MP entries
|
| 543 |
+
mpentries = []
|
| 544 |
+
try:
|
| 545 |
+
rawmpentries = mp[0xB002]
|
| 546 |
+
for entrynum in range(0, quant):
|
| 547 |
+
unpackedentry = struct.unpack_from(
|
| 548 |
+
f"{endianness}LLLHH", rawmpentries, entrynum * 16
|
| 549 |
+
)
|
| 550 |
+
labels = ("Attribute", "Size", "DataOffset", "EntryNo1", "EntryNo2")
|
| 551 |
+
mpentry = dict(zip(labels, unpackedentry))
|
| 552 |
+
mpentryattr = {
|
| 553 |
+
"DependentParentImageFlag": bool(mpentry["Attribute"] & (1 << 31)),
|
| 554 |
+
"DependentChildImageFlag": bool(mpentry["Attribute"] & (1 << 30)),
|
| 555 |
+
"RepresentativeImageFlag": bool(mpentry["Attribute"] & (1 << 29)),
|
| 556 |
+
"Reserved": (mpentry["Attribute"] & (3 << 27)) >> 27,
|
| 557 |
+
"ImageDataFormat": (mpentry["Attribute"] & (7 << 24)) >> 24,
|
| 558 |
+
"MPType": mpentry["Attribute"] & 0x00FFFFFF,
|
| 559 |
+
}
|
| 560 |
+
if mpentryattr["ImageDataFormat"] == 0:
|
| 561 |
+
mpentryattr["ImageDataFormat"] = "JPEG"
|
| 562 |
+
else:
|
| 563 |
+
msg = "unsupported picture format in MPO"
|
| 564 |
+
raise SyntaxError(msg)
|
| 565 |
+
mptypemap = {
|
| 566 |
+
0x000000: "Undefined",
|
| 567 |
+
0x010001: "Large Thumbnail (VGA Equivalent)",
|
| 568 |
+
0x010002: "Large Thumbnail (Full HD Equivalent)",
|
| 569 |
+
0x020001: "Multi-Frame Image (Panorama)",
|
| 570 |
+
0x020002: "Multi-Frame Image: (Disparity)",
|
| 571 |
+
0x020003: "Multi-Frame Image: (Multi-Angle)",
|
| 572 |
+
0x030000: "Baseline MP Primary Image",
|
| 573 |
+
}
|
| 574 |
+
mpentryattr["MPType"] = mptypemap.get(mpentryattr["MPType"], "Unknown")
|
| 575 |
+
mpentry["Attribute"] = mpentryattr
|
| 576 |
+
mpentries.append(mpentry)
|
| 577 |
+
mp[0xB002] = mpentries
|
| 578 |
+
except KeyError as e:
|
| 579 |
+
msg = "malformed MP Index (bad MP Entry)"
|
| 580 |
+
raise SyntaxError(msg) from e
|
| 581 |
+
# Next we should try and parse the individual image unique ID list;
|
| 582 |
+
# we don't because I've never seen this actually used in a real MPO
|
| 583 |
+
# file and so can't test it.
|
| 584 |
+
return mp
|
| 585 |
+
|
| 586 |
+
|
| 587 |
+
# --------------------------------------------------------------------
|
| 588 |
+
# stuff to save JPEG files
|
| 589 |
+
|
| 590 |
+
RAWMODE = {
|
| 591 |
+
"1": "L",
|
| 592 |
+
"L": "L",
|
| 593 |
+
"RGB": "RGB",
|
| 594 |
+
"RGBX": "RGB",
|
| 595 |
+
"CMYK": "CMYK;I", # assume adobe conventions
|
| 596 |
+
"YCbCr": "YCbCr",
|
| 597 |
+
}
|
| 598 |
+
|
| 599 |
+
# fmt: off
|
| 600 |
+
zigzag_index = (
|
| 601 |
+
0, 1, 5, 6, 14, 15, 27, 28,
|
| 602 |
+
2, 4, 7, 13, 16, 26, 29, 42,
|
| 603 |
+
3, 8, 12, 17, 25, 30, 41, 43,
|
| 604 |
+
9, 11, 18, 24, 31, 40, 44, 53,
|
| 605 |
+
10, 19, 23, 32, 39, 45, 52, 54,
|
| 606 |
+
20, 22, 33, 38, 46, 51, 55, 60,
|
| 607 |
+
21, 34, 37, 47, 50, 56, 59, 61,
|
| 608 |
+
35, 36, 48, 49, 57, 58, 62, 63,
|
| 609 |
+
)
|
| 610 |
+
|
| 611 |
+
samplings = {
|
| 612 |
+
(1, 1, 1, 1, 1, 1): 0,
|
| 613 |
+
(2, 1, 1, 1, 1, 1): 1,
|
| 614 |
+
(2, 2, 1, 1, 1, 1): 2,
|
| 615 |
+
}
|
| 616 |
+
# fmt: on
|
| 617 |
+
|
| 618 |
+
|
| 619 |
+
def get_sampling(im):
|
| 620 |
+
# There's no subsampling when images have only 1 layer
|
| 621 |
+
# (grayscale images) or when they are CMYK (4 layers),
|
| 622 |
+
# so set subsampling to the default value.
|
| 623 |
+
#
|
| 624 |
+
# NOTE: currently Pillow can't encode JPEG to YCCK format.
|
| 625 |
+
# If YCCK support is added in the future, subsampling code will have
|
| 626 |
+
# to be updated (here and in JpegEncode.c) to deal with 4 layers.
|
| 627 |
+
if not hasattr(im, "layers") or im.layers in (1, 4):
|
| 628 |
+
return -1
|
| 629 |
+
sampling = im.layer[0][1:3] + im.layer[1][1:3] + im.layer[2][1:3]
|
| 630 |
+
return samplings.get(sampling, -1)
|
| 631 |
+
|
| 632 |
+
|
| 633 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 634 |
+
if im.width == 0 or im.height == 0:
|
| 635 |
+
msg = "cannot write empty image as JPEG"
|
| 636 |
+
raise ValueError(msg)
|
| 637 |
+
|
| 638 |
+
try:
|
| 639 |
+
rawmode = RAWMODE[im.mode]
|
| 640 |
+
except KeyError as e:
|
| 641 |
+
msg = f"cannot write mode {im.mode} as JPEG"
|
| 642 |
+
raise OSError(msg) from e
|
| 643 |
+
|
| 644 |
+
info = im.encoderinfo
|
| 645 |
+
|
| 646 |
+
dpi = [round(x) for x in info.get("dpi", (0, 0))]
|
| 647 |
+
|
| 648 |
+
quality = info.get("quality", -1)
|
| 649 |
+
subsampling = info.get("subsampling", -1)
|
| 650 |
+
qtables = info.get("qtables")
|
| 651 |
+
|
| 652 |
+
if quality == "keep":
|
| 653 |
+
quality = -1
|
| 654 |
+
subsampling = "keep"
|
| 655 |
+
qtables = "keep"
|
| 656 |
+
elif quality in presets:
|
| 657 |
+
preset = presets[quality]
|
| 658 |
+
quality = -1
|
| 659 |
+
subsampling = preset.get("subsampling", -1)
|
| 660 |
+
qtables = preset.get("quantization")
|
| 661 |
+
elif not isinstance(quality, int):
|
| 662 |
+
msg = "Invalid quality setting"
|
| 663 |
+
raise ValueError(msg)
|
| 664 |
+
else:
|
| 665 |
+
if subsampling in presets:
|
| 666 |
+
subsampling = presets[subsampling].get("subsampling", -1)
|
| 667 |
+
if isinstance(qtables, str) and qtables in presets:
|
| 668 |
+
qtables = presets[qtables].get("quantization")
|
| 669 |
+
|
| 670 |
+
if subsampling == "4:4:4":
|
| 671 |
+
subsampling = 0
|
| 672 |
+
elif subsampling == "4:2:2":
|
| 673 |
+
subsampling = 1
|
| 674 |
+
elif subsampling == "4:2:0":
|
| 675 |
+
subsampling = 2
|
| 676 |
+
elif subsampling == "4:1:1":
|
| 677 |
+
# For compatibility. Before Pillow 4.3, 4:1:1 actually meant 4:2:0.
|
| 678 |
+
# Set 4:2:0 if someone is still using that value.
|
| 679 |
+
subsampling = 2
|
| 680 |
+
elif subsampling == "keep":
|
| 681 |
+
if im.format != "JPEG":
|
| 682 |
+
msg = "Cannot use 'keep' when original image is not a JPEG"
|
| 683 |
+
raise ValueError(msg)
|
| 684 |
+
subsampling = get_sampling(im)
|
| 685 |
+
|
| 686 |
+
def validate_qtables(qtables):
|
| 687 |
+
if qtables is None:
|
| 688 |
+
return qtables
|
| 689 |
+
if isinstance(qtables, str):
|
| 690 |
+
try:
|
| 691 |
+
lines = [
|
| 692 |
+
int(num)
|
| 693 |
+
for line in qtables.splitlines()
|
| 694 |
+
for num in line.split("#", 1)[0].split()
|
| 695 |
+
]
|
| 696 |
+
except ValueError as e:
|
| 697 |
+
msg = "Invalid quantization table"
|
| 698 |
+
raise ValueError(msg) from e
|
| 699 |
+
else:
|
| 700 |
+
qtables = [lines[s : s + 64] for s in range(0, len(lines), 64)]
|
| 701 |
+
if isinstance(qtables, (tuple, list, dict)):
|
| 702 |
+
if isinstance(qtables, dict):
|
| 703 |
+
qtables = [
|
| 704 |
+
qtables[key] for key in range(len(qtables)) if key in qtables
|
| 705 |
+
]
|
| 706 |
+
elif isinstance(qtables, tuple):
|
| 707 |
+
qtables = list(qtables)
|
| 708 |
+
if not (0 < len(qtables) < 5):
|
| 709 |
+
msg = "None or too many quantization tables"
|
| 710 |
+
raise ValueError(msg)
|
| 711 |
+
for idx, table in enumerate(qtables):
|
| 712 |
+
try:
|
| 713 |
+
if len(table) != 64:
|
| 714 |
+
msg = "Invalid quantization table"
|
| 715 |
+
raise TypeError(msg)
|
| 716 |
+
table = array.array("H", table)
|
| 717 |
+
except TypeError as e:
|
| 718 |
+
msg = "Invalid quantization table"
|
| 719 |
+
raise ValueError(msg) from e
|
| 720 |
+
else:
|
| 721 |
+
qtables[idx] = list(table)
|
| 722 |
+
return qtables
|
| 723 |
+
|
| 724 |
+
if qtables == "keep":
|
| 725 |
+
if im.format != "JPEG":
|
| 726 |
+
msg = "Cannot use 'keep' when original image is not a JPEG"
|
| 727 |
+
raise ValueError(msg)
|
| 728 |
+
qtables = getattr(im, "quantization", None)
|
| 729 |
+
qtables = validate_qtables(qtables)
|
| 730 |
+
|
| 731 |
+
extra = info.get("extra", b"")
|
| 732 |
+
|
| 733 |
+
MAX_BYTES_IN_MARKER = 65533
|
| 734 |
+
icc_profile = info.get("icc_profile")
|
| 735 |
+
if icc_profile:
|
| 736 |
+
ICC_OVERHEAD_LEN = 14
|
| 737 |
+
MAX_DATA_BYTES_IN_MARKER = MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN
|
| 738 |
+
markers = []
|
| 739 |
+
while icc_profile:
|
| 740 |
+
markers.append(icc_profile[:MAX_DATA_BYTES_IN_MARKER])
|
| 741 |
+
icc_profile = icc_profile[MAX_DATA_BYTES_IN_MARKER:]
|
| 742 |
+
i = 1
|
| 743 |
+
for marker in markers:
|
| 744 |
+
size = o16(2 + ICC_OVERHEAD_LEN + len(marker))
|
| 745 |
+
extra += (
|
| 746 |
+
b"\xFF\xE2"
|
| 747 |
+
+ size
|
| 748 |
+
+ b"ICC_PROFILE\0"
|
| 749 |
+
+ o8(i)
|
| 750 |
+
+ o8(len(markers))
|
| 751 |
+
+ marker
|
| 752 |
+
)
|
| 753 |
+
i += 1
|
| 754 |
+
|
| 755 |
+
comment = info.get("comment", im.info.get("comment"))
|
| 756 |
+
|
| 757 |
+
# "progressive" is the official name, but older documentation
|
| 758 |
+
# says "progression"
|
| 759 |
+
# FIXME: issue a warning if the wrong form is used (post-1.1.7)
|
| 760 |
+
progressive = info.get("progressive", False) or info.get("progression", False)
|
| 761 |
+
|
| 762 |
+
optimize = info.get("optimize", False)
|
| 763 |
+
|
| 764 |
+
exif = info.get("exif", b"")
|
| 765 |
+
if isinstance(exif, Image.Exif):
|
| 766 |
+
exif = exif.tobytes()
|
| 767 |
+
if len(exif) > MAX_BYTES_IN_MARKER:
|
| 768 |
+
msg = "EXIF data is too long"
|
| 769 |
+
raise ValueError(msg)
|
| 770 |
+
|
| 771 |
+
# get keyword arguments
|
| 772 |
+
im.encoderconfig = (
|
| 773 |
+
quality,
|
| 774 |
+
progressive,
|
| 775 |
+
info.get("smooth", 0),
|
| 776 |
+
optimize,
|
| 777 |
+
info.get("keep_rgb", False),
|
| 778 |
+
info.get("streamtype", 0),
|
| 779 |
+
dpi[0],
|
| 780 |
+
dpi[1],
|
| 781 |
+
subsampling,
|
| 782 |
+
info.get("restart_marker_blocks", 0),
|
| 783 |
+
info.get("restart_marker_rows", 0),
|
| 784 |
+
qtables,
|
| 785 |
+
comment,
|
| 786 |
+
extra,
|
| 787 |
+
exif,
|
| 788 |
+
)
|
| 789 |
+
|
| 790 |
+
# if we optimize, libjpeg needs a buffer big enough to hold the whole image
|
| 791 |
+
# in a shot. Guessing on the size, at im.size bytes. (raw pixel size is
|
| 792 |
+
# channels*size, this is a value that's been used in a django patch.
|
| 793 |
+
# https://github.com/matthewwithanm/django-imagekit/issues/50
|
| 794 |
+
bufsize = 0
|
| 795 |
+
if optimize or progressive:
|
| 796 |
+
# CMYK can be bigger
|
| 797 |
+
if im.mode == "CMYK":
|
| 798 |
+
bufsize = 4 * im.size[0] * im.size[1]
|
| 799 |
+
# keep sets quality to -1, but the actual value may be high.
|
| 800 |
+
elif quality >= 95 or quality == -1:
|
| 801 |
+
bufsize = 2 * im.size[0] * im.size[1]
|
| 802 |
+
else:
|
| 803 |
+
bufsize = im.size[0] * im.size[1]
|
| 804 |
+
if exif:
|
| 805 |
+
bufsize += len(exif) + 5
|
| 806 |
+
if extra:
|
| 807 |
+
bufsize += len(extra) + 1
|
| 808 |
+
else:
|
| 809 |
+
# The EXIF info needs to be written as one block, + APP1, + one spare byte.
|
| 810 |
+
# Ensure that our buffer is big enough. Same with the icc_profile block.
|
| 811 |
+
bufsize = max(bufsize, len(exif) + 5, len(extra) + 1)
|
| 812 |
+
|
| 813 |
+
ImageFile._save(im, fp, [("jpeg", (0, 0) + im.size, 0, rawmode)], bufsize)
|
| 814 |
+
|
| 815 |
+
|
| 816 |
+
def _save_cjpeg(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 817 |
+
# ALTERNATIVE: handle JPEGs via the IJG command line utilities.
|
| 818 |
+
tempfile = im._dump()
|
| 819 |
+
subprocess.check_call(["cjpeg", "-outfile", filename, tempfile])
|
| 820 |
+
try:
|
| 821 |
+
os.unlink(tempfile)
|
| 822 |
+
except OSError:
|
| 823 |
+
pass
|
| 824 |
+
|
| 825 |
+
|
| 826 |
+
##
|
| 827 |
+
# Factory for making JPEG and MPO instances
|
| 828 |
+
def jpeg_factory(fp=None, filename=None):
|
| 829 |
+
im = JpegImageFile(fp, filename)
|
| 830 |
+
try:
|
| 831 |
+
mpheader = im._getmp()
|
| 832 |
+
if mpheader[45057] > 1:
|
| 833 |
+
for segment, content in im.applist:
|
| 834 |
+
if segment == "APP1" and b' hdrgm:Version="' in content:
|
| 835 |
+
# Ultra HDR images are not yet supported
|
| 836 |
+
return im
|
| 837 |
+
# It's actually an MPO
|
| 838 |
+
from .MpoImagePlugin import MpoImageFile
|
| 839 |
+
|
| 840 |
+
# Don't reload everything, just convert it.
|
| 841 |
+
im = MpoImageFile.adopt(im, mpheader)
|
| 842 |
+
except (TypeError, IndexError):
|
| 843 |
+
# It is really a JPEG
|
| 844 |
+
pass
|
| 845 |
+
except SyntaxError:
|
| 846 |
+
warnings.warn(
|
| 847 |
+
"Image appears to be a malformed MPO file, it will be "
|
| 848 |
+
"interpreted as a base JPEG file"
|
| 849 |
+
)
|
| 850 |
+
return im
|
| 851 |
+
|
| 852 |
+
|
| 853 |
+
# ---------------------------------------------------------------------
|
| 854 |
+
# Registry stuff
|
| 855 |
+
|
| 856 |
+
Image.register_open(JpegImageFile.format, jpeg_factory, _accept)
|
| 857 |
+
Image.register_save(JpegImageFile.format, _save)
|
| 858 |
+
|
| 859 |
+
Image.register_extensions(JpegImageFile.format, [".jfif", ".jpe", ".jpg", ".jpeg"])
|
| 860 |
+
|
| 861 |
+
Image.register_mime(JpegImageFile.format, "image/jpeg")
|
.venv/lib/python3.11/site-packages/PIL/MicImagePlugin.py
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# Microsoft Image Composer support for PIL
|
| 6 |
+
#
|
| 7 |
+
# Notes:
|
| 8 |
+
# uses TiffImagePlugin.py to read the actual image streams
|
| 9 |
+
#
|
| 10 |
+
# History:
|
| 11 |
+
# 97-01-20 fl Created
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 14 |
+
# Copyright (c) Fredrik Lundh 1997.
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import olefile
|
| 21 |
+
|
| 22 |
+
from . import Image, TiffImagePlugin
|
| 23 |
+
|
| 24 |
+
#
|
| 25 |
+
# --------------------------------------------------------------------
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def _accept(prefix: bytes) -> bool:
|
| 29 |
+
return prefix[:8] == olefile.MAGIC
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
##
|
| 33 |
+
# Image plugin for Microsoft's Image Composer file format.
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
class MicImageFile(TiffImagePlugin.TiffImageFile):
|
| 37 |
+
format = "MIC"
|
| 38 |
+
format_description = "Microsoft Image Composer"
|
| 39 |
+
_close_exclusive_fp_after_loading = False
|
| 40 |
+
|
| 41 |
+
def _open(self) -> None:
|
| 42 |
+
# read the OLE directory and see if this is a likely
|
| 43 |
+
# to be a Microsoft Image Composer file
|
| 44 |
+
|
| 45 |
+
try:
|
| 46 |
+
self.ole = olefile.OleFileIO(self.fp)
|
| 47 |
+
except OSError as e:
|
| 48 |
+
msg = "not an MIC file; invalid OLE file"
|
| 49 |
+
raise SyntaxError(msg) from e
|
| 50 |
+
|
| 51 |
+
# find ACI subfiles with Image members (maybe not the
|
| 52 |
+
# best way to identify MIC files, but what the... ;-)
|
| 53 |
+
|
| 54 |
+
self.images = [
|
| 55 |
+
path
|
| 56 |
+
for path in self.ole.listdir()
|
| 57 |
+
if path[1:] and path[0][-4:] == ".ACI" and path[1] == "Image"
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
# if we didn't find any images, this is probably not
|
| 61 |
+
# an MIC file.
|
| 62 |
+
if not self.images:
|
| 63 |
+
msg = "not an MIC file; no image entries"
|
| 64 |
+
raise SyntaxError(msg)
|
| 65 |
+
|
| 66 |
+
self.frame = -1
|
| 67 |
+
self._n_frames = len(self.images)
|
| 68 |
+
self.is_animated = self._n_frames > 1
|
| 69 |
+
|
| 70 |
+
self.__fp = self.fp
|
| 71 |
+
self.seek(0)
|
| 72 |
+
|
| 73 |
+
def seek(self, frame):
|
| 74 |
+
if not self._seek_check(frame):
|
| 75 |
+
return
|
| 76 |
+
try:
|
| 77 |
+
filename = self.images[frame]
|
| 78 |
+
except IndexError as e:
|
| 79 |
+
msg = "no such frame"
|
| 80 |
+
raise EOFError(msg) from e
|
| 81 |
+
|
| 82 |
+
self.fp = self.ole.openstream(filename)
|
| 83 |
+
|
| 84 |
+
TiffImagePlugin.TiffImageFile._open(self)
|
| 85 |
+
|
| 86 |
+
self.frame = frame
|
| 87 |
+
|
| 88 |
+
def tell(self) -> int:
|
| 89 |
+
return self.frame
|
| 90 |
+
|
| 91 |
+
def close(self) -> None:
|
| 92 |
+
self.__fp.close()
|
| 93 |
+
self.ole.close()
|
| 94 |
+
super().close()
|
| 95 |
+
|
| 96 |
+
def __exit__(self, *args: object) -> None:
|
| 97 |
+
self.__fp.close()
|
| 98 |
+
self.ole.close()
|
| 99 |
+
super().__exit__()
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
#
|
| 103 |
+
# --------------------------------------------------------------------
|
| 104 |
+
|
| 105 |
+
Image.register_open(MicImageFile.format, MicImageFile, _accept)
|
| 106 |
+
|
| 107 |
+
Image.register_extension(MicImageFile.format, ".mic")
|
.venv/lib/python3.11/site-packages/PIL/MpegImagePlugin.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# MPEG file handling
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 95-09-09 fl Created
|
| 9 |
+
#
|
| 10 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 11 |
+
# Copyright (c) Fredrik Lundh 1995.
|
| 12 |
+
#
|
| 13 |
+
# See the README file for information on usage and redistribution.
|
| 14 |
+
#
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
from . import Image, ImageFile
|
| 18 |
+
from ._binary import i8
|
| 19 |
+
from ._typing import SupportsRead
|
| 20 |
+
|
| 21 |
+
#
|
| 22 |
+
# Bitstream parser
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
class BitStream:
|
| 26 |
+
def __init__(self, fp: SupportsRead[bytes]) -> None:
|
| 27 |
+
self.fp = fp
|
| 28 |
+
self.bits = 0
|
| 29 |
+
self.bitbuffer = 0
|
| 30 |
+
|
| 31 |
+
def next(self) -> int:
|
| 32 |
+
return i8(self.fp.read(1))
|
| 33 |
+
|
| 34 |
+
def peek(self, bits: int) -> int:
|
| 35 |
+
while self.bits < bits:
|
| 36 |
+
c = self.next()
|
| 37 |
+
if c < 0:
|
| 38 |
+
self.bits = 0
|
| 39 |
+
continue
|
| 40 |
+
self.bitbuffer = (self.bitbuffer << 8) + c
|
| 41 |
+
self.bits += 8
|
| 42 |
+
return self.bitbuffer >> (self.bits - bits) & (1 << bits) - 1
|
| 43 |
+
|
| 44 |
+
def skip(self, bits: int) -> None:
|
| 45 |
+
while self.bits < bits:
|
| 46 |
+
self.bitbuffer = (self.bitbuffer << 8) + i8(self.fp.read(1))
|
| 47 |
+
self.bits += 8
|
| 48 |
+
self.bits = self.bits - bits
|
| 49 |
+
|
| 50 |
+
def read(self, bits: int) -> int:
|
| 51 |
+
v = self.peek(bits)
|
| 52 |
+
self.bits = self.bits - bits
|
| 53 |
+
return v
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
def _accept(prefix: bytes) -> bool:
|
| 57 |
+
return prefix[:4] == b"\x00\x00\x01\xb3"
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
##
|
| 61 |
+
# Image plugin for MPEG streams. This plugin can identify a stream,
|
| 62 |
+
# but it cannot read it.
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
class MpegImageFile(ImageFile.ImageFile):
|
| 66 |
+
format = "MPEG"
|
| 67 |
+
format_description = "MPEG"
|
| 68 |
+
|
| 69 |
+
def _open(self) -> None:
|
| 70 |
+
assert self.fp is not None
|
| 71 |
+
|
| 72 |
+
s = BitStream(self.fp)
|
| 73 |
+
if s.read(32) != 0x1B3:
|
| 74 |
+
msg = "not an MPEG file"
|
| 75 |
+
raise SyntaxError(msg)
|
| 76 |
+
|
| 77 |
+
self._mode = "RGB"
|
| 78 |
+
self._size = s.read(12), s.read(12)
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
# --------------------------------------------------------------------
|
| 82 |
+
# Registry stuff
|
| 83 |
+
|
| 84 |
+
Image.register_open(MpegImageFile.format, MpegImageFile, _accept)
|
| 85 |
+
|
| 86 |
+
Image.register_extensions(MpegImageFile.format, [".mpg", ".mpeg"])
|
| 87 |
+
|
| 88 |
+
Image.register_mime(MpegImageFile.format, "video/mpeg")
|
.venv/lib/python3.11/site-packages/PIL/PSDraw.py
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# Simple PostScript graphics interface
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 1996-04-20 fl Created
|
| 9 |
+
# 1999-01-10 fl Added gsave/grestore to image method
|
| 10 |
+
# 2005-05-04 fl Fixed floating point issue in image (from Eric Etheridge)
|
| 11 |
+
#
|
| 12 |
+
# Copyright (c) 1997-2005 by Secret Labs AB. All rights reserved.
|
| 13 |
+
# Copyright (c) 1996 by Fredrik Lundh.
|
| 14 |
+
#
|
| 15 |
+
# See the README file for information on usage and redistribution.
|
| 16 |
+
#
|
| 17 |
+
from __future__ import annotations
|
| 18 |
+
|
| 19 |
+
import sys
|
| 20 |
+
from typing import TYPE_CHECKING
|
| 21 |
+
|
| 22 |
+
from . import EpsImagePlugin
|
| 23 |
+
|
| 24 |
+
##
|
| 25 |
+
# Simple PostScript graphics interface.
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
class PSDraw:
|
| 29 |
+
"""
|
| 30 |
+
Sets up printing to the given file. If ``fp`` is omitted,
|
| 31 |
+
``sys.stdout.buffer`` or ``sys.stdout`` is assumed.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
def __init__(self, fp=None):
|
| 35 |
+
if not fp:
|
| 36 |
+
try:
|
| 37 |
+
fp = sys.stdout.buffer
|
| 38 |
+
except AttributeError:
|
| 39 |
+
fp = sys.stdout
|
| 40 |
+
self.fp = fp
|
| 41 |
+
|
| 42 |
+
def begin_document(self, id: str | None = None) -> None:
|
| 43 |
+
"""Set up printing of a document. (Write PostScript DSC header.)"""
|
| 44 |
+
# FIXME: incomplete
|
| 45 |
+
self.fp.write(
|
| 46 |
+
b"%!PS-Adobe-3.0\n"
|
| 47 |
+
b"save\n"
|
| 48 |
+
b"/showpage { } def\n"
|
| 49 |
+
b"%%EndComments\n"
|
| 50 |
+
b"%%BeginDocument\n"
|
| 51 |
+
)
|
| 52 |
+
# self.fp.write(ERROR_PS) # debugging!
|
| 53 |
+
self.fp.write(EDROFF_PS)
|
| 54 |
+
self.fp.write(VDI_PS)
|
| 55 |
+
self.fp.write(b"%%EndProlog\n")
|
| 56 |
+
self.isofont: dict[bytes, int] = {}
|
| 57 |
+
|
| 58 |
+
def end_document(self) -> None:
|
| 59 |
+
"""Ends printing. (Write PostScript DSC footer.)"""
|
| 60 |
+
self.fp.write(b"%%EndDocument\nrestore showpage\n%%End\n")
|
| 61 |
+
if hasattr(self.fp, "flush"):
|
| 62 |
+
self.fp.flush()
|
| 63 |
+
|
| 64 |
+
def setfont(self, font: str, size: int) -> None:
|
| 65 |
+
"""
|
| 66 |
+
Selects which font to use.
|
| 67 |
+
|
| 68 |
+
:param font: A PostScript font name
|
| 69 |
+
:param size: Size in points.
|
| 70 |
+
"""
|
| 71 |
+
font_bytes = bytes(font, "UTF-8")
|
| 72 |
+
if font_bytes not in self.isofont:
|
| 73 |
+
# reencode font
|
| 74 |
+
self.fp.write(
|
| 75 |
+
b"/PSDraw-%s ISOLatin1Encoding /%s E\n" % (font_bytes, font_bytes)
|
| 76 |
+
)
|
| 77 |
+
self.isofont[font_bytes] = 1
|
| 78 |
+
# rough
|
| 79 |
+
self.fp.write(b"/F0 %d /PSDraw-%s F\n" % (size, font_bytes))
|
| 80 |
+
|
| 81 |
+
def line(self, xy0: tuple[int, int], xy1: tuple[int, int]) -> None:
|
| 82 |
+
"""
|
| 83 |
+
Draws a line between the two points. Coordinates are given in
|
| 84 |
+
PostScript point coordinates (72 points per inch, (0, 0) is the lower
|
| 85 |
+
left corner of the page).
|
| 86 |
+
"""
|
| 87 |
+
self.fp.write(b"%d %d %d %d Vl\n" % (*xy0, *xy1))
|
| 88 |
+
|
| 89 |
+
def rectangle(self, box: tuple[int, int, int, int]) -> None:
|
| 90 |
+
"""
|
| 91 |
+
Draws a rectangle.
|
| 92 |
+
|
| 93 |
+
:param box: A tuple of four integers, specifying left, bottom, width and
|
| 94 |
+
height.
|
| 95 |
+
"""
|
| 96 |
+
self.fp.write(b"%d %d M 0 %d %d Vr\n" % box)
|
| 97 |
+
|
| 98 |
+
def text(self, xy: tuple[int, int], text: str) -> None:
|
| 99 |
+
"""
|
| 100 |
+
Draws text at the given position. You must use
|
| 101 |
+
:py:meth:`~PIL.PSDraw.PSDraw.setfont` before calling this method.
|
| 102 |
+
"""
|
| 103 |
+
text_bytes = bytes(text, "UTF-8")
|
| 104 |
+
text_bytes = b"\\(".join(text_bytes.split(b"("))
|
| 105 |
+
text_bytes = b"\\)".join(text_bytes.split(b")"))
|
| 106 |
+
self.fp.write(b"%d %d M (%s) S\n" % (xy + (text_bytes,)))
|
| 107 |
+
|
| 108 |
+
if TYPE_CHECKING:
|
| 109 |
+
from . import Image
|
| 110 |
+
|
| 111 |
+
def image(
|
| 112 |
+
self, box: tuple[int, int, int, int], im: Image.Image, dpi: int | None = None
|
| 113 |
+
) -> None:
|
| 114 |
+
"""Draw a PIL image, centered in the given box."""
|
| 115 |
+
# default resolution depends on mode
|
| 116 |
+
if not dpi:
|
| 117 |
+
if im.mode == "1":
|
| 118 |
+
dpi = 200 # fax
|
| 119 |
+
else:
|
| 120 |
+
dpi = 100 # grayscale
|
| 121 |
+
# image size (on paper)
|
| 122 |
+
x = im.size[0] * 72 / dpi
|
| 123 |
+
y = im.size[1] * 72 / dpi
|
| 124 |
+
# max allowed size
|
| 125 |
+
xmax = float(box[2] - box[0])
|
| 126 |
+
ymax = float(box[3] - box[1])
|
| 127 |
+
if x > xmax:
|
| 128 |
+
y = y * xmax / x
|
| 129 |
+
x = xmax
|
| 130 |
+
if y > ymax:
|
| 131 |
+
x = x * ymax / y
|
| 132 |
+
y = ymax
|
| 133 |
+
dx = (xmax - x) / 2 + box[0]
|
| 134 |
+
dy = (ymax - y) / 2 + box[1]
|
| 135 |
+
self.fp.write(b"gsave\n%f %f translate\n" % (dx, dy))
|
| 136 |
+
if (x, y) != im.size:
|
| 137 |
+
# EpsImagePlugin._save prints the image at (0,0,xsize,ysize)
|
| 138 |
+
sx = x / im.size[0]
|
| 139 |
+
sy = y / im.size[1]
|
| 140 |
+
self.fp.write(b"%f %f scale\n" % (sx, sy))
|
| 141 |
+
EpsImagePlugin._save(im, self.fp, "", 0)
|
| 142 |
+
self.fp.write(b"\ngrestore\n")
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
# --------------------------------------------------------------------
|
| 146 |
+
# PostScript driver
|
| 147 |
+
|
| 148 |
+
#
|
| 149 |
+
# EDROFF.PS -- PostScript driver for Edroff 2
|
| 150 |
+
#
|
| 151 |
+
# History:
|
| 152 |
+
# 94-01-25 fl: created (edroff 2.04)
|
| 153 |
+
#
|
| 154 |
+
# Copyright (c) Fredrik Lundh 1994.
|
| 155 |
+
#
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
EDROFF_PS = b"""\
|
| 159 |
+
/S { show } bind def
|
| 160 |
+
/P { moveto show } bind def
|
| 161 |
+
/M { moveto } bind def
|
| 162 |
+
/X { 0 rmoveto } bind def
|
| 163 |
+
/Y { 0 exch rmoveto } bind def
|
| 164 |
+
/E { findfont
|
| 165 |
+
dup maxlength dict begin
|
| 166 |
+
{
|
| 167 |
+
1 index /FID ne { def } { pop pop } ifelse
|
| 168 |
+
} forall
|
| 169 |
+
/Encoding exch def
|
| 170 |
+
dup /FontName exch def
|
| 171 |
+
currentdict end definefont pop
|
| 172 |
+
} bind def
|
| 173 |
+
/F { findfont exch scalefont dup setfont
|
| 174 |
+
[ exch /setfont cvx ] cvx bind def
|
| 175 |
+
} bind def
|
| 176 |
+
"""
|
| 177 |
+
|
| 178 |
+
#
|
| 179 |
+
# VDI.PS -- PostScript driver for VDI meta commands
|
| 180 |
+
#
|
| 181 |
+
# History:
|
| 182 |
+
# 94-01-25 fl: created (edroff 2.04)
|
| 183 |
+
#
|
| 184 |
+
# Copyright (c) Fredrik Lundh 1994.
|
| 185 |
+
#
|
| 186 |
+
|
| 187 |
+
VDI_PS = b"""\
|
| 188 |
+
/Vm { moveto } bind def
|
| 189 |
+
/Va { newpath arcn stroke } bind def
|
| 190 |
+
/Vl { moveto lineto stroke } bind def
|
| 191 |
+
/Vc { newpath 0 360 arc closepath } bind def
|
| 192 |
+
/Vr { exch dup 0 rlineto
|
| 193 |
+
exch dup 0 exch rlineto
|
| 194 |
+
exch neg 0 rlineto
|
| 195 |
+
0 exch neg rlineto
|
| 196 |
+
setgray fill } bind def
|
| 197 |
+
/Tm matrix def
|
| 198 |
+
/Ve { Tm currentmatrix pop
|
| 199 |
+
translate scale newpath 0 0 .5 0 360 arc closepath
|
| 200 |
+
Tm setmatrix
|
| 201 |
+
} bind def
|
| 202 |
+
/Vf { currentgray exch setgray fill setgray } bind def
|
| 203 |
+
"""
|
| 204 |
+
|
| 205 |
+
#
|
| 206 |
+
# ERROR.PS -- Error handler
|
| 207 |
+
#
|
| 208 |
+
# History:
|
| 209 |
+
# 89-11-21 fl: created (pslist 1.10)
|
| 210 |
+
#
|
| 211 |
+
|
| 212 |
+
ERROR_PS = b"""\
|
| 213 |
+
/landscape false def
|
| 214 |
+
/errorBUF 200 string def
|
| 215 |
+
/errorNL { currentpoint 10 sub exch pop 72 exch moveto } def
|
| 216 |
+
errordict begin /handleerror {
|
| 217 |
+
initmatrix /Courier findfont 10 scalefont setfont
|
| 218 |
+
newpath 72 720 moveto $error begin /newerror false def
|
| 219 |
+
(PostScript Error) show errorNL errorNL
|
| 220 |
+
(Error: ) show
|
| 221 |
+
/errorname load errorBUF cvs show errorNL errorNL
|
| 222 |
+
(Command: ) show
|
| 223 |
+
/command load dup type /stringtype ne { errorBUF cvs } if show
|
| 224 |
+
errorNL errorNL
|
| 225 |
+
(VMstatus: ) show
|
| 226 |
+
vmstatus errorBUF cvs show ( bytes available, ) show
|
| 227 |
+
errorBUF cvs show ( bytes used at level ) show
|
| 228 |
+
errorBUF cvs show errorNL errorNL
|
| 229 |
+
(Operand stargck: ) show errorNL /ostargck load {
|
| 230 |
+
dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
|
| 231 |
+
} forall errorNL
|
| 232 |
+
(Execution stargck: ) show errorNL /estargck load {
|
| 233 |
+
dup type /stringtype ne { errorBUF cvs } if 72 0 rmoveto show errorNL
|
| 234 |
+
} forall
|
| 235 |
+
end showpage
|
| 236 |
+
} def end
|
| 237 |
+
"""
|
.venv/lib/python3.11/site-packages/PIL/PcdImagePlugin.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# PCD file handling
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 96-05-10 fl Created
|
| 9 |
+
# 96-05-27 fl Added draft mode (128x192, 256x384)
|
| 10 |
+
#
|
| 11 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 12 |
+
# Copyright (c) Fredrik Lundh 1996.
|
| 13 |
+
#
|
| 14 |
+
# See the README file for information on usage and redistribution.
|
| 15 |
+
#
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
from . import Image, ImageFile
|
| 19 |
+
|
| 20 |
+
##
|
| 21 |
+
# Image plugin for PhotoCD images. This plugin only reads the 768x512
|
| 22 |
+
# image from the file; higher resolutions are encoded in a proprietary
|
| 23 |
+
# encoding.
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
class PcdImageFile(ImageFile.ImageFile):
|
| 27 |
+
format = "PCD"
|
| 28 |
+
format_description = "Kodak PhotoCD"
|
| 29 |
+
|
| 30 |
+
def _open(self) -> None:
|
| 31 |
+
# rough
|
| 32 |
+
assert self.fp is not None
|
| 33 |
+
|
| 34 |
+
self.fp.seek(2048)
|
| 35 |
+
s = self.fp.read(2048)
|
| 36 |
+
|
| 37 |
+
if s[:4] != b"PCD_":
|
| 38 |
+
msg = "not a PCD file"
|
| 39 |
+
raise SyntaxError(msg)
|
| 40 |
+
|
| 41 |
+
orientation = s[1538] & 3
|
| 42 |
+
self.tile_post_rotate = None
|
| 43 |
+
if orientation == 1:
|
| 44 |
+
self.tile_post_rotate = 90
|
| 45 |
+
elif orientation == 3:
|
| 46 |
+
self.tile_post_rotate = -90
|
| 47 |
+
|
| 48 |
+
self._mode = "RGB"
|
| 49 |
+
self._size = 768, 512 # FIXME: not correct for rotated images!
|
| 50 |
+
self.tile = [("pcd", (0, 0) + self.size, 96 * 2048, None)]
|
| 51 |
+
|
| 52 |
+
def load_end(self) -> None:
|
| 53 |
+
if self.tile_post_rotate:
|
| 54 |
+
# Handle rotated PCDs
|
| 55 |
+
assert self.im is not None
|
| 56 |
+
|
| 57 |
+
self.im = self.im.rotate(self.tile_post_rotate)
|
| 58 |
+
self._size = self.im.size
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
#
|
| 62 |
+
# registry
|
| 63 |
+
|
| 64 |
+
Image.register_open(PcdImageFile.format, PcdImageFile)
|
| 65 |
+
|
| 66 |
+
Image.register_extension(PcdImageFile.format, ".pcd")
|
.venv/lib/python3.11/site-packages/PIL/PcfFontFile.py
ADDED
|
@@ -0,0 +1,254 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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onts
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) 1997-2003 by Secret Labs AB.
|
| 14 |
+
# Copyright (c) 1997-2003 by Fredrik Lundh.
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
import io
|
| 21 |
+
from typing import BinaryIO, Callable
|
| 22 |
+
|
| 23 |
+
from . import FontFile, Image
|
| 24 |
+
from ._binary import i8
|
| 25 |
+
from ._binary import i16be as b16
|
| 26 |
+
from ._binary import i16le as l16
|
| 27 |
+
from ._binary import i32be as b32
|
| 28 |
+
from ._binary import i32le as l32
|
| 29 |
+
|
| 30 |
+
# --------------------------------------------------------------------
|
| 31 |
+
# declarations
|
| 32 |
+
|
| 33 |
+
PCF_MAGIC = 0x70636601 # "\x01fcp"
|
| 34 |
+
|
| 35 |
+
PCF_PROPERTIES = 1 << 0
|
| 36 |
+
PCF_ACCELERATORS = 1 << 1
|
| 37 |
+
PCF_METRICS = 1 << 2
|
| 38 |
+
PCF_BITMAPS = 1 << 3
|
| 39 |
+
PCF_INK_METRICS = 1 << 4
|
| 40 |
+
PCF_BDF_ENCODINGS = 1 << 5
|
| 41 |
+
PCF_SWIDTHS = 1 << 6
|
| 42 |
+
PCF_GLYPH_NAMES = 1 << 7
|
| 43 |
+
PCF_BDF_ACCELERATORS = 1 << 8
|
| 44 |
+
|
| 45 |
+
BYTES_PER_ROW: list[Callable[[int], int]] = [
|
| 46 |
+
lambda bits: ((bits + 7) >> 3),
|
| 47 |
+
lambda bits: ((bits + 15) >> 3) & ~1,
|
| 48 |
+
lambda bits: ((bits + 31) >> 3) & ~3,
|
| 49 |
+
lambda bits: ((bits + 63) >> 3) & ~7,
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def sz(s: bytes, o: int) -> bytes:
|
| 54 |
+
return s[o : s.index(b"\0", o)]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
class PcfFontFile(FontFile.FontFile):
|
| 58 |
+
"""Font file plugin for the X11 PCF format."""
|
| 59 |
+
|
| 60 |
+
name = "name"
|
| 61 |
+
|
| 62 |
+
def __init__(self, fp: BinaryIO, charset_encoding: str = "iso8859-1"):
|
| 63 |
+
self.charset_encoding = charset_encoding
|
| 64 |
+
|
| 65 |
+
magic = l32(fp.read(4))
|
| 66 |
+
if magic != PCF_MAGIC:
|
| 67 |
+
msg = "not a PCF file"
|
| 68 |
+
raise SyntaxError(msg)
|
| 69 |
+
|
| 70 |
+
super().__init__()
|
| 71 |
+
|
| 72 |
+
count = l32(fp.read(4))
|
| 73 |
+
self.toc = {}
|
| 74 |
+
for i in range(count):
|
| 75 |
+
type = l32(fp.read(4))
|
| 76 |
+
self.toc[type] = l32(fp.read(4)), l32(fp.read(4)), l32(fp.read(4))
|
| 77 |
+
|
| 78 |
+
self.fp = fp
|
| 79 |
+
|
| 80 |
+
self.info = self._load_properties()
|
| 81 |
+
|
| 82 |
+
metrics = self._load_metrics()
|
| 83 |
+
bitmaps = self._load_bitmaps(metrics)
|
| 84 |
+
encoding = self._load_encoding()
|
| 85 |
+
|
| 86 |
+
#
|
| 87 |
+
# create glyph structure
|
| 88 |
+
|
| 89 |
+
for ch, ix in enumerate(encoding):
|
| 90 |
+
if ix is not None:
|
| 91 |
+
(
|
| 92 |
+
xsize,
|
| 93 |
+
ysize,
|
| 94 |
+
left,
|
| 95 |
+
right,
|
| 96 |
+
width,
|
| 97 |
+
ascent,
|
| 98 |
+
descent,
|
| 99 |
+
attributes,
|
| 100 |
+
) = metrics[ix]
|
| 101 |
+
self.glyph[ch] = (
|
| 102 |
+
(width, 0),
|
| 103 |
+
(left, descent - ysize, xsize + left, descent),
|
| 104 |
+
(0, 0, xsize, ysize),
|
| 105 |
+
bitmaps[ix],
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
def _getformat(
|
| 109 |
+
self, tag: int
|
| 110 |
+
) -> tuple[BinaryIO, int, Callable[[bytes], int], Callable[[bytes], int]]:
|
| 111 |
+
format, size, offset = self.toc[tag]
|
| 112 |
+
|
| 113 |
+
fp = self.fp
|
| 114 |
+
fp.seek(offset)
|
| 115 |
+
|
| 116 |
+
format = l32(fp.read(4))
|
| 117 |
+
|
| 118 |
+
if format & 4:
|
| 119 |
+
i16, i32 = b16, b32
|
| 120 |
+
else:
|
| 121 |
+
i16, i32 = l16, l32
|
| 122 |
+
|
| 123 |
+
return fp, format, i16, i32
|
| 124 |
+
|
| 125 |
+
def _load_properties(self) -> dict[bytes, bytes | int]:
|
| 126 |
+
#
|
| 127 |
+
# font properties
|
| 128 |
+
|
| 129 |
+
properties = {}
|
| 130 |
+
|
| 131 |
+
fp, format, i16, i32 = self._getformat(PCF_PROPERTIES)
|
| 132 |
+
|
| 133 |
+
nprops = i32(fp.read(4))
|
| 134 |
+
|
| 135 |
+
# read property description
|
| 136 |
+
p = [(i32(fp.read(4)), i8(fp.read(1)), i32(fp.read(4))) for _ in range(nprops)]
|
| 137 |
+
|
| 138 |
+
if nprops & 3:
|
| 139 |
+
fp.seek(4 - (nprops & 3), io.SEEK_CUR) # pad
|
| 140 |
+
|
| 141 |
+
data = fp.read(i32(fp.read(4)))
|
| 142 |
+
|
| 143 |
+
for k, s, v in p:
|
| 144 |
+
property_value: bytes | int = sz(data, v) if s else v
|
| 145 |
+
properties[sz(data, k)] = property_value
|
| 146 |
+
|
| 147 |
+
return properties
|
| 148 |
+
|
| 149 |
+
def _load_metrics(self) -> list[tuple[int, int, int, int, int, int, int, int]]:
|
| 150 |
+
#
|
| 151 |
+
# font metrics
|
| 152 |
+
|
| 153 |
+
metrics: list[tuple[int, int, int, int, int, int, int, int]] = []
|
| 154 |
+
|
| 155 |
+
fp, format, i16, i32 = self._getformat(PCF_METRICS)
|
| 156 |
+
|
| 157 |
+
append = metrics.append
|
| 158 |
+
|
| 159 |
+
if (format & 0xFF00) == 0x100:
|
| 160 |
+
# "compressed" metrics
|
| 161 |
+
for i in range(i16(fp.read(2))):
|
| 162 |
+
left = i8(fp.read(1)) - 128
|
| 163 |
+
right = i8(fp.read(1)) - 128
|
| 164 |
+
width = i8(fp.read(1)) - 128
|
| 165 |
+
ascent = i8(fp.read(1)) - 128
|
| 166 |
+
descent = i8(fp.read(1)) - 128
|
| 167 |
+
xsize = right - left
|
| 168 |
+
ysize = ascent + descent
|
| 169 |
+
append((xsize, ysize, left, right, width, ascent, descent, 0))
|
| 170 |
+
|
| 171 |
+
else:
|
| 172 |
+
# "jumbo" metrics
|
| 173 |
+
for i in range(i32(fp.read(4))):
|
| 174 |
+
left = i16(fp.read(2))
|
| 175 |
+
right = i16(fp.read(2))
|
| 176 |
+
width = i16(fp.read(2))
|
| 177 |
+
ascent = i16(fp.read(2))
|
| 178 |
+
descent = i16(fp.read(2))
|
| 179 |
+
attributes = i16(fp.read(2))
|
| 180 |
+
xsize = right - left
|
| 181 |
+
ysize = ascent + descent
|
| 182 |
+
append((xsize, ysize, left, right, width, ascent, descent, attributes))
|
| 183 |
+
|
| 184 |
+
return metrics
|
| 185 |
+
|
| 186 |
+
def _load_bitmaps(
|
| 187 |
+
self, metrics: list[tuple[int, int, int, int, int, int, int, int]]
|
| 188 |
+
) -> list[Image.Image]:
|
| 189 |
+
#
|
| 190 |
+
# bitmap data
|
| 191 |
+
|
| 192 |
+
fp, format, i16, i32 = self._getformat(PCF_BITMAPS)
|
| 193 |
+
|
| 194 |
+
nbitmaps = i32(fp.read(4))
|
| 195 |
+
|
| 196 |
+
if nbitmaps != len(metrics):
|
| 197 |
+
msg = "Wrong number of bitmaps"
|
| 198 |
+
raise OSError(msg)
|
| 199 |
+
|
| 200 |
+
offsets = [i32(fp.read(4)) for _ in range(nbitmaps)]
|
| 201 |
+
|
| 202 |
+
bitmap_sizes = [i32(fp.read(4)) for _ in range(4)]
|
| 203 |
+
|
| 204 |
+
# byteorder = format & 4 # non-zero => MSB
|
| 205 |
+
bitorder = format & 8 # non-zero => MSB
|
| 206 |
+
padindex = format & 3
|
| 207 |
+
|
| 208 |
+
bitmapsize = bitmap_sizes[padindex]
|
| 209 |
+
offsets.append(bitmapsize)
|
| 210 |
+
|
| 211 |
+
data = fp.read(bitmapsize)
|
| 212 |
+
|
| 213 |
+
pad = BYTES_PER_ROW[padindex]
|
| 214 |
+
mode = "1;R"
|
| 215 |
+
if bitorder:
|
| 216 |
+
mode = "1"
|
| 217 |
+
|
| 218 |
+
bitmaps = []
|
| 219 |
+
for i in range(nbitmaps):
|
| 220 |
+
xsize, ysize = metrics[i][:2]
|
| 221 |
+
b, e = offsets[i : i + 2]
|
| 222 |
+
bitmaps.append(
|
| 223 |
+
Image.frombytes("1", (xsize, ysize), data[b:e], "raw", mode, pad(xsize))
|
| 224 |
+
)
|
| 225 |
+
|
| 226 |
+
return bitmaps
|
| 227 |
+
|
| 228 |
+
def _load_encoding(self) -> list[int | None]:
|
| 229 |
+
fp, format, i16, i32 = self._getformat(PCF_BDF_ENCODINGS)
|
| 230 |
+
|
| 231 |
+
first_col, last_col = i16(fp.read(2)), i16(fp.read(2))
|
| 232 |
+
first_row, last_row = i16(fp.read(2)), i16(fp.read(2))
|
| 233 |
+
|
| 234 |
+
i16(fp.read(2)) # default
|
| 235 |
+
|
| 236 |
+
nencoding = (last_col - first_col + 1) * (last_row - first_row + 1)
|
| 237 |
+
|
| 238 |
+
# map character code to bitmap index
|
| 239 |
+
encoding: list[int | None] = [None] * min(256, nencoding)
|
| 240 |
+
|
| 241 |
+
encoding_offsets = [i16(fp.read(2)) for _ in range(nencoding)]
|
| 242 |
+
|
| 243 |
+
for i in range(first_col, len(encoding)):
|
| 244 |
+
try:
|
| 245 |
+
encoding_offset = encoding_offsets[
|
| 246 |
+
ord(bytearray([i]).decode(self.charset_encoding))
|
| 247 |
+
]
|
| 248 |
+
if encoding_offset != 0xFFFF:
|
| 249 |
+
encoding[i] = encoding_offset
|
| 250 |
+
except UnicodeDecodeError:
|
| 251 |
+
# character is not supported in selected encoding
|
| 252 |
+
pass
|
| 253 |
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2 |
+
# The Python Imaging Library.
|
| 3 |
+
#
|
| 4 |
+
# SPIDER image file handling
|
| 5 |
+
#
|
| 6 |
+
# History:
|
| 7 |
+
# 2004-08-02 Created BB
|
| 8 |
+
# 2006-03-02 added save method
|
| 9 |
+
# 2006-03-13 added support for stack images
|
| 10 |
+
#
|
| 11 |
+
# Copyright (c) 2004 by Health Research Inc. (HRI) RENSSELAER, NY 12144.
|
| 12 |
+
# Copyright (c) 2004 by William Baxter.
|
| 13 |
+
# Copyright (c) 2004 by Secret Labs AB.
|
| 14 |
+
# Copyright (c) 2004 by Fredrik Lundh.
|
| 15 |
+
#
|
| 16 |
+
|
| 17 |
+
##
|
| 18 |
+
# Image plugin for the Spider image format. This format is used
|
| 19 |
+
# by the SPIDER software, in processing image data from electron
|
| 20 |
+
# microscopy and tomography.
|
| 21 |
+
##
|
| 22 |
+
|
| 23 |
+
#
|
| 24 |
+
# SpiderImagePlugin.py
|
| 25 |
+
#
|
| 26 |
+
# The Spider image format is used by SPIDER software, in processing
|
| 27 |
+
# image data from electron microscopy and tomography.
|
| 28 |
+
#
|
| 29 |
+
# Spider home page:
|
| 30 |
+
# https://spider.wadsworth.org/spider_doc/spider/docs/spider.html
|
| 31 |
+
#
|
| 32 |
+
# Details about the Spider image format:
|
| 33 |
+
# https://spider.wadsworth.org/spider_doc/spider/docs/image_doc.html
|
| 34 |
+
#
|
| 35 |
+
from __future__ import annotations
|
| 36 |
+
|
| 37 |
+
import os
|
| 38 |
+
import struct
|
| 39 |
+
import sys
|
| 40 |
+
from typing import IO, TYPE_CHECKING, Any, Tuple, cast
|
| 41 |
+
|
| 42 |
+
from . import Image, ImageFile
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def isInt(f: Any) -> int:
|
| 46 |
+
try:
|
| 47 |
+
i = int(f)
|
| 48 |
+
if f - i == 0:
|
| 49 |
+
return 1
|
| 50 |
+
else:
|
| 51 |
+
return 0
|
| 52 |
+
except (ValueError, OverflowError):
|
| 53 |
+
return 0
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
iforms = [1, 3, -11, -12, -21, -22]
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
# There is no magic number to identify Spider files, so just check a
|
| 60 |
+
# series of header locations to see if they have reasonable values.
|
| 61 |
+
# Returns no. of bytes in the header, if it is a valid Spider header,
|
| 62 |
+
# otherwise returns 0
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def isSpiderHeader(t: tuple[float, ...]) -> int:
|
| 66 |
+
h = (99,) + t # add 1 value so can use spider header index start=1
|
| 67 |
+
# header values 1,2,5,12,13,22,23 should be integers
|
| 68 |
+
for i in [1, 2, 5, 12, 13, 22, 23]:
|
| 69 |
+
if not isInt(h[i]):
|
| 70 |
+
return 0
|
| 71 |
+
# check iform
|
| 72 |
+
iform = int(h[5])
|
| 73 |
+
if iform not in iforms:
|
| 74 |
+
return 0
|
| 75 |
+
# check other header values
|
| 76 |
+
labrec = int(h[13]) # no. records in file header
|
| 77 |
+
labbyt = int(h[22]) # total no. of bytes in header
|
| 78 |
+
lenbyt = int(h[23]) # record length in bytes
|
| 79 |
+
if labbyt != (labrec * lenbyt):
|
| 80 |
+
return 0
|
| 81 |
+
# looks like a valid header
|
| 82 |
+
return labbyt
|
| 83 |
+
|
| 84 |
+
|
| 85 |
+
def isSpiderImage(filename: str) -> int:
|
| 86 |
+
with open(filename, "rb") as fp:
|
| 87 |
+
f = fp.read(92) # read 23 * 4 bytes
|
| 88 |
+
t = struct.unpack(">23f", f) # try big-endian first
|
| 89 |
+
hdrlen = isSpiderHeader(t)
|
| 90 |
+
if hdrlen == 0:
|
| 91 |
+
t = struct.unpack("<23f", f) # little-endian
|
| 92 |
+
hdrlen = isSpiderHeader(t)
|
| 93 |
+
return hdrlen
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
class SpiderImageFile(ImageFile.ImageFile):
|
| 97 |
+
format = "SPIDER"
|
| 98 |
+
format_description = "Spider 2D image"
|
| 99 |
+
_close_exclusive_fp_after_loading = False
|
| 100 |
+
|
| 101 |
+
def _open(self) -> None:
|
| 102 |
+
# check header
|
| 103 |
+
n = 27 * 4 # read 27 float values
|
| 104 |
+
f = self.fp.read(n)
|
| 105 |
+
|
| 106 |
+
try:
|
| 107 |
+
self.bigendian = 1
|
| 108 |
+
t = struct.unpack(">27f", f) # try big-endian first
|
| 109 |
+
hdrlen = isSpiderHeader(t)
|
| 110 |
+
if hdrlen == 0:
|
| 111 |
+
self.bigendian = 0
|
| 112 |
+
t = struct.unpack("<27f", f) # little-endian
|
| 113 |
+
hdrlen = isSpiderHeader(t)
|
| 114 |
+
if hdrlen == 0:
|
| 115 |
+
msg = "not a valid Spider file"
|
| 116 |
+
raise SyntaxError(msg)
|
| 117 |
+
except struct.error as e:
|
| 118 |
+
msg = "not a valid Spider file"
|
| 119 |
+
raise SyntaxError(msg) from e
|
| 120 |
+
|
| 121 |
+
h = (99,) + t # add 1 value : spider header index starts at 1
|
| 122 |
+
iform = int(h[5])
|
| 123 |
+
if iform != 1:
|
| 124 |
+
msg = "not a Spider 2D image"
|
| 125 |
+
raise SyntaxError(msg)
|
| 126 |
+
|
| 127 |
+
self._size = int(h[12]), int(h[2]) # size in pixels (width, height)
|
| 128 |
+
self.istack = int(h[24])
|
| 129 |
+
self.imgnumber = int(h[27])
|
| 130 |
+
|
| 131 |
+
if self.istack == 0 and self.imgnumber == 0:
|
| 132 |
+
# stk=0, img=0: a regular 2D image
|
| 133 |
+
offset = hdrlen
|
| 134 |
+
self._nimages = 1
|
| 135 |
+
elif self.istack > 0 and self.imgnumber == 0:
|
| 136 |
+
# stk>0, img=0: Opening the stack for the first time
|
| 137 |
+
self.imgbytes = int(h[12]) * int(h[2]) * 4
|
| 138 |
+
self.hdrlen = hdrlen
|
| 139 |
+
self._nimages = int(h[26])
|
| 140 |
+
# Point to the first image in the stack
|
| 141 |
+
offset = hdrlen * 2
|
| 142 |
+
self.imgnumber = 1
|
| 143 |
+
elif self.istack == 0 and self.imgnumber > 0:
|
| 144 |
+
# stk=0, img>0: an image within the stack
|
| 145 |
+
offset = hdrlen + self.stkoffset
|
| 146 |
+
self.istack = 2 # So Image knows it's still a stack
|
| 147 |
+
else:
|
| 148 |
+
msg = "inconsistent stack header values"
|
| 149 |
+
raise SyntaxError(msg)
|
| 150 |
+
|
| 151 |
+
if self.bigendian:
|
| 152 |
+
self.rawmode = "F;32BF"
|
| 153 |
+
else:
|
| 154 |
+
self.rawmode = "F;32F"
|
| 155 |
+
self._mode = "F"
|
| 156 |
+
|
| 157 |
+
self.tile = [("raw", (0, 0) + self.size, offset, (self.rawmode, 0, 1))]
|
| 158 |
+
self._fp = self.fp # FIXME: hack
|
| 159 |
+
|
| 160 |
+
@property
|
| 161 |
+
def n_frames(self) -> int:
|
| 162 |
+
return self._nimages
|
| 163 |
+
|
| 164 |
+
@property
|
| 165 |
+
def is_animated(self) -> bool:
|
| 166 |
+
return self._nimages > 1
|
| 167 |
+
|
| 168 |
+
# 1st image index is zero (although SPIDER imgnumber starts at 1)
|
| 169 |
+
def tell(self) -> int:
|
| 170 |
+
if self.imgnumber < 1:
|
| 171 |
+
return 0
|
| 172 |
+
else:
|
| 173 |
+
return self.imgnumber - 1
|
| 174 |
+
|
| 175 |
+
def seek(self, frame: int) -> None:
|
| 176 |
+
if self.istack == 0:
|
| 177 |
+
msg = "attempt to seek in a non-stack file"
|
| 178 |
+
raise EOFError(msg)
|
| 179 |
+
if not self._seek_check(frame):
|
| 180 |
+
return
|
| 181 |
+
self.stkoffset = self.hdrlen + frame * (self.hdrlen + self.imgbytes)
|
| 182 |
+
self.fp = self._fp
|
| 183 |
+
self.fp.seek(self.stkoffset)
|
| 184 |
+
self._open()
|
| 185 |
+
|
| 186 |
+
# returns a byte image after rescaling to 0..255
|
| 187 |
+
def convert2byte(self, depth: int = 255) -> Image.Image:
|
| 188 |
+
extrema = self.getextrema()
|
| 189 |
+
assert isinstance(extrema[0], float)
|
| 190 |
+
minimum, maximum = cast(Tuple[float, float], extrema)
|
| 191 |
+
m: float = 1
|
| 192 |
+
if maximum != minimum:
|
| 193 |
+
m = depth / (maximum - minimum)
|
| 194 |
+
b = -m * minimum
|
| 195 |
+
return self.point(lambda i: i * m + b).convert("L")
|
| 196 |
+
|
| 197 |
+
if TYPE_CHECKING:
|
| 198 |
+
from . import ImageTk
|
| 199 |
+
|
| 200 |
+
# returns a ImageTk.PhotoImage object, after rescaling to 0..255
|
| 201 |
+
def tkPhotoImage(self) -> ImageTk.PhotoImage:
|
| 202 |
+
from . import ImageTk
|
| 203 |
+
|
| 204 |
+
return ImageTk.PhotoImage(self.convert2byte(), palette=256)
|
| 205 |
+
|
| 206 |
+
|
| 207 |
+
# --------------------------------------------------------------------
|
| 208 |
+
# Image series
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# given a list of filenames, return a list of images
|
| 212 |
+
def loadImageSeries(filelist: list[str] | None = None) -> list[SpiderImageFile] | None:
|
| 213 |
+
"""create a list of :py:class:`~PIL.Image.Image` objects for use in a montage"""
|
| 214 |
+
if filelist is None or len(filelist) < 1:
|
| 215 |
+
return None
|
| 216 |
+
|
| 217 |
+
imglist = []
|
| 218 |
+
for img in filelist:
|
| 219 |
+
if not os.path.exists(img):
|
| 220 |
+
print(f"unable to find {img}")
|
| 221 |
+
continue
|
| 222 |
+
try:
|
| 223 |
+
with Image.open(img) as im:
|
| 224 |
+
im = im.convert2byte()
|
| 225 |
+
except Exception:
|
| 226 |
+
if not isSpiderImage(img):
|
| 227 |
+
print(f"{img} is not a Spider image file")
|
| 228 |
+
continue
|
| 229 |
+
im.info["filename"] = img
|
| 230 |
+
imglist.append(im)
|
| 231 |
+
return imglist
|
| 232 |
+
|
| 233 |
+
|
| 234 |
+
# --------------------------------------------------------------------
|
| 235 |
+
# For saving images in Spider format
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def makeSpiderHeader(im: Image.Image) -> list[bytes]:
|
| 239 |
+
nsam, nrow = im.size
|
| 240 |
+
lenbyt = nsam * 4 # There are labrec records in the header
|
| 241 |
+
labrec = int(1024 / lenbyt)
|
| 242 |
+
if 1024 % lenbyt != 0:
|
| 243 |
+
labrec += 1
|
| 244 |
+
labbyt = labrec * lenbyt
|
| 245 |
+
nvalues = int(labbyt / 4)
|
| 246 |
+
if nvalues < 23:
|
| 247 |
+
return []
|
| 248 |
+
|
| 249 |
+
hdr = [0.0] * nvalues
|
| 250 |
+
|
| 251 |
+
# NB these are Fortran indices
|
| 252 |
+
hdr[1] = 1.0 # nslice (=1 for an image)
|
| 253 |
+
hdr[2] = float(nrow) # number of rows per slice
|
| 254 |
+
hdr[3] = float(nrow) # number of records in the image
|
| 255 |
+
hdr[5] = 1.0 # iform for 2D image
|
| 256 |
+
hdr[12] = float(nsam) # number of pixels per line
|
| 257 |
+
hdr[13] = float(labrec) # number of records in file header
|
| 258 |
+
hdr[22] = float(labbyt) # total number of bytes in header
|
| 259 |
+
hdr[23] = float(lenbyt) # record length in bytes
|
| 260 |
+
|
| 261 |
+
# adjust for Fortran indexing
|
| 262 |
+
hdr = hdr[1:]
|
| 263 |
+
hdr.append(0.0)
|
| 264 |
+
# pack binary data into a string
|
| 265 |
+
return [struct.pack("f", v) for v in hdr]
|
| 266 |
+
|
| 267 |
+
|
| 268 |
+
def _save(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 269 |
+
if im.mode[0] != "F":
|
| 270 |
+
im = im.convert("F")
|
| 271 |
+
|
| 272 |
+
hdr = makeSpiderHeader(im)
|
| 273 |
+
if len(hdr) < 256:
|
| 274 |
+
msg = "Error creating Spider header"
|
| 275 |
+
raise OSError(msg)
|
| 276 |
+
|
| 277 |
+
# write the SPIDER header
|
| 278 |
+
fp.writelines(hdr)
|
| 279 |
+
|
| 280 |
+
rawmode = "F;32NF" # 32-bit native floating point
|
| 281 |
+
ImageFile._save(im, fp, [("raw", (0, 0) + im.size, 0, (rawmode, 0, 1))])
|
| 282 |
+
|
| 283 |
+
|
| 284 |
+
def _save_spider(im: Image.Image, fp: IO[bytes], filename: str | bytes) -> None:
|
| 285 |
+
# get the filename extension and register it with Image
|
| 286 |
+
filename_ext = os.path.splitext(filename)[1]
|
| 287 |
+
ext = filename_ext.decode() if isinstance(filename_ext, bytes) else filename_ext
|
| 288 |
+
Image.register_extension(SpiderImageFile.format, ext)
|
| 289 |
+
_save(im, fp, filename)
|
| 290 |
+
|
| 291 |
+
|
| 292 |
+
# --------------------------------------------------------------------
|
| 293 |
+
|
| 294 |
+
|
| 295 |
+
Image.register_open(SpiderImageFile.format, SpiderImageFile)
|
| 296 |
+
Image.register_save(SpiderImageFile.format, _save_spider)
|
| 297 |
+
|
| 298 |
+
if __name__ == "__main__":
|
| 299 |
+
if len(sys.argv) < 2:
|
| 300 |
+
print("Syntax: python3 SpiderImagePlugin.py [infile] [outfile]")
|
| 301 |
+
sys.exit()
|
| 302 |
+
|
| 303 |
+
filename = sys.argv[1]
|
| 304 |
+
if not isSpiderImage(filename):
|
| 305 |
+
print("input image must be in Spider format")
|
| 306 |
+
sys.exit()
|
| 307 |
+
|
| 308 |
+
with Image.open(filename) as im:
|
| 309 |
+
print(f"image: {im}")
|
| 310 |
+
print(f"format: {im.format}")
|
| 311 |
+
print(f"size: {im.size}")
|
| 312 |
+
print(f"mode: {im.mode}")
|
| 313 |
+
print("max, min: ", end=" ")
|
| 314 |
+
print(im.getextrema())
|
| 315 |
+
|
| 316 |
+
if len(sys.argv) > 2:
|
| 317 |
+
outfile = sys.argv[2]
|
| 318 |
+
|
| 319 |
+
# perform some image operation
|
| 320 |
+
im = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
|
| 321 |
+
print(
|
| 322 |
+
f"saving a flipped version of {os.path.basename(filename)} "
|
| 323 |
+
f"as {outfile} "
|
| 324 |
+
)
|
| 325 |
+
im.save(outfile, SpiderImageFile.format)
|
.venv/lib/python3.11/site-packages/PIL/SunImagePlugin.py
ADDED
|
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 fl Fixed palette loading (from Jean-Claude Rimbault)
|
| 12 |
+
#
|
| 13 |
+
# Copyright (c) 1997-2001 by Secret Labs AB
|
| 14 |
+
# Copyright (c) 1995-1996 by Fredrik Lundh
|
| 15 |
+
#
|
| 16 |
+
# See the README file for information on usage and redistribution.
|
| 17 |
+
#
|
| 18 |
+
from __future__ import annotations
|
| 19 |
+
|
| 20 |
+
from . import Image, ImageFile, ImagePalette
|
| 21 |
+
from ._binary import i32be as i32
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def _accept(prefix: bytes) -> bool:
|
| 25 |
+
return len(prefix) >= 4 and i32(prefix) == 0x59A66A95
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
##
|
| 29 |
+
# Image plugin for Sun raster files.
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
class SunImageFile(ImageFile.ImageFile):
|
| 33 |
+
format = "SUN"
|
| 34 |
+
format_description = "Sun Raster File"
|
| 35 |
+
|
| 36 |
+
def _open(self) -> None:
|
| 37 |
+
# The Sun Raster file header is 32 bytes in length
|
| 38 |
+
# and has the following format:
|
| 39 |
+
|
| 40 |
+
# typedef struct _SunRaster
|
| 41 |
+
# {
|
| 42 |
+
# DWORD MagicNumber; /* Magic (identification) number */
|
| 43 |
+
# DWORD Width; /* Width of image in pixels */
|
| 44 |
+
# DWORD Height; /* Height of image in pixels */
|
| 45 |
+
# DWORD Depth; /* Number of bits per pixel */
|
| 46 |
+
# DWORD Length; /* Size of image data in bytes */
|
| 47 |
+
# DWORD Type; /* Type of raster file */
|
| 48 |
+
# DWORD ColorMapType; /* Type of color map */
|
| 49 |
+
# DWORD ColorMapLength; /* Size of the color map in bytes */
|
| 50 |
+
# } SUNRASTER;
|
| 51 |
+
|
| 52 |
+
assert self.fp is not None
|
| 53 |
+
|
| 54 |
+
# HEAD
|
| 55 |
+
s = self.fp.read(32)
|
| 56 |
+
if not _accept(s):
|
| 57 |
+
msg = "not an SUN raster file"
|
| 58 |
+
raise SyntaxError(msg)
|
| 59 |
+
|
| 60 |
+
offset = 32
|
| 61 |
+
|
| 62 |
+
self._size = i32(s, 4), i32(s, 8)
|
| 63 |
+
|
| 64 |
+
depth = i32(s, 12)
|
| 65 |
+
# data_length = i32(s, 16) # unreliable, ignore.
|
| 66 |
+
file_type = i32(s, 20)
|
| 67 |
+
palette_type = i32(s, 24) # 0: None, 1: RGB, 2: Raw/arbitrary
|
| 68 |
+
palette_length = i32(s, 28)
|
| 69 |
+
|
| 70 |
+
if depth == 1:
|
| 71 |
+
self._mode, rawmode = "1", "1;I"
|
| 72 |
+
elif depth == 4:
|
| 73 |
+
self._mode, rawmode = "L", "L;4"
|
| 74 |
+
elif depth == 8:
|
| 75 |
+
self._mode = rawmode = "L"
|
| 76 |
+
elif depth == 24:
|
| 77 |
+
if file_type == 3:
|
| 78 |
+
self._mode, rawmode = "RGB", "RGB"
|
| 79 |
+
else:
|
| 80 |
+
self._mode, rawmode = "RGB", "BGR"
|
| 81 |
+
elif depth == 32:
|
| 82 |
+
if file_type == 3:
|
| 83 |
+
self._mode, rawmode = "RGB", "RGBX"
|
| 84 |
+
else:
|
| 85 |
+
self._mode, rawmode = "RGB", "BGRX"
|
| 86 |
+
else:
|
| 87 |
+
msg = "Unsupported Mode/Bit Depth"
|
| 88 |
+
raise SyntaxError(msg)
|
| 89 |
+
|
| 90 |
+
if palette_length:
|
| 91 |
+
if palette_length > 1024:
|
| 92 |
+
msg = "Unsupported Color Palette Length"
|
| 93 |
+
raise SyntaxError(msg)
|
| 94 |
+
|
| 95 |
+
if palette_type != 1:
|
| 96 |
+
msg = "Unsupported Palette Type"
|
| 97 |
+
raise SyntaxError(msg)
|
| 98 |
+
|
| 99 |
+
offset = offset + palette_length
|
| 100 |
+
self.palette = ImagePalette.raw("RGB;L", self.fp.read(palette_length))
|
| 101 |
+
if self.mode == "L":
|
| 102 |
+
self._mode = "P"
|
| 103 |
+
rawmode = rawmode.replace("L", "P")
|
| 104 |
+
|
| 105 |
+
# 16 bit boundaries on stride
|
| 106 |
+
stride = ((self.size[0] * depth + 15) // 16) * 2
|
| 107 |
+
|
| 108 |
+
# file type: Type is the version (or flavor) of the bitmap
|
| 109 |
+
# file. The following values are typically found in the Type
|
| 110 |
+
# field:
|
| 111 |
+
# 0000h Old
|
| 112 |
+
# 0001h Standard
|
| 113 |
+
# 0002h Byte-encoded
|
| 114 |
+
# 0003h RGB format
|
| 115 |
+
# 0004h TIFF format
|
| 116 |
+
# 0005h IFF format
|
| 117 |
+
# FFFFh Experimental
|
| 118 |
+
|
| 119 |
+
# Old and standard are the same, except for the length tag.
|
| 120 |
+
# byte-encoded is run-length-encoded
|
| 121 |
+
# RGB looks similar to standard, but RGB byte order
|
| 122 |
+
# TIFF and IFF mean that they were converted from T/IFF
|
| 123 |
+
# Experimental means that it's something else.
|
| 124 |
+
# (https://www.fileformat.info/format/sunraster/egff.htm)
|
| 125 |
+
|
| 126 |
+
if file_type in (0, 1, 3, 4, 5):
|
| 127 |
+
self.tile = [("raw", (0, 0) + self.size, offset, (rawmode, stride))]
|
| 128 |
+
elif file_type == 2:
|
| 129 |
+
self.tile = [("sun_rle", (0, 0) + self.size, offset, rawmode)]
|
| 130 |
+
else:
|
| 131 |
+
msg = "Unsupported Sun Raster file type"
|
| 132 |
+
raise SyntaxError(msg)
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
#
|
| 136 |
+
# registry
|
| 137 |
+
|
| 138 |
+
|
| 139 |
+
Image.register_open(SunImageFile.format, SunImageFile, _accept)
|
| 140 |
+
|
| 141 |
+
Image.register_extension(SunImageFile.format, ".ras")
|
.venv/lib/python3.11/site-packages/PIL/TarIO.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#
|
| 2 |
+
# The Python Imaging Library.
|
| 3 |
+
# $Id$
|
| 4 |
+
#
|
| 5 |
+
# read files from within a tar file
|
| 6 |
+
#
|
| 7 |
+
# History:
|
| 8 |
+
# 95-06-18 fl Created
|
| 9 |
+
# 96-05-28 fl Open files in binary mode
|
| 10 |
+
#
|
| 11 |
+
# Copyright (c) Secret Labs AB 1997.
|
| 12 |
+
# Copyright (c) Fredrik Lundh 1995-96.
|
| 13 |
+
#
|
| 14 |
+
# See the README file for information on usage and redistribution.
|
| 15 |
+
#
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import io
|
| 19 |
+
|
| 20 |
+
from . import ContainerIO
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
class TarIO(ContainerIO.ContainerIO[bytes]):
|
| 24 |
+
"""A file object that provides read access to a given member of a TAR file."""
|
| 25 |
+
|
| 26 |
+
def __init__(self, tarfile: str, file: str) -> None:
|
| 27 |
+
"""
|
| 28 |
+
Create file object.
|
| 29 |
+
|
| 30 |
+
:param tarfile: Name of TAR file.
|
| 31 |
+
:param file: Name of member file.
|
| 32 |
+
"""
|
| 33 |
+
self.fh = open(tarfile, "rb")
|
| 34 |
+
|
| 35 |
+
while True:
|
| 36 |
+
s = self.fh.read(512)
|
| 37 |
+
if len(s) != 512:
|
| 38 |
+
msg = "unexpected end of tar file"
|
| 39 |
+
raise OSError(msg)
|
| 40 |
+
|
| 41 |
+
name = s[:100].decode("utf-8")
|
| 42 |
+
i = name.find("\0")
|
| 43 |
+
if i == 0:
|
| 44 |
+
msg = "cannot find subfile"
|
| 45 |
+
raise OSError(msg)
|
| 46 |
+
if i > 0:
|
| 47 |
+
name = name[:i]
|
| 48 |
+
|
| 49 |
+
size = int(s[124:135], 8)
|
| 50 |
+
|
| 51 |
+
if file == name:
|
| 52 |
+
break
|
| 53 |
+
|
| 54 |
+
self.fh.seek((size + 511) & (~511), io.SEEK_CUR)
|
| 55 |
+
|
| 56 |
+
# Open region
|
| 57 |
+
super().__init__(self.fh, self.fh.tell(), size)
|
| 58 |
+
|
| 59 |
+
# Context manager support
|
| 60 |
+
def __enter__(self) -> TarIO:
|
| 61 |
+
return self
|
| 62 |
+
|
| 63 |
+
def __exit__(self, *args: object) -> None:
|
| 64 |
+
self.close()
|
| 65 |
+
|
| 66 |
+
def close(self) -> None:
|
| 67 |
+
self.fh.close()
|
.venv/lib/python3.11/site-packages/PIL/__init__.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Pillow (Fork of the Python Imaging Library)
|
| 2 |
+
|
| 3 |
+
Pillow is the friendly PIL fork by Jeffrey A. Clark and contributors.
|
| 4 |
+
https://github.com/python-pillow/Pillow/
|
| 5 |
+
|
| 6 |
+
Pillow is forked from PIL 1.1.7.
|
| 7 |
+
|
| 8 |
+
PIL is the Python Imaging Library by Fredrik Lundh and contributors.
|
| 9 |
+
Copyright (c) 1999 by Secret Labs AB.
|
| 10 |
+
|
| 11 |
+
Use PIL.__version__ for this Pillow version.
|
| 12 |
+
|
| 13 |
+
;-)
|
| 14 |
+
"""
|
| 15 |
+
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
from . import _version
|
| 19 |
+
|
| 20 |
+
# VERSION was removed in Pillow 6.0.0.
|
| 21 |
+
# PILLOW_VERSION was removed in Pillow 9.0.0.
|
| 22 |
+
# Use __version__ instead.
|
| 23 |
+
__version__ = _version.__version__
|
| 24 |
+
del _version
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
_plugins = [
|
| 28 |
+
"BlpImagePlugin",
|
| 29 |
+
"BmpImagePlugin",
|
| 30 |
+
"BufrStubImagePlugin",
|
| 31 |
+
"CurImagePlugin",
|
| 32 |
+
"DcxImagePlugin",
|
| 33 |
+
"DdsImagePlugin",
|
| 34 |
+
"EpsImagePlugin",
|
| 35 |
+
"FitsImagePlugin",
|
| 36 |
+
"FliImagePlugin",
|
| 37 |
+
"FpxImagePlugin",
|
| 38 |
+
"FtexImagePlugin",
|
| 39 |
+
"GbrImagePlugin",
|
| 40 |
+
"GifImagePlugin",
|
| 41 |
+
"GribStubImagePlugin",
|
| 42 |
+
"Hdf5StubImagePlugin",
|
| 43 |
+
"IcnsImagePlugin",
|
| 44 |
+
"IcoImagePlugin",
|
| 45 |
+
"ImImagePlugin",
|
| 46 |
+
"ImtImagePlugin",
|
| 47 |
+
"IptcImagePlugin",
|
| 48 |
+
"JpegImagePlugin",
|
| 49 |
+
"Jpeg2KImagePlugin",
|
| 50 |
+
"McIdasImagePlugin",
|
| 51 |
+
"MicImagePlugin",
|
| 52 |
+
"MpegImagePlugin",
|
| 53 |
+
"MpoImagePlugin",
|
| 54 |
+
"MspImagePlugin",
|
| 55 |
+
"PalmImagePlugin",
|
| 56 |
+
"PcdImagePlugin",
|
| 57 |
+
"PcxImagePlugin",
|
| 58 |
+
"PdfImagePlugin",
|
| 59 |
+
"PixarImagePlugin",
|
| 60 |
+
"PngImagePlugin",
|
| 61 |
+
"PpmImagePlugin",
|
| 62 |
+
"PsdImagePlugin",
|
| 63 |
+
"QoiImagePlugin",
|
| 64 |
+
"SgiImagePlugin",
|
| 65 |
+
"SpiderImagePlugin",
|
| 66 |
+
"SunImagePlugin",
|
| 67 |
+
"TgaImagePlugin",
|
| 68 |
+
"TiffImagePlugin",
|
| 69 |
+
"WebPImagePlugin",
|
| 70 |
+
"WmfImagePlugin",
|
| 71 |
+
"XbmImagePlugin",
|
| 72 |
+
"XpmImagePlugin",
|
| 73 |
+
"XVThumbImagePlugin",
|
| 74 |
+
]
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
class UnidentifiedImageError(OSError):
|
| 78 |
+
"""
|
| 79 |
+
Raised in :py:meth:`PIL.Image.open` if an image cannot be opened and identified.
|
| 80 |
+
|
| 81 |
+
If a PNG image raises this error, setting :data:`.ImageFile.LOAD_TRUNCATED_IMAGES`
|
| 82 |
+
to true may allow the image to be opened after all. The setting will ignore missing
|
| 83 |
+
data and checksum failures.
|
| 84 |
+
"""
|
| 85 |
+
|
| 86 |
+
pass
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/BmpImagePlugin.cpython-311.pyc
ADDED
|
Binary file (18.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/EpsImagePlugin.cpython-311.pyc
ADDED
|
Binary file (18.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/GimpGradientFile.cpython-311.pyc
ADDED
|
Binary file (6.59 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/GimpPaletteFile.cpython-311.pyc
ADDED
|
Binary file (2.64 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/GribStubImagePlugin.cpython-311.pyc
ADDED
|
Binary file (3.03 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/IcnsImagePlugin.cpython-311.pyc
ADDED
|
Binary file (18.6 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/IcoImagePlugin.cpython-311.pyc
ADDED
|
Binary file (15.3 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageChops.cpython-311.pyc
ADDED
|
Binary file (11.9 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageDraw.cpython-311.pyc
ADDED
|
Binary file (47.5 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageQt.cpython-311.pyc
ADDED
|
Binary file (8.74 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageSequence.cpython-311.pyc
ADDED
|
Binary file (3.95 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageShow.cpython-311.pyc
ADDED
|
Binary file (15.7 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/ImageStat.cpython-311.pyc
ADDED
|
Binary file (8.67 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/IptcImagePlugin.cpython-311.pyc
ADDED
|
Binary file (9.31 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/JpegPresets.cpython-311.pyc
ADDED
|
Binary file (8.44 kB). View file
|
|
|
.venv/lib/python3.11/site-packages/PIL/__pycache__/McIdasImagePlugin.cpython-311.pyc
ADDED
|
Binary file (2.45 kB). View file
|
|
|