Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

OverflowError in PIL.Image.fromarray #1475

Closed
birgander2 opened this issue Oct 6, 2015 · 17 comments · Fixed by #3791
Closed

OverflowError in PIL.Image.fromarray #1475

birgander2 opened this issue Oct 6, 2015 · 17 comments · Fixed by #3791
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature. Enhancement NumPy

Comments

@birgander2
Copy link

When trying o generate a huge image (73067 x 28160) from a numpy ndarray, pillow throws me an
"OverflowError: size does not fit in an int". Is pillow limited to maximum sizes of below 2**31bit?

I used:

im = PIL.Image.fromarray(array, mode='F')
@wiredfool
Copy link
Member

There are some tests for large images Tests/large_memory_numpy_test.py, but the most they've gone up to is just over 2 gigapixel.

@hugovk
Copy link
Member

hugovk commented Oct 6, 2015

@birgander2 What's the full trace?

@birgander2
Copy link
Author

    arr = PIL.Image.fromarray(array[k, ...].astype(np.float32), mode='F')
  File "/opt/anaconda/lib/python3.4/site-packages/PIL/Image.py", line 2163, in fromarray
    return frombuffer(mode, size, obj, "raw", rawmode, 0, 1)
  File "/opt/anaconda/lib/python3.4/site-packages/PIL/Image.py", line 2113, in frombuffer
    return frombytes(mode, size, data, decoder_name, args)
  File "/opt/anaconda/lib/python3.4/site-packages/PIL/Image.py", line 2047, in frombytes
    im.frombytes(data, decoder_name, args)
  File "/opt/anaconda/lib/python3.4/site-packages/PIL/Image.py", line 728, in frombytes
    s = d.decode(data)
OverflowError: size does not fit in an int
>>> 

And I have to correct myself, the array has only shape (36352, 18144)

@radarhere
Copy link
Member

radarhere commented Oct 7, 2015

From my tests, here is the minimal code required to produce the problem -

import numpy
from PIL import Image

array = numpy.ndarray(shape=(16384, 16384))
Image.fromarray(array)

So the limit is not 2 ** 31, it's 2 ** 28.

@wiredfool
Copy link
Member

Can you try running the large memory test listed above?

@radarhere
Copy link
Member

Both of the large memory tests pass for me.

@wiredfool
Copy link
Member

@birgander2 What platform are you on, and are you on a 64 bit python?

@birgander2
Copy link
Author

I'm on linux using python 3.4.3 with pillow 3.0.0. And yes, it is 64bit python.

@wiredfool
Copy link
Member

wiredfool commented Oct 9, 2015

These tests pass slowly:

    def _write_png(self, xdim, ydim):
        dtype = np.uint8
        a = np.zeros((xdim, ydim), dtype=dtype)
        f = self.tempfile('temp.png')
        im = Image.fromarray(a, 'L')
        im.save(f)

    def test_large(self):
        """ succeeded prepatch"""
        self._write_png(XDIM, YDIM)

    def test_2gpx(self):
        """failed prepatch"""
        self._write_png(XDIM, XDIM)

    def test_1475(self):
        """ issue #1475 """
        self._write_png(36352, 18144)

This test fails:

        a = np.zeros((36352, 18144), dtype=np.float)
        im = Image.fromarray(a, mode='F')

I'm digging in to see where we're actually failing, but it does appear that the float has something to do with it.

@wiredfool
Copy link
Member

wiredfool commented Oct 9, 2015

Yep. There's an int length in there.

What's happening is that for some cases, we need to decode the bytes.

That's effectively here in decode.c:

    UINT8* buffer;
    int bufsize, status;
    ImagingSectionCookie cookie;

    if (!PyArg_ParseTuple(args, PY_ARG_BYTES_LENGTH, &buffer, &bufsize))
        return NULL;

It's an integer bufsize. It looks like it can be fixed by changing all the ints to py_ssize_t, and defining PY_SSIZE_T_CLEAN before including Python.h

wiredfool added a commit to wiredfool/Pillow that referenced this issue Oct 9, 2015
@wiredfool wiredfool added Bug Any unexpected behavior, until confirmed feature. Enhancement labels Dec 29, 2015
@wiredfool wiredfool added this to the 3.2.0 milestone Dec 29, 2015
bgilbert added a commit to openslide/openslide-python that referenced this issue Jun 10, 2016
PIL.Image.frombuffer() raises OverflowError on buffers >= 2 GB when
mapping color channels (python-pillow/Pillow#1475).  Work around this by
loading large buffers in smaller chunks and pasting them into the result
image.

Fixes #17.
@radarhere radarhere removed this from the 3.2.0 milestone Jan 6, 2017
@ibesora
Copy link

ibesora commented Jan 18, 2018

Hi, is there some progress in this issue?
I've been working with very big images and I get the same error on version 5.

@wiredfool
Copy link
Member

wiredfool commented Jan 18, 2018

No progress.

@ibesora
Copy link

ibesora commented Jan 19, 2018

Thanks for the info @wiredfool . I suppose it's not something with high priority. Will resort to a workaround instead.

@rcasero

This comment has been minimized.

@bgilbert

This comment has been minimized.

@rcasero

This comment has been minimized.

@radarhere
Copy link
Member

@wiredfool's suggestion was coincidentally implemented in #3749. With that PR, the error here changes from 'OverflowError: size does not fit in an int' to 'ValueError: not enough image data'.

I have created PR #3791 to take the last step to resolve this.

@aclark4life aclark4life added the Anaconda Issues with Anaconda's Pillow label May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Anaconda Issues with Anaconda's Pillow Bug Any unexpected behavior, until confirmed feature. Enhancement NumPy
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants