Skip to content

ValueError: buffer is not large enough #6507

@arry-lee

Description

@arry-lee

What did you do?

I tried to open a bmp image and show it.

What did you expect to happen?

The image is opened and loaded normally.

What actually happened?

ValueError: buffer is not large enough

Traceback (most recent call last):
  File "D:\Anaconda3\envs\paddle_env\lib\site-packages\IPython\core\interactiveshell.py", line 3444, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-2a2b01505643>", line 1, in <module>
    runfile('E:/00IT/P/uniform/tests/test_image.py', wdir='E:/00IT/P/uniform/tests')
  File "D:\PyCharm\plugins\python\helpers\pydev\_pydev_bundle\pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "D:\PyCharm\plugins\python\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "E:/00IT/P/uniform/tests/test_image.py", line 13, in <module>
    img.putalpha(mask_image)
  File "D:\Anaconda3\envs\paddle_env\lib\site-packages\PIL\Image.py", line 1717, in putalpha
    alpha.load()
  File "D:\Anaconda3\envs\paddle_env\lib\site-packages\PIL\ImageFile.py", line 200, in load
    self.im = Image.core.map_buffer(
ValueError: buffer is not large enough

What are your OS, Python and Pillow versions?

  • OS: windows
  • Python: 3.9.5
  • Pillow: 8.10
from PIL import Image

path = "MX11.bmp"
image = Image.open(path)
image .show()

MX11.zip

How to fix this?

I modified ImageFile.load method like this. Put ValueError in the Exception Tuple. It solved my problem. I wonder if there will be side effects.

                try:
                    # use mmap, if possible
                    import mmap

                    with open(self.filename) as fp:
                        self.map = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ)
                    self.im = Image.core.map_buffer(
                        self.map, self.size, decoder_name, offset, args
                    )
                    readonly = 1
                    # After trashing self.im,
                    # we might need to reload the palette data.
                    if self.palette:
                        self.palette.dirty = 1
                except (AttributeError, OSError, ImportError, ValueError):
                    self.map = None

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions