Skip to content

Commit 5071692

Browse files
committed
Fixed Image.frombytes() for images with a zero dimension
1 parent d05ff50 commit 5071692

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

Tests/test_image.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,10 @@ def test_zero_tobytes(self, size):
906906
im = Image.new("RGB", size)
907907
assert im.tobytes() == b""
908908

909+
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
910+
def test_zero_frombytes(self, size):
911+
Image.frombytes("RGB", size, b"")
912+
909913
def test_has_transparency_data(self):
910914
for mode in ("1", "L", "P", "RGB"):
911915
im = Image.new(mode, (1, 1))

src/PIL/Image.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2967,15 +2967,16 @@ def frombytes(mode, size, data, decoder_name="raw", *args):
29672967

29682968
_check_size(size)
29692969

2970-
# may pass tuple instead of argument list
2971-
if len(args) == 1 and isinstance(args[0], tuple):
2972-
args = args[0]
2970+
im = new(mode, size)
2971+
if im.width != 0 and im.height != 0:
2972+
# may pass tuple instead of argument list
2973+
if len(args) == 1 and isinstance(args[0], tuple):
2974+
args = args[0]
29732975

2974-
if decoder_name == "raw" and args == ():
2975-
args = mode
2976+
if decoder_name == "raw" and args == ():
2977+
args = mode
29762978

2977-
im = new(mode, size)
2978-
im.frombytes(data, decoder_name, args)
2979+
im.frombytes(data, decoder_name, args)
29792980
return im
29802981

29812982

0 commit comments

Comments
 (0)