Skip to content

Image.frombytes throws on empty image #7492

@dragazo

Description

@dragazo

What did you do?

I have some code I use to render text as images. When trying to render an empty string, it fails on Image.frombytes

What did you expect to happen?

I expected Image.frombytes with size (0, 0) and bytes [] (as a numpy array of uint8) to successfully yield an empty image.

What actually happened?

An exception is thrown:

Traceback (most recent call last):
  File "/home/devin/Desktop/pyblox/netsblox/turtle.py", line 89, in wrapped
    fn(*args, **kwargs)
  File "/home/devin/Desktop/pyblox/netsblox/turtle.py", line 119, in wrapped
    return f(*args, **kwargs)
  File "<stdin>", line 34, in my_onstart
  File "/home/devin/Desktop/pyblox/netsblox/turtle.py", line 1250, in say
    imgs = [_render_text(x, size = 8, color = (0, 0, 0)) for x in lines]
  File "/home/devin/Desktop/pyblox/netsblox/turtle.py", line 1250, in <listcomp>
    imgs = [_render_text(x, size = 8, color = (0, 0, 0)) for x in lines]
  File "/home/devin/Desktop/pyblox/netsblox/turtle.py", line 80, in _render_text
    text_mask = Image.frombytes('L', text_mask.size, _np.array(text_mask).astype(_np.uint8)) # convert ImagingCore to Image
  File "/home/devin/.local/lib/python3.8/site-packages/PIL/Image.py", line 2976, in frombytes
    im.frombytes(data, decoder_name, args)
  File "/home/devin/.local/lib/python3.8/site-packages/PIL/Image.py", line 804, in frombytes
    d.setimage(self.im)
ValueError: tile cannot extend outside image

What are your OS, Python and Pillow versions?

  • OS: Linux Mint 20
  • Python: 3.8.10
  • Pillow: 10.1.0
font = ImageFont.truetype('example-font.otf')
text = ''
color = (0, 0, 0)

text_mask = font.getmask(text, mode = 'L')
text_mask = Image.frombytes('L', text_mask.size, _np.array(text_mask).astype(_np.uint8)) # fails here with exception

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions