diff --git a/src/PIL/Image.py b/src/PIL/Image.py index ddbaec20a05..3493df1e467 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -708,7 +708,7 @@ def close(self) -> None: more information. """ if getattr(self, "map", None): - if sys.platform == "win32" and hasattr(sys, "pypy_version_info"): + if sys.platform == "win32" and sys.implementation.name == "pypy": self.map.close() self.map: mmap.mmap | None = None diff --git a/src/PIL/ImageColor.py b/src/PIL/ImageColor.py index ef1548c7e23..c2a84100f54 100644 --- a/src/PIL/ImageColor.py +++ b/src/PIL/ImageColor.py @@ -62,15 +62,12 @@ def getrgb(color: str) -> tuple[int, int, int] | tuple[int, int, int, int]: ) if re.match("#[a-f0-9]{6}$", color): - return int(color[1:3], 16), int(color[3:5], 16), int(color[5:7], 16) + r, g, b = bytes.fromhex(color[1:]) + return r, g, b if re.match("#[a-f0-9]{8}$", color): - return ( - int(color[1:3], 16), - int(color[3:5], 16), - int(color[5:7], 16), - int(color[7:9], 16), - ) + r, g, b, a = bytes.fromhex(color[1:]) + return r, g, b, a m = re.match(r"rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color) if m: diff --git a/src/PIL/ImageFilter.py b/src/PIL/ImageFilter.py index 428ceb1aaa9..ffdb7ffca10 100644 --- a/src/PIL/ImageFilter.py +++ b/src/PIL/ImageFilter.py @@ -17,7 +17,6 @@ from __future__ import annotations import abc -import functools from collections.abc import Sequence from typing import cast @@ -79,7 +78,7 @@ def __init__( ) -> None: if scale is None: # default scale is sum of kernel - scale = functools.reduce(lambda a, b: a + b, kernel) + scale = sum(kernel) if size[0] * size[1] != len(kernel): msg = "not enough coefficients in kernel" raise ValueError(msg) diff --git a/src/PIL/ImageOps.py b/src/PIL/ImageOps.py index cdec4d5dc7d..fea9d1258bf 100644 --- a/src/PIL/ImageOps.py +++ b/src/PIL/ImageOps.py @@ -18,8 +18,6 @@ # from __future__ import annotations -import functools -import operator import re from collections.abc import Sequence from typing import Literal, Protocol, cast, overload @@ -120,9 +118,7 @@ def autocontrast( if not isinstance(cutoff, tuple): cutoff = (cutoff, cutoff) # get number of pixels - n = 0 - for ix in range(256): - n = n + h[ix] + n = sum(h) # remove cutoff% pixels from the low end cut = int(n * cutoff[0] // 100) for lo in range(256): @@ -494,7 +490,7 @@ def equalize(image: Image.Image, mask: Image.Image | None = None) -> Image.Image if len(histo) <= 1: lut.extend(list(range(256))) else: - step = (functools.reduce(operator.add, histo) - histo[-1]) // 255 + step = (sum(histo) - histo[-1]) // 255 if not step: lut.extend(list(range(256))) else: diff --git a/src/PIL/PdfParser.py b/src/PIL/PdfParser.py index 5ba70ba209d..132b6ac8b78 100644 --- a/src/PIL/PdfParser.py +++ b/src/PIL/PdfParser.py @@ -177,7 +177,7 @@ def keys(self) -> set[int]: def write(self, f: IO[bytes]) -> int: keys = sorted(set(self.new_entries.keys()) | set(self.deleted_entries.keys())) - deleted_keys = sorted(set(self.deleted_entries.keys())) + deleted_keys = sorted(self.deleted_entries) startxref = f.tell() f.write(b"xref\n") while keys: