Skip to content

Commit

Permalink
Return an empty bytestring from tobytes() for an empty image
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jan 7, 2022
1 parent 0648692 commit d015458
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,11 @@ def test_exif_load_from_fp(self):
34665: 196,
}

@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
def test_zero_tobytes(self, size):
im = Image.new("RGB", size)
assert im.tobytes() == b""

def test_categories_deprecation(self):
with pytest.warns(DeprecationWarning):
assert hopper().category == 0
Expand Down
3 changes: 3 additions & 0 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,9 @@ def tobytes(self, encoder_name="raw", *args):
:returns: A :py:class:`bytes` object.
"""

if self.width == 0 or self.height == 0:
return b""

# may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
Expand Down

0 comments on commit d015458

Please sign in to comment.