Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

putalpha does not allow other color values #37

Merged
merged 2 commits into from
Jun 12, 2024
Merged
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
8 changes: 3 additions & 5 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -1933,8 +1933,7 @@ def putalpha(self, alpha: Image | int) -> None:
The new layer must be either "L" or "1".

:param alpha: The new alpha layer. This can either be an "L" or "1"
image having the same size as this image, or an integer or
other color value.
image having the same size as this image, or an integer.
"""

self._ensure_mutable()
Expand Down Expand Up @@ -2075,9 +2074,8 @@ def putpixel(self, xy: tuple[int, int], value: float | tuple[int, ...]) -> None:
if self.mode == "PA":
alpha = value[3] if len(value) == 4 else 255
value = value[:3]
value = self.palette.getcolor(value, self)
if self.mode == "PA":
value = (value, alpha) # type: ignore[assignment]
palette_index = self.palette.getcolor(value, self)
value = (palette_index, alpha) if self.mode == "PA" else palette_index
return self.im.putpixel(xy, value)

def remap_palette(self, dest_map, source_palette=None):
Expand Down
5 changes: 3 additions & 2 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
all_screens: bool = False,
xdisplay: str | None = None,
) -> Image.Image:
im: Image.Image
if xdisplay is None:
if sys.platform == "darwin":
fh, filepath = tempfile.mkstemp(".png")
Expand All @@ -42,7 +43,7 @@
left, top, right, bottom = bbox
args += ["-R", f"{left},{top},{right-left},{bottom-top}"]
subprocess.call(args + ["-x", filepath])
im: Image.Image = Image.open(filepath)
im = Image.open(filepath)
im.load()
os.unlink(filepath)
if bbox:
Expand Down Expand Up @@ -84,7 +85,7 @@
fh, filepath = tempfile.mkstemp(".png")
os.close(fh)
subprocess.call(["gnome-screenshot", "-f", filepath])
im: Image.Image = Image.open(filepath) # type: ignore[no-redef, unused-ignore]
im = Image.open(filepath)

Check warning on line 88 in src/PIL/ImageGrab.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L88

Added line #L88 was not covered by tests
im.load()
os.unlink(filepath)
if bbox:
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/PyAccess.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ def __setitem__(self, xy: tuple[int, int], color: float | tuple[int, ...]) -> No
if self._im.mode == "PA":
alpha = color[3] if len(color) == 4 else 255
color = color[:3]
color = self._palette.getcolor(color, self._img)
if self._im.mode == "PA":
color = (color, alpha) # type: ignore[assignment]
palette_index = self._palette.getcolor(color, self._img)
color = (palette_index, alpha) if self._im.mode == "PA" else palette_index

return self.set_pixel(x, y, color)

Expand Down
Loading