Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fixed frombytes() for images with a zero dimension #7493

Merged
merged 2 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Tests/test_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,13 @@ def test_zero_tobytes(self, size):
im = Image.new("RGB", size)
assert im.tobytes() == b""

@pytest.mark.parametrize("size", ((1, 0), (0, 1), (0, 0)))
def test_zero_frombytes(self, size):
Image.frombytes("RGB", size, b"")

im = Image.new("RGB", size)
im.frombytes(b"")

def test_has_transparency_data(self):
for mode in ("1", "L", "P", "RGB"):
im = Image.new(mode, (1, 1))
Expand Down
18 changes: 11 additions & 7 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ def frombytes(self, data, decoder_name="raw", *args):
but loads data into this image instead of creating a new image object.
"""

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

# may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
Expand Down Expand Up @@ -2967,15 +2970,16 @@ def frombytes(mode, size, data, decoder_name="raw", *args):

_check_size(size)

# may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]
im = new(mode, size)
if im.width != 0 and im.height != 0:
# may pass tuple instead of argument list
if len(args) == 1 and isinstance(args[0], tuple):
args = args[0]

if decoder_name == "raw" and args == ():
args = mode
if decoder_name == "raw" and args == ():
args = mode

im = new(mode, size)
im.frombytes(data, decoder_name, args)
im.frombytes(data, decoder_name, args)
return im


Expand Down