Skip to content

Commit a5a6ca1

Browse files
authored
Merge pull request #7493 from radarhere/frombytes
Fixed frombytes() for images with a zero dimension
2 parents 14c539e + 91f115b commit a5a6ca1

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

Tests/test_image.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,13 @@ 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+
913+
im = Image.new("RGB", size)
914+
im.frombytes(b"")
915+
909916
def test_has_transparency_data(self):
910917
for mode in ("1", "L", "P", "RGB"):
911918
im = Image.new(mode, (1, 1))

src/PIL/Image.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,9 @@ def frombytes(self, data, decoder_name="raw", *args):
791791
but loads data into this image instead of creating a new image object.
792792
"""
793793

794+
if self.width == 0 or self.height == 0:
795+
return
796+
794797
# may pass tuple instead of argument list
795798
if len(args) == 1 and isinstance(args[0], tuple):
796799
args = args[0]
@@ -2967,15 +2970,16 @@ def frombytes(mode, size, data, decoder_name="raw", *args):
29672970

29682971
_check_size(size)
29692972

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

2974-
if decoder_name == "raw" and args == ():
2975-
args = mode
2979+
if decoder_name == "raw" and args == ():
2980+
args = mode
29762981

2977-
im = new(mode, size)
2978-
im.frombytes(data, decoder_name, args)
2982+
im.frombytes(data, decoder_name, args)
29792983
return im
29802984

29812985

0 commit comments

Comments
 (0)