Skip to content
Closed
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: 8 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io
import logging
import math
import os
import shutil
import sys
Expand Down Expand Up @@ -619,6 +620,13 @@ def test_blend_i16(self, mode: str) -> None:
assert Image.blend(im1, im2, -2.0).getpixel((0, 0)) == 0
assert Image.blend(im1, im2, 6.0).getpixel((0, 0)) == 65535

def test_blend_invalid_alpha(self) -> None:
im1 = Image.new("L", (2, 2), 10)
im2 = Image.new("L", (2, 2), 20)
for alpha in (math.inf, math.nan):
with pytest.raises(ValueError, match="invalid alpha"):
Image.blend(im1, im2, alpha)

def test_check_size(self) -> None:
# Checking that the _check_size function throws value errors when we want it to
with pytest.raises(ValueError):
Expand Down
4 changes: 4 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -3848,6 +3848,10 @@ def blend(im1: Image, im2: Image, alpha: float) -> Image:
:returns: An :py:class:`~PIL.Image.Image` object.
"""

if math.isinf(alpha) or math.isnan(alpha):
msg = "invalid alpha"
raise ValueError(msg)

im1.load()
im2.load()
return im1._new(core.blend(im1.im, im2.im, alpha))
Expand Down
Loading