Skip to content

Commit 41c73d1

Browse files
veeceeyclaude
andcommitted
Fix UnboundLocalError in _border for invalid tuple lengths and document rgba() color format
The _border helper in ImageOps raised UnboundLocalError when given a tuple with a length other than 2 or 4 (e.g. 1-tuple or 3-tuple). This changes it to raise a clear ValueError instead. Also adds documentation for the rgba() color format in ImageColor, which was supported in code and tested but missing from the docs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent f78663b commit 41c73d1

3 files changed

Lines changed: 14 additions & 0 deletions

File tree

Tests/test_imageops.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,13 @@ def test_expand_palette(border: int | tuple[int, int, int, int]) -> None:
256256
assert_image_equal(im_cropped, im)
257257

258258

259+
@pytest.mark.parametrize("border", ((1,), (1, 2, 3), (1, 2, 3, 4, 5)))
260+
def test_expand_invalid_border(border: tuple[int, ...]) -> None:
261+
with Image.open("Tests/images/hopper.ppm") as im:
262+
with pytest.raises(ValueError):
263+
ImageOps.expand(im, border)
264+
265+
259266
def test_colorize_2color() -> None:
260267
# Test the colorizing function with 2-color functionality
261268

docs/reference/ImageColor.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ The ImageColor module supports the following string formats:
2727
as three percentages (0% to 100%). For example, ``rgb(255,0,0)`` and
2828
``rgb(100%,0%,0%)`` both specify pure red.
2929

30+
* RGBA functions, given as ``rgba(red, green, blue, alpha)`` where the color
31+
values and the alpha value are integers in the range 0 to 255. For example,
32+
``rgba(255,0,0,128)`` specifies pure red with 50% opacity.
33+
3034
* Hue-Saturation-Lightness (HSL) functions, given as ``hsl(hue, saturation%,
3135
lightness%)`` where hue is the color given as an angle between 0 and 360
3236
(red=0, green=120, blue=240), saturation is a value between 0% and 100%

src/PIL/ImageOps.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def _border(border: int | tuple[int, ...]) -> tuple[int, int, int, int]:
3636
left, top = right, bottom = border
3737
elif len(border) == 4:
3838
left, top, right, bottom = border
39+
else:
40+
msg = "border must be an integer or a 2- or 4-tuple"
41+
raise ValueError(msg)
3942
else:
4043
left = top = right = bottom = border
4144
return left, top, right, bottom

0 commit comments

Comments
 (0)