Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 4 additions & 7 deletions src/PIL/ImageColor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +65 to +66

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well spare the unpack-repack:

Suggested change
r, g, b = bytes.fromhex(color[1:])
return r, g, b
return tuple(bytes.fromhex(color[1:])) # 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
Comment on lines +69 to +70

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well spare the unpack-repack here too:

Suggested change
r, g, b, a = bytes.fromhex(color[1:])
return r, g, b, a
return tuple(bytes.fromhex(color[1:])) # r, g, b, a


m = re.match(r"rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$", color)
if m:
Expand Down
3 changes: 1 addition & 2 deletions src/PIL/ImageFilter.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
from __future__ import annotations

import abc
import functools
from collections.abc import Sequence
from typing import cast

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion src/PIL/PdfParser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down