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 b516059
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 @@ -716,6 +716,9 @@ def tobytes(self, encoder_name="raw", *args):

self.load()

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

# unpack data
e = _getencoder(self.mode, encoder_name, args)
e.setimage(self.im)
Expand Down

0 comments on commit b516059

Please sign in to comment.