Skip to content

Commit 5908ccc

Browse files
authored
Merge pull request #5938 from radarhere/zero
Return an empty bytestring from tobytes() for an empty image
2 parents 3ca3370 + b516059 commit 5908ccc

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Tests/test_image.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,11 @@ def test_exif_load_from_fp(self):
786786
34665: 196,
787787
}
788788

789+
@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
790+
def test_zero_tobytes(self, size):
791+
im = Image.new("RGB", size)
792+
assert im.tobytes() == b""
793+
789794
def test_categories_deprecation(self):
790795
with pytest.warns(DeprecationWarning):
791796
assert hopper().category == 0

src/PIL/Image.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ def tobytes(self, encoder_name="raw", *args):
716716

717717
self.load()
718718

719+
if self.width == 0 or self.height == 0:
720+
return b""
721+
719722
# unpack data
720723
e = _getencoder(self.mode, encoder_name, args)
721724
e.setimage(self.im)

0 commit comments

Comments
 (0)